-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt_for_test.go
82 lines (69 loc) · 1.71 KB
/
stmt_for_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
package codegen
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestForNoCheckPanic(t *testing.T) {
assert.Panics(t, func() {
var sb strings.Builder
For(Declare("i").Values(Int(0)), nil, Identifier("i").Increment()).
writeStmt(&sb)
})
}
func TestForOnlyCheck(t *testing.T) {
const want = `for ;obj1.uid!=obj2.uid; {
}
`
var sb strings.Builder
got := For(nil, Identifier("obj1").Field("uid").NotEquals(Identifier("obj2").Field("uid")), nil).
writeStmt(&sb)
assert.False(t, got)
assert.Equal(t, want, sb.String())
}
func TestForInitAndCheck(t *testing.T) {
const want = `for i:=0;i<len(myStr); {
}
`
var sb strings.Builder
got := For(Declare("i").Values(Int(0)), Identifier("i").LessThan(Len(Identifier("myStr"))), nil).
writeStmt(&sb)
assert.False(t, got)
assert.Equal(t, want, sb.String())
}
func TestForCheckAndPost(t *testing.T) {
const want = `for ;i<len(myStr);i++ {
}
`
var sb strings.Builder
got := For(nil, Identifier("i").LessThan(Len(Identifier("myStr"))), Identifier("i").Increment()).
writeStmt(&sb)
assert.False(t, got)
assert.Equal(t, want, sb.String())
}
func TestForAllStatements(t *testing.T) {
const want = `for i:=0;i<len(myStr);i++ {
}
`
var sb strings.Builder
got := For(Declare("i").Values(Int(0)), Identifier("i").LessThan(Len(Identifier("myStr"))), Identifier("i").Increment()).
writeStmt(&sb)
assert.False(t, got)
assert.Equal(t, want, sb.String())
}
func TestForWithBlock(t *testing.T) {
const want = `for true {
if false {
return
}
}
`
var sb strings.Builder
got := For(nil, Identifier("true"), nil).Block(
If(Identifier("false")).Block(
Return(),
),
).writeStmt(&sb)
assert.False(t, got)
assert.Equal(t, want, formatSb(sb))
}