This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
192 lines (177 loc) · 5.21 KB
/
parser_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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
//
// This file is part of sxpf.
//
// sxpf is licensed under the latest version of the EUPL // (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//-----------------------------------------------------------------------------
package sxpf_test
import (
"bytes"
"io"
"strings"
"testing"
"github.com/t73fde/sxpf"
)
func TestParseString(t *testing.T) {
pml, pmr := strings.Repeat("(", 5000), strings.Repeat(")", 5000)
bml, bmr := strings.Repeat("[", 5000), strings.Repeat("]", 5000)
testcases := []struct {
src string
exp string
}{
{"a", "A"},
{"a ", "A"},
{"a ; comment", "A"},
{`""`, `""`},
{`"a"`, `"a"`},
{`"\""`, `"\""`},
{`"\\"`, `"\\"`},
{`"\t"`, `"\t"`},
{`"\r"`, `"\r"`},
{`"\n"`, `"\n"`},
{`"\x"`, `"x"`}, {`"\x4"`, `"x4"`}, {`"\x41"`, `"A"`}, {`"\x4g"`, `"x4g"`},
{`"\u"`, `"u"`}, {`"\u0"`, `"u0"`}, {`"\u00"`, `"u00"`}, {`"\u004"`, `"u004"`}, {`"\u0042"`, `"B"`},
{`"\U"`, `"U"`}, {`"\U0"`, `"U0"`}, {`"\U00"`, `"U00"`}, {`"\U000"`, `"U000"`}, {`"\U0000"`, `"U0000"`},
{`"\U00004"`, `"U00004"`}, {`"\U000043"`, `"C"`},
{"()", "()"},
{"(a)", "(A)"},
{"((a))", "((A))"},
{"(a b c)", "(A B C)"},
{"(a b . c)", "(A B . C)"},
{`("a" b "c")`, `("a" B "c")`},
{`("a" "b" "c")`, `("a" "b" "c")`},
{`("a""b""c")`, `("a" "b" "c")`},
{"(A ((b c) d) (e f))", "(A ((B C) D) (E F))"},
{"(A.B)", "(A . B)"},
{`("A"."B")`, `("A" . "B")`},
{`("A".b)`, `("A" . B)`},
{"[]", "[]"},
{"[a]", "[A]"},
{"[[a]]", "[[A]]"},
{"[a b c]", "[A B C]"},
{`["a" b "c"]`, `["a" B "c"]`},
{"[A [[b c] d] [e f]]", "[A [[B C] D] [E F]]"},
{"{}", "{}"},
{"{a}", "{A ()}"},
{`{"a" a}`, `{"a" A}`},
{"{c {a}}", "{C {A ()}}"},
{"{c{a b}}", "{C {A B}}"},
{"A; bla", "A"},
{"; bla\na", "A"},
{"; bla\n\r\n\na", "A"},
{"; bla\n; bla\na", "A"},
{"; bla\n\n; bla\na", "A"},
{pml + pml + pmr + pmr, pml + pml + pmr + pmr},
{bml + bml + bmr + bmr, bml + bml + bmr + bmr},
{pml + bml + bmr + pmr, pml + bml + bmr + pmr},
{bml + pml + pmr + bmr, bml + pml + pmr + bmr},
}
for i, tc := range testcases {
smk := sxpf.NewTrivialSymbolMaker()
val, err := sxpf.ParseString(smk, tc.src)
if err != nil {
t.Errorf("%d: ReadString(%q) resulted in error: %v", i, tc.src, err)
continue
}
if val == nil {
t.Errorf("%d: ReadString(%q) resulted in nil value", i, tc.src)
continue
}
got := val.String()
if tc.exp != got {
t.Errorf("%d: ReadString(%q) should return %q, but got %q", i, tc.src, tc.exp, got)
}
}
}
func TestParseMultiple(t *testing.T) {
testcases := []struct {
src string
exp string
}{
{"", ""},
{"A B", "A B"},
{" A B ", "A B"},
{"[A][B]", "[A] [B]"},
{" [A] [B] ", "[A] [B]"},
{"A;B\nC", "A C"},
}
for i, tc := range testcases {
var buf bytes.Buffer
smk := sxpf.NewTrivialSymbolMaker()
reader := bytes.NewBufferString(tc.src)
for cnt := 0; ; cnt++ {
val, err := sxpf.ParseValue(smk, reader)
if err == io.EOF {
break
}
if err != nil {
t.Errorf("%d: ReadString(%q) resulted in error: %v", i, tc.src, err)
continue
}
if val == nil {
t.Errorf("%d: ReadString(%q) resulted in nil value", i, tc.src)
continue
}
if cnt > 0 {
buf.WriteByte((' '))
}
buf.WriteString(val.String())
}
got := buf.String()
if tc.exp != got {
t.Errorf("%d: ReadString(%q) should return %q, but got %q", i, tc.src, tc.exp, got)
}
}
}
func TestParseBytesWithError(t *testing.T) {
testcases := []struct {
src string
msg string
}{
{"A B", sxpf.ErrMissingEOF.Error()},
{"(A", sxpf.ErrMissingCloseParenthesis.Error()},
{"(", sxpf.ErrMissingCloseParenthesis.Error()},
{")", sxpf.ErrMissingOpenParenthesis.Error()},
{"())", sxpf.ErrMissingEOF.Error()}, // b/c "()" is already an expression
{`("`, sxpf.ErrMissingQuote.Error()},
{`(")`, sxpf.ErrMissingQuote.Error()},
{`(")()`, sxpf.ErrMissingQuote.Error()},
{"(.A)", sxpf.ErrMissingCloseParenthesis.Error()},
{"[A", sxpf.ErrMissingCloseBracket.Error()},
{"[", sxpf.ErrMissingCloseBracket.Error()},
{"]", sxpf.ErrMissingOpenBracket.Error()},
{"[]]", sxpf.ErrMissingEOF.Error()}, // b/c "[]" is already an expression
{`["`, sxpf.ErrMissingQuote.Error()},
{`["]`, sxpf.ErrMissingQuote.Error()},
{`["][]`, sxpf.ErrMissingQuote.Error()},
{`"`, sxpf.ErrMissingQuote.Error()},
{`"a`, sxpf.ErrMissingQuote.Error()},
{`"\`, sxpf.ErrMissingQuote.Error()},
{`"\"`, sxpf.ErrMissingQuote.Error()},
{`"\x`, sxpf.ErrMissingQuote.Error()},
{`"\x1`, sxpf.ErrMissingQuote.Error()},
{`"\x11`, sxpf.ErrMissingQuote.Error()},
}
for i, tc := range testcases {
smk := sxpf.NewTrivialSymbolMaker()
val, err := sxpf.ParseBytes(smk, []byte(tc.src))
if err == nil {
t.Errorf("%d: ReadString(%q) should result in error, but got value of type %T: %v", i, tc.src, val, val)
continue
}
got := err.Error()
if got != tc.msg {
t.Errorf("%d: ReadString(%q) should result in error %q, but got %q", i, tc.src, tc.msg, got)
}
}
}
func FuzzParseBytes(f *testing.F) {
smk := sxpf.NewTrivialSymbolMaker()
f.Fuzz(func(t *testing.T, src []byte) {
t.Parallel()
sxpf.ParseBytes(smk, src)
})
}