-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
163 lines (148 loc) · 4.13 KB
/
Copy pathparser_test.go
File metadata and controls
163 lines (148 loc) · 4.13 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sexpr
import (
"math/big"
"testing"
)
func buildNilExample() *SExpr {
return mkNil()
}
func buildNumberExample() *SExpr {
return mkNumber(big.NewInt(100))
}
func buildSymbolExample() *SExpr {
return mkSymbol("+")
}
func buildConsCellExample() *SExpr {
return mkConsCell(mkSymbol("A"), mkSymbol("B"))
}
func buildProperListExample() *SExpr {
return mkConsCell(mkSymbol("A"), mkConsCell(mkSymbol("B"), mkConsCell(mkSymbol("C"), mkNil())))
}
func buildDottedListExample() *SExpr {
return mkConsCell(mkSymbol("A"), mkConsCell(mkSymbol("B"), mkSymbol("C")))
}
func buildQuoteExample() *SExpr {
return mkConsCell(mkSymbol("QUOTE"), mkConsCell(mkSymbol("A"), mkNil()))
}
type SExprBuilder func() *SExpr
func TestParseExample(t *testing.T) {
for idx, test := range []struct {
input string
expectedSExprBuilder SExprBuilder
}{
{"()", buildNilExample},
{"100", buildNumberExample},
{"+", buildSymbolExample},
{"(A . B)", buildConsCellExample},
{"(A B C)", buildProperListExample},
{"(A B . C)", buildDottedListExample},
{"'A", buildQuoteExample},
} {
actual, err := NewParser().Parse(test.input)
expected := test.expectedSExprBuilder()
if err != nil {
t.Errorf("\nin test %d\nunexpected error", idx)
continue
}
if actual.SExprString() != expected.SExprString() {
t.Errorf("\nin test %d (\"%s\")\nerror: got %s\n expected %s",
idx, test.input, actual.SExprString(), expected.SExprString())
}
}
}
func TestParserInvalid(t *testing.T) {
for idx, test := range []string{
"",
"(",
"'",
")",
"x)",
"( ) ( )",
"(a . () . () . ())",
"((x .",
"(x",
"x|",
"|",
"2)",
"')",
"()|",
"(A.B)|",
"(|)",
"(A|B)",
"A|B",
"A.B",
"A B",
"(A)B)",
"(A|",
} {
_, err := NewParser().Parse(test)
if err == nil {
t.Errorf("\nin test %d\nshould error", idx)
}
}
}
func TestParserProperList(t *testing.T) {
for idx, test := range []struct {
input, expectedSExprString string
}{
{"()", "NIL"},
{"a", "A"},
// proper lists
{"(())", "(NIL . NIL)"},
{"(a)", "(A . NIL)"},
{"((a))", "((A . NIL) . NIL)"},
//
{"(a b c)", "(A . (B . (C . NIL)))"},
{"(a b c d)", "(A . (B . (C . (D . NIL))))"},
{"( a b c d e)", "(A . (B . (C . (D . (E . NIL)))))"},
{"( (a b )c)", "((A . (B . NIL)) . (C . NIL))"},
{"(a b (c d))", "(A . (B . ((C . (D . NIL)) . NIL)))"},
{"(a () () a)", "(A . (NIL . (NIL . (A . NIL))))"},
// dotted lists
{"(a (b . c))", "(A . ((B . C) . NIL))"},
{"(a . b)", "(A . B)"},
{"(a . (b . c))", "(A . (B . C))"},
{"(a . (b . (c . d)))", "(A . (B . (C . D)))"},
{"(a . ((b . c) . d))", "(A . ((B . C) . D))"},
//
{"(a b . c)", "(A . (B . C))"},
{"(a b c . d)", "(A . (B . (C . D)))"},
{"(a (b c) d . e)", "(A . ((B . (C . NIL)) . (D . E)))"},
{"(a b (c d) . e)", "(A . (B . ((C . (D . NIL)) . E)))"},
{"(a b c . (d e))", "(A . (B . (C . (D . (E . NIL)))))"},
{"(a b c . (d . e))", "(A . (B . (C . (D . E))))"},
{"(a(b.c ))", "(A . ((B . C) . NIL))"},
//
{"(a . ( ( ) . ( ( ) . a)))", "(A . (NIL . (NIL . A)))"},
} {
actual, err := NewParser().Parse(test.input)
if err != nil {
t.Errorf("\nin test %d\nunexpected error", idx)
continue
}
if actual.SExprString() != test.expectedSExprString {
t.Errorf("\nin test %d (\"%s\")\nerror: got %s\n expected %s",
idx, test.input, actual.SExprString(), test.expectedSExprString)
}
}
}
func TestParseQuote(t *testing.T) {
for idx, test := range []struct {
input, expectedSExpr string
}{
{"'(1 2)", "(QUOTE . ((1 . (2 . NIL)) . NIL))"},
{"'(1 . 2)", "(QUOTE . ((1 . 2) . NIL))"},
{"(quote . (1 . 2))", "(QUOTE . (1 . 2))"},
{"'a", "(QUOTE . (A . NIL))"},
{"'(a)", "(QUOTE . ((A . NIL) . NIL))"},
{"''a", "(QUOTE . ((QUOTE . (A . NIL)) . NIL))"},
{"''(a)", "(QUOTE . ((QUOTE . ((A . NIL) . NIL)) . NIL))"},
{"(' a 'b ' c)", "((QUOTE . (A . NIL)) . ((QUOTE . (B . NIL)) . ((QUOTE . (C . NIL)) . NIL)))"},
} {
actual, _ := NewParser().Parse(test.input)
if actual.SExprString() != test.expectedSExpr {
t.Errorf("\nerror: in test %d (\"%s\"):\n\texpected: %s\n\tgot %s",
idx, test.input, test.expectedSExpr, actual.SExprString())
}
}
}