File: main_test.go

package info (click to toggle)
golang-github-alecthomas-participle-v2 2.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: javascript: 1,164; sh: 41; makefile: 7
file content (61 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download
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
package main

import (
	"testing"

	require "github.com/alecthomas/assert/v2"
)

func TestCustomExprParser(t *testing.T) {
	type testCase struct {
		src      string
		expected Expr
	}

	for _, c := range []testCase{
		{`1`, ExprNumber{1}},
		{`1.5`, ExprNumber{1.5}},
		{`"a"`, ExprString{"a"}},
		{`(1)`, ExprParens{ExprNumber{1}}},
		{`1+1`, ExprBinary{ExprNumber{1}, "+", ExprNumber{1}}},
		{`1-1`, ExprBinary{ExprNumber{1}, "-", ExprNumber{1}}},
		{`1*1`, ExprBinary{ExprNumber{1}, "*", ExprNumber{1}}},
		{`1/1`, ExprBinary{ExprNumber{1}, "/", ExprNumber{1}}},
		{`1%1`, ExprBinary{ExprNumber{1}, "%", ExprNumber{1}}},
		{`a - -b`, ExprBinary{ExprIdent{"a"}, "-", ExprUnary{"-", ExprIdent{"b"}}}},
		{
			`a + b - c * d / e % f`,
			ExprBinary{
				ExprIdent{"a"}, "+", ExprBinary{
					ExprIdent{"b"}, "-", ExprBinary{
						ExprIdent{"c"}, "*", ExprBinary{
							ExprIdent{"d"}, "/", ExprBinary{
								ExprIdent{"e"}, "%", ExprIdent{"f"},
							},
						},
					},
				},
			},
		},
		{
			`a * b + c * d`,
			ExprBinary{
				ExprBinary{ExprIdent{"a"}, "*", ExprIdent{"b"}},
				"+",
				ExprBinary{ExprIdent{"c"}, "*", ExprIdent{"d"}},
			},
		},
		{
			`(a + b) * (c + d)`,
			ExprBinary{
				ExprParens{ExprBinary{ExprIdent{"a"}, "+", ExprIdent{"b"}}},
				"*",
				ExprParens{ExprBinary{ExprIdent{"c"}, "+", ExprIdent{"d"}}},
			},
		},
	} {
		actual, err := parser.ParseString("", c.src)
		require.NoError(t, err)
		require.Equal(t, c.expected, actual.X)
	}
}