Browse Source

Add test for special chars in section/key/value

Alois Mahdal 7 years ago
parent
commit
dea6cb099a
1 changed files with 198 additions and 0 deletions
  1. 198
    0
      tests/inigrep_chars/TF_RUN

+ 198
- 0
tests/inigrep_chars/TF_RUN View File

@@ -0,0 +1,198 @@
1
+#!/bin/bash
2
+
3
+. "$TF_DIR/include/subtest.sh"
4
+. "$TF_DIR/include/tools.sh"
5
+
6
+. "$(shellfu-get path)" || tf_exit_error "failed to init shellfu"
7
+export PRETTY=plain
8
+shellfu import inigrep  || tf_exit_error "failed to import inigrep"
9
+
10
+#shellcheck disable=SC2016
11
+specchar() {
12
+    #
13
+    # List all "special" chars and their names
14
+    #
15
+    local what=$1
16
+    {
17
+        echo ' space'
18
+        echo '!exclamation'
19
+        echo '"quote'
20
+        echo '#hash'
21
+        echo '$dollar'
22
+        echo '%percent'
23
+        echo '&ampersand'
24
+        echo "'apostrophe"
25
+        echo '(lbracket'
26
+        echo ')rbracket'
27
+        echo '*asterisk'
28
+        echo '+plus'
29
+        echo ',comma'
30
+        echo '-minus'
31
+        echo '.dot'
32
+        echo '/slash'
33
+        echo ':colon'
34
+        echo ';semicolon'
35
+        echo '<lt'
36
+        echo '=equal'
37
+        echo '>gt'
38
+        echo '?question'
39
+        echo '@at'
40
+        echo '[lsbracket'
41
+        echo '\backslash'
42
+        echo ']rsbracket'
43
+        echo '^caret'
44
+        echo '_underscore'
45
+        echo '`backtick'
46
+        echo '{lcbracket'
47
+        echo '|pipe'
48
+        echo '}rcbracket'
49
+        echo '~tilde'
50
+#       echo '☯yinyang'
51
+    } \
52
+      | case $what in
53
+            CHARS)  grep -o ^.       ;;
54
+            NAMES)  sed s/.//        ;;
55
+            "")     cat              ;;
56
+            *)      grep -x ".$what" \
57
+                      | grep -o ^.   ;;
58
+        esac
59
+}
60
+
61
+tf_enum_subtests() {
62
+    #
63
+    # Enumerate test names
64
+    #
65
+    {
66
+        specchar NAMES \
67
+          | sed s/^/k_/
68
+        specchar NAMES \
69
+          | sed s/^/s_/
70
+        specchar NAMES \
71
+          | sed s/^/v_/
72
+    } \
73
+      | grep -vxF k_dot
74
+}
75
+
76
+tf_do_subtest() {
77
+    local t_name=$1
78
+    local t_cmd
79
+    local char
80
+    local charname=${t_name:2}
81
+    local o_out="oracle/$t_name.stdout"
82
+    local o_err="oracle/$t_name.stderr"
83
+    local o_es=0
84
+    local t_in="test/$t_name.stdin"
85
+    char=$(specchar "$charname")
86
+    echo -n > "$o_out"
87
+    echo -n > "$o_err"
88
+
89
+    # Variables marked with "a" ant "z" are traps, what we want inigrep to
90
+    # catch is placed in $key_tt, $section_t or $value_tt
91
+    #
92
+    local section_a="section_a"
93
+    local section_t="section_t"
94
+    local section_z="section_z"
95
+    local key_aa="key_aa"
96
+    local key_az="key_az"
97
+    local key_ta="key_ta"
98
+    local key_tt="key_tt"
99
+    local key_tz="key_tz"
100
+    local key_za="key_za"
101
+    local key_zz="key_zz"
102
+    local value_aa="value_aa"
103
+    local value_az="value_az"
104
+    local value_ta="value_ta"
105
+    local value_tt="value_tt"
106
+    local value_tz="value_tz"
107
+    local value_za="value_za"
108
+    local value_zz="value_zz"
109
+
110
+    # test input
111
+    #
112
+    case $t_name in
113
+        v_*)
114
+            value_ta="anoter value with $charname ($char) inside"
115
+            value_tt="value with $charname ($char) inside"
116
+            value_tz="yet anoter value with $charname ($char) inside"
117
+            ;;
118
+        s_*)
119
+            section_a="another_section_with_${charname}${char}inside"
120
+            section_t="section_with_${charname}${char}inside"
121
+            section_z="yet_another_section_with_${charname}${char}inside"
122
+            value_tt="value from section with $charname inside"
123
+            ;;
124
+        k_*)
125
+            key_ta="another_key_with_${charname}${char}inside"
126
+            key_tt="key_with_${charname}${char}inside"
127
+            key_tz="yet_another_key_with_${charname}${char}inside"
128
+            value_tt="value for key with $charname inside"
129
+            ;;
130
+    esac
131
+    {
132
+        echo "#t_name:   $t_name"
133
+        echo "#charname: $charname"
134
+        echo "#char:     $char"
135
+        echo
136
+        echo "[$section_a]"
137
+        echo "    $key_aa = $value_aa"
138
+        echo "    $key_az = $value_az"
139
+        echo
140
+        echo "[$section_t]"
141
+        echo "    $key_ta = $value_ta"
142
+        echo "    $key_tt = $value_tt"  # aiming THIS; rest is traps
143
+        echo "    $key_tz = $value_tz"
144
+        echo
145
+        echo "[$section_z]"
146
+        echo "    $key_za = $value_za"
147
+        echo "    $key_zz = $value_zz"
148
+        echo
149
+    } > "$t_in"
150
+
151
+    # test command
152
+    #
153
+    case $t_name in
154
+        v_*)           t_cmd="inigrep section_t.key_tt"                               ;;
155
+        s_apostrophe)  t_cmd='inigrep "section_with_apostrophe'"'"'inside.key_tt"'    ;;
156
+        s_*)           t_cmd="inigrep 'section_with_${charname}${char}inside.key_tt'" ;;
157
+        k_apostrophe)  t_cmd='inigrep "section_t.key_with_apostrophe'"'"'inside"'     ;;
158
+        k_*)           t_cmd="inigrep 'section_t.key_with_${charname}${char}inside'"  ;;
159
+    esac
160
+
161
+    # oracle output
162
+    #
163
+    case $t_name in
164
+        v_*)    echo "value with $charname ($char) inside"      ;;
165
+        s_*)    echo "value from section with $charname inside" ;;
166
+        k_*)    echo "value for key with $charname inside"      ;;
167
+    esac > "$o_out"
168
+
169
+    case $t_name in
170
+        k_backslash)
171
+            o_es=2
172
+            echo -n '' > "$o_out"
173
+            echo "invalid char '\\' in key name: $key_tt" > "$o_err"
174
+            ;;
175
+        k_equal)
176
+            o_es=2
177
+            echo -n '' > "$o_out"
178
+            echo "invalid char '=' in key name: $key_tt" > "$o_err"
179
+            ;;
180
+        k_lsbracket)
181
+            o_es=2
182
+            echo -n '' > "$o_out"
183
+            echo "invalid char '[' in key name: $key_tt" > "$o_err"
184
+            ;;
185
+        s_rsbracket)
186
+            o_es=2
187
+            echo -n '' > "$o_out"
188
+            echo "invalid char ']' in section name: $section_t" > "$o_err"
189
+            ;;
190
+    esac
191
+
192
+    # pray and...
193
+    #
194
+    tf_testflt -n "$t_name" -i "$t_in" -O "$o_out" -E "$o_err" -S $o_es "$t_cmd"
195
+}
196
+
197
+mkdir -p test oracle result
198
+tf_do_subtests