-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
65 lines (60 loc) · 1.42 KB
/
main_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
package main
import (
"fmt"
"testing"
)
func TestParseL(t *testing.T) {
tests := map[string]string{
"1": "1",
"1 + 2": "{1 + 2}",
"1 + 2 * 3": "{{1 + 2} * 3}",
"1 * 2 + 3": "{{1 * 2} + 3}",
"1 * 2 + 3 * 4": "{{{1 * 2} + 3} * 4}",
"1 + 2 * 3 + 4": "{{{1 + 2} * 3} + 4}",
}
for in, want := range tests {
tokens := newTokens(in)
expr := parseL(tokens)
got := fmt.Sprintf("%v", expr)
assertEqual(t, want, got)
}
}
func TestParseR(t *testing.T) {
tests := map[string]string{
"1": "1",
"1 + 2": "{1 + 2}",
"1 + 2 * 3": "{1 + {2 * 3}}",
"1 * 2 + 3": "{1 * {2 + 3}}",
"1 * 2 + 3 * 4": "{1 * {2 + {3 * 4}}}",
"1 + 2 * 3 + 4": "{1 + {2 * {3 + 4}}}",
}
for in, want := range tests {
tokens := newTokens(in)
expr := parseR(tokens)
got := fmt.Sprintf("%v", expr)
assertEqual(t, want, got)
}
}
func TestParse(t *testing.T) {
tests := map[string]string{
"1": "1",
"1 + 2": "{1 + 2}",
"1 + 2 * 3": "{1 + {2 * 3}}",
"1 * 2 + 3": "{{1 * 2} + 3}",
"1 * 2 + 3 * 4": "{{1 * 2} + {3 * 4}}",
"1 + 2 * 3 + 4": "{{1 + {2 * 3}} + 4}",
}
for in, want := range tests {
tokens := newTokens(in)
expr := parse(tokens, lowest)
got := fmt.Sprintf("%v", expr)
assertEqual(t, want, got)
}
}
func assertEqual(t *testing.T, want, got string) {
t.Helper()
if want == got {
return
}
t.Fatalf("want != got\n%s\n%s", want, got)
}