-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag_test.go
186 lines (169 loc) · 4 KB
/
dag_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
package goabnf_test
import (
_ "embed"
"testing"
goabnf "github.com/pandatix/go-abnf"
"github.com/stretchr/testify/assert"
)
//go:embed testdata/nocycle.abnf
var nocycleAbnf []byte
//go:embed testdata/cycle.abnf
var cycleAbnf []byte
func Test_U_IsDAG(t *testing.T) {
t.Parallel()
var tests = map[string]struct {
Input []byte
ExpectedIsDag bool
}{
"no-cycle": {
Input: nocycleAbnf,
ExpectedIsDag: true,
},
"cycle": {
Input: cycleAbnf,
ExpectedIsDag: false,
},
"abnf": {
// The ABNF grammar is cylic due to the ~Composite DP on alternation with group and option
Input: abnfAbnf,
ExpectedIsDag: false,
},
}
for testname, tt := range tests {
t.Run(testname, func(t *testing.T) {
assert := assert.New(t)
g, err := goabnf.ParseABNF(tt.Input)
if !assert.Nil(err) {
t.FailNow()
}
// Side checks that this dependency graph does not produce an
// empty dependency graph. It should not be, at any time,
// as only the diagram header is a non-empty content.
mrmd := g.DependencyGraph().Mermaid()
assert.NotEmpty(mrmd)
// Do the same with the pretty print.
ptp := g.PrettyPrint()
assert.NotEmpty(ptp)
isDag := g.IsDAG()
assert.Equal(tt.ExpectedIsDag, isDag)
})
}
}
func Test_U_RuleContainsCycle(t *testing.T) {
t.Parallel()
var tests = map[string]struct {
Grammar *goabnf.Grammar
Rulename string
ExpectedRes bool
ExpectErr bool
}{
"abnf": {
Grammar: goabnf.ABNF,
Rulename: "rulelist",
ExpectedRes: true,
ExpectErr: false,
},
"a-self-left": {
Grammar: mustGrammar("a = a \"a\"\r\n"),
Rulename: "a",
ExpectedRes: true,
ExpectErr: false,
},
"a-self-right": {
Grammar: mustGrammar("a = \"a\" a\r\n"),
Rulename: "a",
ExpectedRes: true,
ExpectErr: false,
},
"ab": {
Grammar: mustGrammar("a = b \"a\"\r\nb = a \"b\"\r\n"),
Rulename: "a",
ExpectedRes: true,
ExpectErr: false,
},
}
for testname, tt := range tests {
t.Run(testname, func(t *testing.T) {
assert := assert.New(t)
res, err := tt.Grammar.RuleContainsCycle(tt.Rulename)
if (err != nil) != tt.ExpectErr {
t.Fatalf("Expected error: %t ; got %v", tt.ExpectErr, err)
}
assert.Equal(tt.ExpectedRes, res)
})
}
}
func Test_U_IsLeftTerminating(t *testing.T) {
t.Parallel()
var tests = map[string]struct {
Grammar *goabnf.Grammar
Rulename string
ExpectedRes bool
ExpectErr bool
}{
"abnf": {
Grammar: goabnf.ABNF,
Rulename: "rulelist",
ExpectedRes: true,
ExpectErr: false,
},
"a-left": {
Grammar: mustGrammar("a = a \"a\"\r\n"),
Rulename: "a",
ExpectedRes: false,
ExpectErr: false,
},
"a-right": {
Grammar: mustGrammar("a = \"a\" a\r\n"),
Rulename: "a",
ExpectedRes: true,
ExpectErr: false,
},
"ab-left": {
Grammar: mustGrammar("a = b \"a\"\r\nb = a \"b\"\r\n"),
Rulename: "a",
ExpectedRes: false,
ExpectErr: false,
},
"ab-right": {
Grammar: mustGrammar("a = b \"a\"\r\nb = \"b\"\r\n"),
Rulename: "a",
ExpectedRes: true,
ExpectErr: false,
},
"option-a-left": {
Grammar: mustGrammar("a = [a] \"a\"\r\n"),
Rulename: "a",
ExpectedRes: false,
ExpectErr: false,
},
"option-a-right": {
Grammar: mustGrammar("a = [\"a\"] a\r\n"),
Rulename: "a",
ExpectedRes: false,
ExpectErr: false,
},
"group-a-left": {
Grammar: mustGrammar("a = (a) \"a\"\r\n"),
Rulename: "a",
ExpectedRes: false,
ExpectErr: false,
},
"group-a-right": {
Grammar: mustGrammar("a = (\"a\") a\r\n"),
Rulename: "a",
ExpectedRes: true,
ExpectErr: false,
},
}
for testname, tt := range tests {
t.Run(testname, func(t *testing.T) {
assert := assert.New(t)
res, err := tt.Grammar.IsLeftTerminating(tt.Rulename)
if (err != nil) != tt.ExpectErr {
t.Fatalf("Expected error: %t ; got %v", tt.ExpectErr, err)
}
assert.Equal(tt.ExpectedRes, res)
})
}
}