This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parserInit_test.go
128 lines (110 loc) · 2.75 KB
/
parserInit_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
package parser_test
import (
"fmt"
"testing"
llp "github.com/romshark/llparser"
"github.com/stretchr/testify/require"
)
func str(format string, a ...interface{}) string {
return fmt.Sprintf(format, a...)
}
func test(t *testing.T, pattern llp.Pattern, expectedErrMsg string) {
// Wrap non-rules into dummy rules
var rule *llp.Rule
if ptr, ok := pattern.(*llp.Rule); ok {
rule = ptr
} else {
rule = &llp.Rule{
Pattern: pattern,
}
}
pr, err := llp.NewParser(rule, rule)
require.Error(t, err)
require.Nil(t, pr)
require.Equal(t, expectedErrMsg, err.Error())
}
type UnsupportedPatternType struct{}
func (UnsupportedPatternType) Container() bool { return true }
func (UnsupportedPatternType) TerminalPattern() llp.Pattern { return nil }
func (UnsupportedPatternType) Desig() string {
return "UnsupportedPatternType"
}
func TestUnsupportedPatternType(t *testing.T) {
test(
t,
UnsupportedPatternType{},
"invalid grammar: unsupported pattern type: "+
"parser_test.UnsupportedPatternType",
)
}
func TestRuleMissingPattern(t *testing.T) {
rl := &llp.Rule{}
test(t, rl, str("invalid grammar: rule %p is missing a pattern", rl))
}
func TestRepeatedMinGreaterMax(t *testing.T) {
min := uint(2)
max := uint(1)
rp := &llp.Repeated{
Min: min,
Max: max,
Pattern: &llp.Exact{Expectation: []rune("test")},
}
test(t, rp, str(
"invalid grammar: repeated %p min (%d) greater max (%d)",
rp,
min,
max,
))
}
func TestRepeatedMissingPattern(t *testing.T) {
rp := &llp.Repeated{}
test(t, rp, str("invalid grammar: repeated %p is missing a pattern", rp))
}
func TestSequenceEmpty(t *testing.T) {
test(t, llp.Sequence{}, "invalid grammar: sequence is empty")
}
func TestEitherEmpty(t *testing.T) {
test(
t,
llp.Either{},
"invalid grammar: either-combinator has 0 option(s)",
)
}
func TestEitherOneOption(t *testing.T) {
test(t, llp.Either{
&llp.Exact{Expectation: []rune("test")},
}, "invalid grammar: either-combinator has 1 option(s)")
}
func TestEitherDuplicateOptions(t *testing.T) {
opt1 := &llp.Exact{Expectation: []rune("opt1")}
opt2 := &llp.Exact{Expectation: []rune("opt2")}
test(t, llp.Either{
opt1,
opt2,
opt1,
}, "invalid grammar: either-combinator has "+
"duplicate options (at index 2)",
)
}
func TestLexedMissingFn(t *testing.T) {
lx := &llp.Lexed{}
test(t, lx, str(
"invalid grammar: lexed-terminal %p is missing the lexer function",
lx,
))
}
func TestExactMissingExpectation(t *testing.T) {
ex := &llp.Exact{}
test(t, ex, str(
"invalid grammar: exact-terminal %p is missing an expectation",
ex,
))
}
func TestNotNested(t *testing.T) {
ex := &llp.Exact{Expectation: []rune("test")}
test(
t,
llp.Not{Pattern: llp.Not{Pattern: ex}},
"invalid grammar: not-combinator is nested",
)
}