-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
94 lines (90 loc) · 1.9 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
package pl_test
import (
"testing"
"github.com/lesomnus/pl"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
tcs := []struct {
desc string
input string
expected *pl.Pl
}{
{
desc: "function without arguments",
input: `(a)`,
expected: pl.NewPl(
must(pl.NewFn("a")),
),
},
{
desc: "function with reference arguments",
input: `(a $.b[1]["c-1"] $[2]["d-d"][3] $.e[4][f])`,
expected: pl.NewPl(
must(pl.NewFn("a",
must(pl.NewRef("b", 1, "c-1")),
must(pl.NewRef(2, "d-d", 3)),
must(pl.NewRef("e", 4, "f")),
)),
),
},
{
desc: "function with multiple arguments",
input: `(a "b" 42 $.a[1].b 3.14 "36")`,
expected: pl.NewPl(
must(pl.NewFn("a", "b", 42, must(pl.NewRef("a", 1, "b")), 3.14, "36")),
),
},
{
desc: "sequence of functions",
input: `(a "b" 42 3.14 "36" | c "d" 21)`,
expected: pl.NewPl(
must(pl.NewFn("a", "b", 42, 3.14, "36")),
must(pl.NewFn("c", "d", 21)),
),
},
{
desc: "nested function",
input: `(a "b" (c "d" 21) 3.14 "36")`,
expected: pl.NewPl(
must(pl.NewFn("a", "b", pl.NewPl(must(pl.NewFn("c", "d", 21))), 3.14, "36")),
),
},
{
desc: "consecutive nested functions",
input: `(a "b" (c "d" 21) (e 3.14) 37)`,
expected: pl.NewPl(
must(pl.NewFn(
"a",
"b",
pl.NewPl(must(pl.NewFn("c", "d", 21))),
pl.NewPl(must(pl.NewFn("e", 3.14))),
37,
)),
),
},
{
desc: "nested sequence of functions",
input: `(a "b" (c "d" 21 | e 3.14) 37)`,
expected: pl.NewPl(
must(pl.NewFn(
"a",
"b",
pl.NewPl(
must(pl.NewFn("c", "d", 21)),
must(pl.NewFn("e", 3.14)),
),
37,
)),
),
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
fns, err := pl.ParseString(tc.input)
require.NoError(err)
require.Equal(tc.expected, fns)
})
}
}