-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_test.go
102 lines (95 loc) · 1.91 KB
/
generate_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
package goabnf_test
import (
"testing"
goabnf "github.com/pandatix/go-abnf"
"github.com/stretchr/testify/assert"
)
var (
testsGenerate = map[string]struct {
Grammar *goabnf.Grammar
Seed int64
Rulename string
ExpectErr bool
}{
"self-loop": {
Grammar: mustGrammar("a=a\r\n"),
Seed: 0,
Rulename: "a",
ExpectErr: true,
},
"deep-loop": {
Grammar: mustGrammar("a=b\r\nb=\"b\" a\r\n"),
Seed: 0,
Rulename: "a",
ExpectErr: true,
},
"avoidable-loop": {
Grammar: mustGrammar("a=*a b\r\nb=\"b\" *a\r\n"),
Seed: 0,
Rulename: "a",
ExpectErr: false,
},
"reflected-loop": {
Grammar: mustGrammar("a=b\r\nb=b\r\n"),
Seed: 0,
Rulename: "a",
ExpectErr: true,
},
"abnf-rulelist-0": {
Grammar: goabnf.ABNF,
Seed: 0,
Rulename: "rulelist",
ExpectErr: false,
},
"abnf-rulelist-1": {
Grammar: goabnf.ABNF,
Seed: 1,
Rulename: "rulelist",
ExpectErr: false,
},
"abnf-rule-64": {
Grammar: goabnf.ABNF,
Seed: 64,
Rulename: "rule",
ExpectErr: false,
},
"abnf-rule-14": {
Grammar: goabnf.ABNF,
Seed: 14,
Rulename: "rule",
ExpectErr: false,
},
"abnf-rulelist-499": {
Grammar: goabnf.ABNF,
Seed: 499,
Rulename: "rulelist",
ExpectErr: false,
},
}
)
func Test_U_Generate(t *testing.T) {
t.Parallel()
for testname, tt := range testsGenerate {
t.Run(testname, func(t *testing.T) {
assert := assert.New(t)
// Generate a random output for a given rule
out, err := tt.Grammar.Generate(tt.Seed, tt.Rulename, goabnf.WithRepMax(4), goabnf.WithThreshold(64))
if tt.ExpectErr {
assert.NotNil(err)
return
} else {
if !assert.Nil(err) {
return
}
}
assert.NotEmpty(out)
})
}
}
func mustGrammar(input string) *goabnf.Grammar {
g, err := goabnf.ParseABNF([]byte(input))
if err != nil {
panic(err)
}
return g
}