Skip to content

Commit 2d78238

Browse files
committed
Write sed tests up to captures and groups
1 parent eedaad9 commit 2d78238

File tree

1 file changed

+162
-6
lines changed

1 file changed

+162
-6
lines changed

tests/test_sed.sh

+162-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/bin/bash
22

3+
export LC_ALL=en_US.UTF-8
4+
35
check_(){
4-
RESULT="$(echo "$3" | sed "$1" "$4")"
5-
if [ "$RESULT" != "$5" ]; then
6-
echo "Test failed: $2" >&2
6+
RESULT="$(echo "$4" | sed "$1" "$5")"
7+
if [ "$RESULT" != "$6" ]; then
8+
echo "Test failed: $3 ($2)" >&2
79
echo "----- expected -----
8-
$5
10+
$6
911
----- returned -----"
1012
echo "$RESULT"
1113
exit 1
@@ -14,22 +16,176 @@ $5
1416

1517
# Check BRE
1618
checkb(){
17-
check_ "-e" "$@"
19+
check_ "-e" "BRE" "$@"
1820
}
1921
# Check ERE
2022
checke(){
21-
check_ "-E" "$@"
23+
check_ "-E" "ERE" "$@"
2224
}
2325
# Check both
2426
check(){
2527
checkb "$@"
2628
checke "$@"
2729
}
2830

31+
fail_(){
32+
if echo | sed "$1" "$4" &>/dev/null; then
33+
echo "Test didn't fail as expected: $3 ($2)" >&2
34+
exit 1
35+
fi
36+
}
37+
38+
# Fail check BRE
39+
failb(){
40+
fail_ "-e" "BRE" "$@"
41+
}
42+
# Fail check ERE
43+
faile(){
44+
fail_ "-E" "ERE" "$@"
45+
}
46+
# Fail check both
47+
fail(){
48+
failb "$@"
49+
faile "$@"
50+
}
51+
2952
# Basics
3053

3154
check 'Custom character class' \
3255
'adn
3356
aqv' 's/[b-eq]/x/g' \
3457
'axn
3558
axv'
59+
60+
check 'Negated custom character class' \
61+
'abcdefgh' \
62+
's/[^b-dg-l]/x/g' \
63+
'xbcdxxgh'
64+
65+
check 'Backslash not special in class' \
66+
"a]\\b
67+
a]\\b" \
68+
"1s/[\\m-p]/x/g
69+
2s/[]]/x/g" \
70+
"a]xb
71+
ax\\b"
72+
73+
check 'Ranges' \
74+
'a-e-i
75+
a-e-i' \
76+
'1s/[d-f]/x/g
77+
2s/[d-f-]/x/g' \
78+
'a-x-i
79+
axxxi'
80+
81+
checkb 'Alternation' \
82+
'acd' \
83+
's/b\|c/x/g' \
84+
'axd'
85+
checke 'Alternation' \
86+
'acd' \
87+
's/b|c/x/g' \
88+
'axd'
89+
90+
check 'Escaped character' \
91+
'abc' \
92+
"s/\\061\\x62\\x(99)/x/g" \
93+
'abc'
94+
95+
# Charater classes
96+
97+
check 'Any character' \
98+
'a ;d
99+
efg' \
100+
's/./x/g' \
101+
'xxxx
102+
xxx'
103+
104+
check 'Word character' \
105+
'hello w0r|_d!' \
106+
's/\w/x/g' \
107+
'xxxxx xxx|xx!'
108+
109+
fail 'Word class' \
110+
's/[[:word:]]/x/g'
111+
112+
check 'Upper case' \
113+
'Hell0 W0r|_D' \
114+
's/[[:upper:]]/X/g' \
115+
'Xell0 X0r|_X'
116+
117+
check 'Lower case' \
118+
'Hell0 W0r|_D' \
119+
's/[[:lower:]]/x/g' \
120+
'Hxxx0 W0x|_D'
121+
122+
check 'Whitespace' \
123+
'Hello world !' \
124+
's/\s/_/g' \
125+
'Hello_world_!'
126+
127+
check 'Whitespace' \
128+
'Hello world !' \
129+
's/[[:space:]]/_/g' \
130+
'Hello_world_!'
131+
132+
check 'Non-whitespace' \
133+
'Hello world !' \
134+
's/[^[:space:]]/x/g' \
135+
'xxxxx xxxxx x'
136+
137+
check 'Digit' \
138+
'H3ll0 W0r|_D' \
139+
's/[[:digit:]]/+/g' \
140+
'H+ll+ W+r|_D'
141+
142+
check 'Hexadecimal digit' \
143+
'H3ll0 W0r|_D' \
144+
's/[[:xdigit:]]/+/g' \
145+
'H+ll+ W+r|_+'
146+
147+
fail 'Octal digit' \
148+
's/[[:odigit:]]/x/g'
149+
150+
check 'Punctuation' \
151+
'+- 01: hello, world! ;) -+' \
152+
's/[[:punct:]]/_/g' \
153+
'__ 01_ hello_ world_ __ __'
154+
155+
check 'Alphabetical characters' \
156+
'+- 01: hello, world! ;) -+' \
157+
's/[[:alpha:]]/x/g' \
158+
'+- 01: xxxxx, xxxxx! ;) -+'
159+
160+
check 'Alphanumerical characters' \
161+
'+- 01: hello, world! ;) -+' \
162+
's/[[:alnum:]]/x/g' \
163+
'+- xx: xxxxx, xxxxx! ;) -+'
164+
165+
fail 'ASCII class' \
166+
's/[[:ascii:]]/x/g'
167+
168+
check 'Character equivalents' \
169+
'Rémi est prêt' \
170+
's/[[=e=]]/_/g' \
171+
'R_mi _st pr_t'
172+
173+
check 'Word boundary' \
174+
'Hello, world' \
175+
's/o\b/x/g' \
176+
'Hellx, world'
177+
178+
check 'Not word boundary' \
179+
'Hello, world' \
180+
's/o\B/x/g' \
181+
'Hello, wxrld'
182+
183+
check 'Begining of line' \
184+
'testing tests' \
185+
's/^t/r/g' \
186+
'resting tests'
187+
188+
check 'End of line' \
189+
'testing tests' \
190+
's/s$/x/g' \
191+
'testing testx'

0 commit comments

Comments
 (0)