-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
165 lines (148 loc) · 2.78 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
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
// SPDX-FileCopyrightText: 2020 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package asciidoctor
import (
"testing"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
func TestGenerateID(t *testing.T) {
type testCase struct {
inputExp map[string]string
doc *Document
desc string
}
var cases = []testCase{{
desc: `Without idprefix and idseparator`,
doc: &Document{
Attributes: DocumentAttribute{},
},
inputExp: map[string]string{
`a b c`: `a_b_c`,
` a b c`: `_a_b_c`,
},
}, {
desc: `With idprefix`,
doc: &Document{
Attributes: DocumentAttribute{
Entry: map[string]string{
docAttrIDPrefix: `123`,
},
},
},
inputExp: map[string]string{
`a b c`: `_123a_b_c`,
` a b c`: `_123_a_b_c`,
},
}, {
desc: `With empty idseparator`,
doc: &Document{
Attributes: DocumentAttribute{
Entry: map[string]string{
docAttrIDSeparator: ``,
},
},
},
inputExp: map[string]string{
`a b c`: `abc`,
` a b c`: `abc`,
},
}, {
desc: `With idseparator`,
doc: &Document{
Attributes: DocumentAttribute{
Entry: map[string]string{
docAttrIDSeparator: `-`,
},
},
},
inputExp: map[string]string{
`a b c`: `a-b-c`,
` a b c`: `_-a-b-c`,
},
}, {
desc: `With idprefix and idseparator`,
doc: &Document{
Attributes: DocumentAttribute{
Entry: map[string]string{
docAttrIDPrefix: `id_`,
docAttrIDSeparator: `-`,
},
},
},
inputExp: map[string]string{
`a b c`: `id_a-b-c`,
` a b c`: `id_-a-b-c`,
},
}}
var (
c testCase
inp string
exp string
got string
)
for _, c = range cases {
for inp, exp = range c.inputExp {
got = generateID(c.doc, inp)
test.Assert(t, c.desc, exp, got)
}
}
}
func TestParseClosedBracket(t *testing.T) {
type testCase struct {
input string
expOut string
expIdx int
}
var cases = []testCase{{
input: `test:[]] input`,
expOut: `test:[]`,
expIdx: 7,
}, {
input: `[test:[]]] input`,
expOut: `[test:[]]`,
expIdx: 9,
}, {
input: `[test:[]] input`,
expOut: ``,
expIdx: -1,
}, {
input: `test:\[\]] input`,
expOut: `test:[]`,
expIdx: 9,
}, {
input: `test:\x\]] input`,
expOut: `test:\x]`,
expIdx: 9,
}}
var (
c testCase
got []byte
gotIdx int
)
for _, c = range cases {
t.Logf(`input: %s`, c.input)
got, gotIdx = parseClosedBracket([]byte(c.input), '[', ']')
test.Assert(t, `got`, c.expOut, string(got))
test.Assert(t, `got index`, c.expIdx, gotIdx)
}
}
func TestIsValidID(t *testing.T) {
type testCase struct {
id string
exp bool
}
var cases = []testCase{{
id: `a`,
exp: true,
}, {
id: `1`,
}}
var (
c testCase
got bool
)
for _, c = range cases {
got = isValidID([]byte(c.id))
test.Assert(t, c.id, c.exp, got)
}
}