Skip to content

Commit 438d0c6

Browse files
committed
test variable regex matches
1 parent 20fc61e commit 438d0c6

1 file changed

Lines changed: 197 additions & 0 deletions

File tree

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package variables
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
)
7+
8+
func TestVariableRegex(t *testing.T) {
9+
testCases := []struct {
10+
name string
11+
regex *regexp.Regexp
12+
matches []string
13+
unmatches []string
14+
}{
15+
{
16+
name: "filenameVarRegex",
17+
regex: filenameVarRegex,
18+
matches: []string{
19+
"{f}",
20+
"{f.up}",
21+
"{f.lw}",
22+
"{f.ti}",
23+
"{f.win}",
24+
"{f.mac}",
25+
"{f.di}",
26+
"{f.norm}",
27+
"{f.dt}",
28+
"{f.dt.YYYY}",
29+
},
30+
unmatches: []string{
31+
"f",
32+
"{f.}",
33+
"{f.unknown}",
34+
},
35+
},
36+
{
37+
name: "extensionVarRegex",
38+
regex: extensionVarRegex,
39+
matches: []string{
40+
"{ext}",
41+
"{2ext}",
42+
"{ext.up}",
43+
},
44+
unmatches: []string{
45+
"ext",
46+
"{ext.}",
47+
"{3ext}",
48+
},
49+
},
50+
{
51+
name: "parentDirVarRegex",
52+
regex: parentDirVarRegex,
53+
matches: []string{
54+
"{p}",
55+
"{1p}",
56+
"{10p}",
57+
"{p.up}",
58+
"{p.dt.hh}",
59+
},
60+
unmatches: []string{
61+
"p",
62+
"{p.}",
63+
},
64+
},
65+
{
66+
name: "indexVarRegex",
67+
regex: indexVarRegex,
68+
matches: []string{
69+
"{%d}",
70+
"{$1%d}",
71+
"{%2d}",
72+
"{%05d}",
73+
},
74+
unmatches: []string{
75+
"d",
76+
},
77+
},
78+
{
79+
name: "hashVarRegex",
80+
regex: hashVarRegex,
81+
matches: []string{
82+
"{hash.sha1}",
83+
"{hash.sha256.up}",
84+
},
85+
unmatches: []string{
86+
"{hash}",
87+
"{hash.}",
88+
"{hash.sha1.}",
89+
},
90+
},
91+
{
92+
name: "transformVarRegex",
93+
regex: transformVarRegex,
94+
matches: []string{
95+
"{.up}",
96+
"{<$1>.up}",
97+
"{<text>.up}",
98+
},
99+
unmatches: []string{
100+
"{up}",
101+
"{.}",
102+
},
103+
},
104+
{
105+
name: "csvVarRegex",
106+
regex: csvVarRegex,
107+
matches: []string{
108+
"{csv.1}",
109+
"{csv.10.up}",
110+
},
111+
unmatches: []string{
112+
"{csv}",
113+
"{csv.}",
114+
"{csv.1.}",
115+
},
116+
},
117+
{
118+
name: "exiftoolVarRegex",
119+
regex: exiftoolVarRegex,
120+
matches: []string{
121+
"{xt.Artist}",
122+
"{xt.Comment.up}",
123+
},
124+
unmatches: []string{
125+
"{xt}",
126+
"{xt.}",
127+
"{xt.Artist.}",
128+
},
129+
},
130+
{
131+
name: "id3VarRegex",
132+
regex: id3VarRegex,
133+
matches: []string{
134+
"{id3.title}",
135+
"{id3.artist.up}",
136+
},
137+
unmatches: []string{
138+
"{id3}",
139+
"{id3.}",
140+
"{id3.title.}",
141+
},
142+
},
143+
{
144+
name: "exifVarRegex",
145+
regex: exifVarRegex,
146+
matches: []string{
147+
"{exif.iso}",
148+
"{x.cdt.YYYY}",
149+
},
150+
unmatches: []string{
151+
"{exif}",
152+
"{exif.}",
153+
"{exif.iso.}",
154+
},
155+
},
156+
{
157+
name: "dateVarRegex",
158+
regex: dateVarRegex,
159+
matches: []string{
160+
"{mtime.YYYY}",
161+
"{ctime.H}",
162+
"{btime.DDDD.up}",
163+
"{atime.MMM}",
164+
"{mtime}",
165+
"{ctime}",
166+
"{btime}",
167+
"{atime}",
168+
"{now}",
169+
"{now.hh.lw}",
170+
},
171+
},
172+
}
173+
174+
for _, tc := range testCases {
175+
t.Run(tc.name, func(t *testing.T) {
176+
for _, match := range tc.matches {
177+
if !tc.regex.MatchString(match) {
178+
t.Errorf(
179+
"expected %q to match %q",
180+
match,
181+
tc.regex.String(),
182+
)
183+
}
184+
}
185+
186+
for _, unmatch := range tc.unmatches {
187+
if tc.regex.MatchString(unmatch) {
188+
t.Errorf(
189+
"expected %q to not match %q",
190+
unmatch,
191+
tc.regex.String(),
192+
)
193+
}
194+
}
195+
})
196+
}
197+
}

0 commit comments

Comments
 (0)