-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
74 lines (62 loc) · 1.57 KB
/
encode_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
package mathematica
import (
"bufio"
"bytes"
"testing"
)
func expectValue(t *testing.T, v interface{}, expected string) {
bbuf := new(bytes.Buffer)
bio := bufio.NewWriter(bbuf)
e := encodeValue(bio, v)
if e != nil {
t.Fatal(e)
return
}
bio.Flush()
encodedTo := bbuf.String()
if encodedTo != expected {
t.Fatalf("Expected `%s`, got `%s` instead", expected, encodedTo)
}
}
func Test_encodeBool(t *testing.T) {
expectValue(t, true, "True")
expectValue(t, false, "False")
}
func Test_encodeFloat(t *testing.T) {
expectValue(t, 1.2, "1.2")
expectValue(t, 3.14159, "3.14159")
expectValue(t, 1.0, "1")
expectValue(t, -1.0, "-1")
expectValue(t, 0.0, "0")
}
func Test_encodeInteger(t *testing.T) {
expectValue(t, 0, "0")
expectValue(t, 1, "1")
expectValue(t, 2, "2")
expectValue(t, -1, "-1")
}
func Test_encodeArrays(t *testing.T) {
expectValue(t, []string{}, `{}`)
expectValue(t, []int{}, `{}`)
expectValue(t, []int{0}, `{0}`)
expectValue(t, []int{0, -1}, `{0,-1}`)
expectValue(t, []string{"hunter"}, `{"hunter"}`)
expectValue(t, []string{"1\n2"}, `{"1\n2"}`)
expectValue(t, []interface{}{0, []int{1, 2, 3}}, `{0,{1,2,3}}`)
}
func Test_encodeStructs(t *testing.T) {
expectValue(t, struct{ X int }{X: 1}, `<|"X"->1|>`)
expectValue(t, struct{ X, Y int }{X: 1}, `<|"X"->1,"Y"->0|>`)
expectValue(t, struct {
X int
Y string
}{Y: "Hunter"}, `<|"X"->0,"Y"->"Hunter"|>`)
expectValue(t, struct {
X int
Y string
Z []int
}{Y: "k", Z: []int{1, 2, 3}}, `<|"X"->0,"Y"->"k","Z"->{1,2,3}|>`)
expectValue(t, struct {
X int `mathematica:"Y"`
}{X: 1}, `<|"Y"->1|>`)
}