-
Notifications
You must be signed in to change notification settings - Fork 0
/
any_test.go
90 lines (73 loc) · 1.73 KB
/
any_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
89
90
package types
import (
"encoding/json"
"fmt"
"math/rand"
"testing"
"time"
"go.olapie.com/utils"
)
func jsonString(i any) string {
b, _ := json.Marshal(i)
return string(b)
}
func nextImage() *Image {
return &Image{
Url: "https://www.image.com/" + fmt.Sprint(time.Now().Unix()),
Width: rand.Int31(),
Height: rand.Int31(),
Format: "png",
}
}
func nextVideo() *Video {
return &Video{
Url: "http://www.video.com/" + fmt.Sprint(time.Now().Unix()),
Format: "rmvb",
Duration: rand.Int31(),
Size: rand.Int31(),
Image: nextImage(),
}
}
func TestAny(t *testing.T) {
t.Run("AliasType", func(t *testing.T) {
type ID int
RegisterAnyType(ID(0))
v := NewAny(ID(10))
b, err := json.Marshal(v)
utils.MustNotErrorT(t, err)
var vv *Any
err = json.Unmarshal(b, &vv)
utils.MustNotErrorT(t, err)
utils.MustEqualT(t, jsonString(v), jsonString(vv))
})
t.Run("String", func(t *testing.T) {
v := NewAny("hello")
b, err := json.Marshal(v)
utils.MustNotErrorT(t, err)
var vv *Any
err = json.Unmarshal(b, &vv)
utils.MustNotErrorT(t, err)
utils.MustEqualT(t, jsonString(v), jsonString(vv))
})
t.Run("Struct", func(t *testing.T) {
v := NewAny(nextVideo())
b, err := json.Marshal(v)
utils.MustNotErrorT(t, err)
var vv *Any
err = json.Unmarshal(b, &vv)
utils.MustNotErrorT(t, err)
utils.MustEqualT(t, jsonString(v), jsonString(vv))
})
t.Run("Array", func(t *testing.T) {
var l []*Any
l = append(l, NewAny("hello"))
l = append(l, NewAny(nextImage()))
l = append(l, NewAny(nextVideo()))
b, err := json.Marshal(l)
utils.MustNotErrorT(t, err)
var ll []*Any
err = json.Unmarshal(b, &ll)
utils.MustNotErrorT(t, err)
utils.MustEqualT(t, jsonString(l), jsonString(ll))
})
}