-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_test.go
88 lines (76 loc) · 1.24 KB
/
conv_test.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//go:build js && wasm
package js
import (
"syscall/js"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConv(t *testing.T) {
type myTest struct {
Name string
Age int
OK bool
X float64
Y int64
Data *myTest
Z []*myTest
}
a := &myTest{
Name: "abc",
Age: 100,
OK: true,
X: 123.456,
Y: 123456789,
Data: &myTest{
Name: "def",
Age: 10,
OK: false,
X: -123.456,
Y: -123456789,
Data: nil,
Z: nil,
},
Z: []*myTest{
{
Name: "1",
Age: 1,
OK: true,
},
{
Name: "2",
Age: 2,
OK: false,
},
{
Name: "3",
Age: 3,
OK: true,
},
},
}
testConv := func(t *testing.T, name string, src, tmp, ans any) {
jsv, err := Marshal(src)
if err != nil {
t.Error(name, err)
return
}
if err := Unmarshal(jsv, tmp); err != nil {
t.Error(name, err)
return
}
assert.Equal(t, tmp, ans, name)
}
testConv(t, "struct", a, new(myTest), a)
boola := true
boolb := false
testConv(t, "boolean", boola, &boolb, &boola)
stra := "ABC"
strb := ""
testConv(t, "string", stra, &strb, &stra)
c := new(myTest)
if err := Unmarshal(js.Null(), c); err != nil {
t.Log("nil", err)
} else {
t.Log("test nil", c)
}
}