-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
58 lines (48 loc) · 1.3 KB
/
Copy pathjson_test.go
File metadata and controls
58 lines (48 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package translation
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestJsonMarshaler_Marshal(t *testing.T) {
m := JsonMarshaler{}
t.Run("RawBytes", func(t *testing.T) {
input := []byte("hello world")
output, err := m.Marshal(input)
assert.NoError(t, err)
assert.Equal(t, input, output, "Should be zero-copy for []byte")
})
t.Run("String", func(t *testing.T) {
input := "hello world"
output, err := m.Marshal(input)
assert.NoError(t, err)
assert.Equal(t, []byte(input), output)
})
t.Run("Struct", func(t *testing.T) {
type data struct{ Name string }
input := data{Name: "test"}
output, err := m.Marshal(input)
assert.NoError(t, err)
assert.JSONEq(t, `{"Name":"test"}`, string(output))
})
}
func TestJsonMarshaler_Unmarshal(t *testing.T) {
m := JsonMarshaler{}
t.Run("Struct", func(t *testing.T) {
type data struct{ Name string }
input := []byte(`{"Name":"test"}`)
var output data
err := m.Unmarshal(input, &output)
assert.NoError(t, err)
assert.Equal(t, "test", output.Name)
})
t.Run("InvalidJSON", func(t *testing.T) {
input := []byte(`{invalid}`)
var output map[string]any
err := m.Unmarshal(input, &output)
assert.Error(t, err)
})
}
func TestJsonMarshaler_String(t *testing.T) {
m := JsonMarshaler{}
assert.Equal(t, "json", m.String())
}