-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_editor_test.go
86 lines (72 loc) · 2.68 KB
/
script_editor_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFormatScript1(t *testing.T) {
have := ParseScript("(ally-visible?)")
expect := "(ally-visible?)\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(and 1 2)")
expect = "(and 1 2)\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(and 1 (+ 2 2))")
expect = "(and 1\n (+ 2 2))\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(move (and 1 2))")
expect = "(move (and 1 2))\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(move (and 1 (+ 2 2)))")
expect = "(move (and 1\n (+ 2 2)))\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(and (and 1 2) 3)")
expect = "(and (and 1 2)\n 3)\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(and (and 1 (+ 2 2)) 3)")
expect = "(and (and 1\n (+ 2 2))\n 3)\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
have = ParseScript("(if (or 1 2) (and 2 3) 4)")
expect = "(if (or 1 2)\n (and 2 3)\n 4)\n"
// fmt.Printf("have:\n%s\nexpect:\n%s\n", FormatScript(have), expect)
assert.Equal(t, expect, FormatScript(have))
}
func TestSimplifyScript(t *testing.T) {
tests := map[string]string{
"(if 1 (+ 2 2) (- 3 3))": "4\n",
"(and (- 2 2) (+ 3 3))": "0\n",
"(and (+ 2 2) (+ 3 3))": "6\n",
"(* 2 0)": "0\n",
"(* 2 6)": "12\n",
"(/ 2 0)": "0\n",
"(/ 16 2)": "8\n",
"(mod 2 0)": "0\n",
"(mod 16 5)": "1\n",
"(or 1 (my-x-pos))": "1\n",
"(or 0 (/ 2 0))": "0\n",
"(< (+ 2 2) (+ 3 3))": "1\n",
"(< (+ 3 3) (+ 2 2))": "0\n",
"(> (+ 2 2) (+ 3 3))": "0\n",
"(> (+ 3 3) (+ 2 2))": "1\n",
"(= (+ 2 2) (+ 2 2))": "1\n",
"(= (+ 3 3) (+ 2 2))": "0\n",
"(not (+ 2 2))": "0\n",
"(not (- 2 2))": "1\n",
"(shoot (+ 2 2))": "(shoot 4)\n",
// Expressions that are not constant should not be simplified.
"(if (my-x-pos) (+ 2 2) (- 3 3))": "(if (my-x-pos)\n 4\n 0)\n",
"(and (+ 2 (my-x-pos)) (+ 3 3))": "(and (+ 2 (my-x-pos))\n 6)\n",
"(or (my-x-pos) (my-x-pos))": "(or (my-x-pos) (my-x-pos))\n",
}
for before, after := range tests {
code := ParseScript(before)
SimplifyTree(code)
assert.Equal(t, after, FormatScript(code))
}
}