-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy paths-expression_test.go
87 lines (82 loc) · 2.25 KB
/
s-expression_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
// thanks for the insight from https://rosettacode.org/wiki/S-Expressions#Go
package evaluator
import "testing"
func TestFscan(t *testing.T) {
type input struct {
exp string
err error
}
inputs := []input{
{` `, nil},
{`"\\\\"`, nil},
{`a`, nil},
{`(a)`, nil},
{`(a b c)`, nil},
{`(!(a b c))`, nil},
{`(not (a b c))`, nil},
{`(a b ( c d ) )`, nil},
{`(a b ( c d () ) )`, nil},
{`(a '(' ')' b ( c ) (d e (f g)))`, nil},
{`((data "quoted data" 123 4.5) (data (!@# (4.5) "(more" "data)")))`, nil},
{`((data "quoted data" ")" 123 4.5) (data (!@# (4.5) "(more" "data)")))`, nil},
{``, ErrNilInput},
{`()`, ErrNilInput},
{`("a" 'b)`, ErrUnexpectedEnd},
{`(a b) c`, ErrLeftOverText},
{`(a b) ( c d )`, ErrLeftOverText},
{`'(' "(" "\"(\"" a b c)`, ErrUnmatchedParenthesis},
}
for _, input := range inputs {
_, err := parse(input.exp)
if err != input.err {
t.Errorf("wanna: %v, got: %v", input.err, err)
}
}
}
func ExampleExpression_ignore() {
s := `(!(a b )())`
exp, err := parse(s)
if err != nil {
return
}
exp.dump(0)
// Output:
// evaluator.list: 3 elements: [! [a b] []]
// evaluator.varString: !
// evaluator.list: 2 elements: [a b]
// evaluator.varString: a
// evaluator.varString: b
// evaluator.list: 0 elements: []
}
func TestFscanStringWithQuotesStriped(t *testing.T) {
type input struct {
data string
advance int
token string
err error
}
data := []input{
{``, 0, ``, nil},
{`strings`, 7, `tring`, nil},
{`st'rings`, 8, `t'ring`, nil},
{`"string"`, 8, `string`, nil},
{`"str'ing"`, 9, `str'ing`, nil},
{`"str ing"`, 9, `str ing`, nil},
{`'string'`, 8, `string`, nil},
{`'str\'ing'`, 10, `str'ing`, nil},
{`'str\\'ing'`, 7, `str\\`, nil},
{`'str\\\'ing'`, 12, `str\\'ing`, nil},
{`'`, 0, ``, ErrUnexpectedEnd},
{`string`, 0, ``, ErrUnexpectedEnd},
{`'str\\\'`, 0, ``, ErrUnexpectedEnd},
{`st'ring`, 0, ``, ErrUnexpectedEnd},
}
for _, d := range data {
advance, token, err := scanStringWithQuotesStriped([]byte(d.data))
if d.advance != advance ||
d.token != string(token) ||
d.err != err {
t.Errorf("%s, advanced, token, error expected to be (%d, %s, %v), but (%d, %s, %v)", d.data, d.advance, d.token, d.err, advance, token, err)
}
}
}