Debian Codegraph @

trace/internal/telemetry/conv_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package telemetry
5
6 import (
7 "bytes"
8 "encoding/json"
9 "testing"
10 "time"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 )
15
16 const schema100 = "http://go.opentelemetry.io/schema/v1.0.0"
17
18 var y2k = time.Unix(0, time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano()) // No location.
19
20 func runJSONEncodingTests[T any](decoded T, encoded []byte) func(*testing.T) {
21 return func(t *testing.T) {
22 t.Helper()
23
24 t.Run("Unmarshal", runJSONUnmarshalTest(decoded, encoded))
25 t.Run("Marshal", runJSONMarshalTest(decoded, encoded))
26 }
27 }
28
29 func runJSONMarshalTest[T any](decoded T, encoded []byte) func(*testing.T) {
30 return func(t *testing.T) {
31 t.Helper()
32
33 got, err := json.Marshal(decoded)
34 require.NoError(t, err)
35
36 var want bytes.Buffer
37 require.NoError(t, json.Compact(&want, encoded))
38 assert.Equal(t, want.String(), string(got))
39 }
40 }
41
42 func runJSONUnmarshalTest[T any](decoded T, encoded []byte) func(*testing.T) {
43 return func(t *testing.T) {
44 t.Helper()
45
46 var got T
47 require.NoError(t, json.Unmarshal(encoded, &got))
48 assert.Equal(t, decoded, got)
49 }
50 }