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
|
package brechars
import (
"testing"
)
func TestGenerator(t *testing.T) {
tests := []struct {
in string
expected string
}{
{"[abc]", "abc"},
// First characters
{"[]", ""},
{"[-]", "-"},
{"[]abc]", "]abc"},
// Character
{"[\u0e010-2]", "ก012"},
// Not
{"[^:cntrl::punct:]", " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"},
{"[^-:cntrl::digit:]", " !\"#$%&'()*+,./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"},
{"[^]:cntrl::digit:]", " !\"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\^_`abcdefghijklmnopqrstuvwxyz{|}~"},
// Classes
{"[:alnum:]", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"},
{"[:alpha:]", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{"[:digit:]", "0123456789"},
{"[:space:]", " \t\n\r\f\v"},
{"[:blank:]", " \t"},
{"[:word:]", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"},
{"[:cntrl:]", "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\u007f"},
{"[:lower:]", "abcdefghijklmnopqrstuvwxyz"},
{"[:upper:]", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{"[:digit::upper:]", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
{"[:print:]", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"},
{"[:graph:]", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"},
{"[:punct:]", "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]"},
{"[:xdigit:]", "abcdefABCDEF0123456789"},
{"[:digit::punct::upper:]", "0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
// Ranges
{"[a-d]", "abcd"},
{"[\x20-\x7E]", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"},
// Swapped
{"[\x7E-\x20]", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"},
}
for _, tt := range tests {
p, err := New()
if err != nil {
t.Fatalf("New failed with %q", err)
}
actual, err := p.Generate(tt.in)
if err != nil {
t.Errorf("%s => failed with %q", tt.in, err)
}
if actual != tt.expected {
t.Errorf("%s => expected %q, got %q", tt.in, tt.expected, actual)
}
}
}
func TestOptions(t *testing.T) {
tests := []struct {
in string
opts []Option
expected string
}{
{"[a-z]", []Option{MinRune('a'), MaxRune('f')}, "abcdef"},
{"[^:cntrl::punct:]", []Option{MinRune('a'), MaxRune('z')}, "abcdefghijklmnopqrstuvwxyz"},
{"[:upper:]", []Option{MinRune('a'), MaxRune('z')}, ""},
}
for _, tt := range tests {
p, err := New(tt.opts...)
if err != nil {
t.Fatalf("New failed with %q", err)
}
actual, err := p.Generate(tt.in)
if err != nil {
t.Errorf("%s => failed with %q", tt.in, err)
}
if actual != tt.expected {
t.Errorf("%s => expected %q, got %q", tt.in, tt.expected, actual)
}
}
}
func TestErrors(t *testing.T) {
tests := []struct {
in string
expected string
}{
{"abc", "missing opening '['"},
{"[a-]", "invalid range"},
{"[aa-]", "invalid range"},
{"[a-b-]", "parse error, unexpected '-'"},
{"[:digit]", "parse error, expecting ':'"},
{"[ab", "parse error, unexpected EOF"},
{"[:blah:]", "invalid class ':blah:'"},
}
for _, tt := range tests {
p, err := New()
if err != nil {
t.Fatalf("New failed with %q", err)
}
_, err = p.Generate(tt.in)
if err == nil {
t.Errorf("%s => should have failed", tt.in)
}
if err.Error() != tt.expected {
t.Errorf("%s => expected %q, got %q", tt.in, tt.expected, err.Error())
}
}
}
|