-
Notifications
You must be signed in to change notification settings - Fork 1
/
snippet_basic_test.go
108 lines (89 loc) · 1.88 KB
/
snippet_basic_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package codegen
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestExpr(t *testing.T) {
tt := require.New(t)
tt.Equal(`1 + 1`, Stringify(Expr("? + ?", 1, 1)))
tt.Equal(`"1" + "1"`, Stringify(Expr("? + ?", "1", "1")))
tt.Equal(`i++`, Stringify(Expr("?++", Id("i"))))
}
func TestComments(t *testing.T) {
tt := require.New(t)
tt.Equal(`// 123123
// 123123
`, Stringify(Comments("123123", "123123")))
}
func TestVal(t *testing.T) {
tt := require.New(t)
tt.Error(TryCatch(func() {
v := make(chan string)
Val(v)
}))
tt.Equal(`"string"`, Stringify(Val("string")))
tt.Equal(`1`, Stringify(Val(1)))
tt.Equal(`1.2`, Stringify(Val(1.2)))
tt.Equal(`1.2`, Stringify(Val(float32(1.2))))
tt.Equal(`'b'`, Stringify(Val('b')))
tt.Equal(`'1'`, Stringify(Val('1')))
tt.Equal(`1`, Stringify(Val(int32(1))))
tt.Equal(`true`, Stringify(Val(true)))
tt.Equal(`[]string{
"1",
"2",
}`, Stringify(Val([]string{"1", "2"})))
tt.Equal(`struct {
Name string `+"`"+`json:"name"`+"`"+`
Empty string
}{
Name: "123",
}`, Stringify(Val(struct {
Name string `json:"name"`
Empty string
}{
Name: "123",
})))
{
type Nested struct {
Name string
*Nested
}
n := Nested{}
n.Name = "string"
n.Nested = &Nested{
Name: "string",
}
tt.Equal(`github_com_go_courier_codegen.Nested{
Name: "string",
Nested: &(github_com_go_courier_codegen.Nested{
Name: "string",
}),
}`,
Stringify(Val(n)))
}
tt.Equal(`[]interface {}{
"1",
nil,
}`, Stringify(Val([]interface{}{"1", nil})))
tt.Equal(`[2]interface {}{
"1",
nil,
}`, Stringify(Val([2]interface{}{"1", nil})))
tt.Equal(`map[string]int{
"1": 1,
"2": 2,
}`, Stringify(Val(map[string]int{"2": 2, "1": 1})))
}
func TestSnippet_Block(t *testing.T) {
tt := require.New(t)
tt.Equal(`{
a := Fn("1")
v := Fn("1")
}`, Stringify(
Block(
Define(Id("a")).By(Call("Fn", Val("1"))),
Define(Id("v")).By(Call("Fn", Val("1"))),
),
))
}