|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
| 3 | +export LC_ALL=en_US.UTF-8 |
| 4 | + |
3 | 5 | 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 |
7 | 9 | echo "----- expected -----
|
8 |
| -$5 |
| 10 | +$6 |
9 | 11 | ----- returned -----"
|
10 | 12 | echo "$RESULT"
|
11 | 13 | exit 1
|
|
14 | 16 |
|
15 | 17 | # Check BRE
|
16 | 18 | checkb(){
|
17 |
| - check_ "-e" "$@" |
| 19 | + check_ "-e" "BRE" "$@" |
18 | 20 | }
|
19 | 21 | # Check ERE
|
20 | 22 | checke(){
|
21 |
| - check_ "-E" "$@" |
| 23 | + check_ "-E" "ERE" "$@" |
22 | 24 | }
|
23 | 25 | # Check both
|
24 | 26 | check(){
|
25 | 27 | checkb "$@"
|
26 | 28 | checke "$@"
|
27 | 29 | }
|
28 | 30 |
|
| 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 | + |
29 | 52 | # Basics
|
30 | 53 |
|
31 | 54 | check 'Custom character class' \
|
32 | 55 | 'adn
|
33 | 56 | aqv' 's/[b-eq]/x/g' \
|
34 | 57 | 'axn
|
35 | 58 | 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