-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparser_test.go
116 lines (106 loc) · 2.22 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ncparser
import (
"testing"
)
func TestParser(t *testing.T) {
content := []byte(`
#COMMENT
# DOUBLE #COMMENT
WORD1 WORD2;
WORD3 {
WORD4 'SQ1' "DQ\t1" #COMMENT
;
#COMMENT
}`)
expected := NginxConfigureBlock([]NginxConfigureCommand{
{
Words: []string{"WORD1", "WORD2"},
},
{
Words: []string{"WORD3"},
Block: NginxConfigureBlock([]NginxConfigureCommand{
{
Words: []string{"WORD4", "SQ1", "DQ\t1"},
},
}),
},
})
block, err := Parse(content)
if err != nil {
t.Error("parse fail:", err.Error())
t.FailNow()
}
if !equalBlock(block, expected) {
t.Errorf("unequal block: expected=%v, actual=%v\n", expected, block)
t.FailNow()
}
}
func TestParseUnterminatedCommand(t *testing.T) {
content := []byte(`WORD `)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func TestParseUnterminatedBlock(t *testing.T) {
content := []byte(`WORD { `)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func TestParseInvalidTokenInCommand(t *testing.T) {
content := []byte(`WORD };`)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func TestParseInvalidTokenInBlock(t *testing.T) {
content := []byte(`WORD { {`)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func TestParseInvalidCommandInBlock(t *testing.T) {
content := []byte(`WORD { WORD`)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func TestParseInvalidFirstToken(t *testing.T) {
content := []byte(`}`)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func TestParseWithScannerFailure(t *testing.T) {
content := []byte(`"WORD\|"`)
block, err := Parse(content)
if err == nil {
t.Error("unexpected parse result:", block)
}
}
func equalBlock(a, b NginxConfigureBlock) bool {
if len(a) != len(b) {
return false
}
for i, c := range a {
d := b[i]
if len(c.Words) != len(d.Words) {
return false
}
for j, w := range c.Words {
if w != d.Words[j] {
return false
}
}
if !equalBlock(c.Block, d.Block) {
return false
}
}
return true
}