forked from tests-always-included/mo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostic
executable file
·393 lines (320 loc) · 12.7 KB
/
diagnostic
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env bash
#
# Test individual functions in mo and make sure they are performing their
# tasks correctly. This can be used to help determine what needs to get
# fixed when making mo work on another version of bash, or another shell
# entirely.
#
# Functions are tested from the most primitive to the most advanced.
# Errors, once found, halt the program. This way we can more easily
# diagnose what's not working and fix those low-level functions first.
PARENT_PID=$$
cd "$(dirname "$0")"
rm -f diagnostic.test
rm -f diagnostic.partial
# Load mo's functions
. ./mo
fail() {
echo "$1"
kill $PARENT_PID
exit 1
}
# No dependencies
echo -n "moIndirect ... "
(
a() {
local V
V="a"
b
echo -n "$V"
}
b() {
local V
V="b"
c V
echo -n "$V"
}
c() {
local "$1" && moIndirect "$1" c
}
[[ "$(a)" != "ca" ]] && fail "Did not assign or scope bled '$RESULT'"
)
echo "ok"
echo -n "moIndirectArray ... "
(
a() {
local V
V=( "a" )
b
echo -n "${#V[@]}"
}
b() {
local V
V=( "b" "b" )
c V
echo -n "${#V[@]}"
}
c() {
local "$1" && moIndirectArray "$1" c c c
}
[[ "$(a)" != "31" ]] && fail "Did not assign or scope bled '$RESULT'"
)
echo "ok"
echo -n "moIsArray ... "
(
TEST=1
moIsArray TEST && fail "Wrongly said number was an array"
)
(
TEST=()
moIsArray TEST || fail "Wrongly said array was not an array"
)
(
# shellcheck disable=SC2034
TEST=""
moIsArray TEST && fail "Wrongly said string was an array"
)
(
unset TEST
moIsArray TEST && fail "Wrongly said undefined was an array"
)
echo "ok"
echo -n "moIsFunction ... "
(
aa() { echo "hi"; }
moIsFunction aa || fail "Did not find a function"
moIsFunction dd && fail "Wrongly found a function"
)
echo "ok"
echo -n "moFindString ... "
(
moFindString POS "abcdefg" "ab"
[[ "$POS" == "0" ]] || fail "Did not find at beginning of string"
moFindString POS "abcdefg" "fg"
[[ "$POS" == "5" ]] || fail "Did not find at end of string"
moFindString POS "abcdefg" "de"
[[ "$POS" == "3" ]] || fail "Did not find at middle of string"
moFindString POS "abcdefg" "CD"
[[ "$POS" == "-1" ]] || fail "Did not correctly return a miss"
)
echo "ok"
echo -n "moFullTagName ... "
(
moFullTagName TAG "abc" "def"
[[ "$TAG" == "abc.def" ]] || fail "Did not work with a context"
moFullTagName TAG "" "one"
[[ "$TAG" == "one" ]] || fail "Did not work without a context"
)
echo "ok"
echo -n "moLoadFile ... "
(
# TODO - find a way to test reading from stdin that is not painful.
touch diagnostic.test
moLoadFile RESULT diagnostic.test
[[ "${#RESULT}" == "0" ]] || fail "Did not read from empty file '$RESULT'"
echo "abc" >> diagnostic.test
moLoadFile RESULT diagnostic.test
[[ "${#RESULT}" == "4" ]] || fail "Did not read from file '$RESULT'"
echo "" >> diagnostic.test
moLoadFile RESULT diagnostic.test
[[ "${#RESULT}" == "5" ]] || fail "Trimmed newline from file '$RESULT'"
rm diagnostic.test
)
echo "ok"
echo -n "moStandaloneDenied ... "
(
moStandaloneDenied RESULT before tag after > diagnostic.test
[[ "$(cat diagnostic.test)" == "before" ]] || fail "Did not output content before tag '$(cat diagnostic.test)'"
[[ "$RESULT" == "after" ]] || fail "Did not set the remaining content '$RESULT'"
rm diagnostic.test
)
echo "ok"
echo -n "moTest ... "
# shellcheck disable=SC2034
(
aa() { echo "hi"; }
bb="bb"
cc=3
dd=( dd )
xx=()
unset yy
zz=""
moTest aa || fail "Did not detect a function"
moTest bb || fail "Did not detect a non-empty string"
moTest cc || fail "Did not detect a number"
moTest dd || fail "Did not detect a populated array"
moTest xx && fail "Erroneously flagged an empty array"
moTest yy && fail "Erroneously flagged an undefined value"
moTest zz && fail "Erroneously flagged an empty string"
MO_FALSE_IF_EMPTY=true moTest zz && fail "Erroneously flagged an empty value as non-false when empty should be false"
)
echo "ok"
echo -n "moTrimChars ... "
(
moTrimChars RESULT "abcdabc" true true a b c
[[ "$RESULT" == "d" ]] || fail "Did not trim multiple characters '$RESULT'"
moTrimChars RESULT "abc" true false a c
[[ "$RESULT" == "bc" ]] || fail "Did not trim only from the front '$RESULT'"
moTrimChars RESULT "abc" false true a c
[[ "$RESULT" == "ab" ]] || fail "Did not trim only from the end '$RESULT'"
)
echo "ok"
echo -n "moGetContent ... "
(
# TODO - find a way to test reading from stdin that is not painful.
# Until then, mock it
moLoadFile() { local "$1" && moIndirect "$1" "STDIN"; }
moGetContent RESULT a
[[ "$RESULT" == "{{>a}}" ]] || fail "Did not construct 1 partial correctly '$RESULT'"
moGetContent RESULT a b c
[[ "$RESULT" == "{{>a}}{{>b}}{{>c}}" ]] || fail "Did not construct 3 partials correctly '$RESULT'"
moGetContent RESULT
[[ "$RESULT" == "STDIN" ]] || fail "Did not call moLoadFile correctly"
)
echo "ok"
echo -n "moIndentLines ... "
(
CR=$'\r'
LF=$'\n'
# shellcheck disable=SC2034
CRLF="$CR$LF"
# CAUTION
# This must have a dot at the end of the input string
# That is part of how moPartial calls this function
for NEWLINE in "CR" "LF" "CRLF"; do
NL="${!NEWLINE}"
moIndentLines RESULT "" "has${NL}${NEWLINE}${NL}."
printf -v QUOTED '%q' "$RESULT"
[[ "$RESULT" == "has${NL}${NEWLINE}${NL}" ]] || fail "Should not have changed string $QUOTED"
moIndentLines RESULT "" "without${NL}trailing${NL}${NEWLINE}."
printf -v QUOTED '%q' "$RESULT"
[[ "$RESULT" == "without${NL}trailing${NL}${NEWLINE}" ]] || fail "Should not have changed string $QUOTED"
moIndentLines RESULT "_-_" "has${NL}${NL}${NEWLINE}${NL}."
printf -v QUOTED '%q' "$RESULT"
[[ "$RESULT" == "_-_has${NL}${NL}_-_${NEWLINE}${NL}" ]] || fail "Should have indented $QUOTED"
moIndentLines RESULT "_-_" "without${NL}${NL}trailing${NL}${NEWLINE}."
printf -v QUOTED '%q' "$RESULT"
[[ "$RESULT" == "_-_without${NL}${NL}_-_trailing${NL}_-_${NEWLINE}" ]] || fail "Should have indented $QUOTED"
done
)
echo "ok"
echo -n "moIsStandalone ... "
(
CR=$'\r'
LF=$'\n'
TAB=$'\t'
moIsStandalone RESULT "" "" false && fail "Needs newline before or beginning of content flag"
moIsStandalone RESULT "" "" true || fail "Has beginning of content flag and no other content"
[[ "$RESULT" == "0 0" ]] || fail "Wrong returned value for no content '$RESULT'"
moIsStandalone RESULT "moo" "cow" false && fail "Needs newlines when there is content"
moIsStandalone RESULT "moo$CR$LF$TAB $TAB " " $TAB $TAB$CR${LF}pasture" false || fail "Has newlines and content but did not flag"
[[ "$RESULT" == "5 6" ]] || fail "Wrong returned value when there was whitespace '$RESULT'"
)
echo "ok"
echo -n "moSplit ... "
(
moSplit RESULT "abc-def-ghi" "-"
[[ "${#RESULT[@]}" == "2" ]] || fail "Returned wrong number of elements with one delimiter ${#RESULT[@]}"
[[ "${RESULT[0]}" == "abc" ]] || fail "Returned wrong left hand string with one delimiter '${RESULT[0]}'"
[[ "${RESULT[1]}" == "def-ghi" ]] || fail "Returned wrong right hand string with one delimiter '${RESULT[1]}'"
moSplit RESULT "abc-def-ghi" "-" "g"
[[ "${#RESULT[@]}" == "3" ]] || fail "Returned wrong number of elements with two delimiters ${#RESULT[@]}"
[[ "${RESULT[0]}" == "abc" ]] || fail "Returned wrong left hand string with two delimiters '${RESULT[0]}'"
[[ "${RESULT[1]}" == "def-" ]] || fail "Returned wrong middle string with two delimiters '${RESULT[1]}'"
[[ "${RESULT[2]}" == "hi" ]] || fail "Returned wrong right hand string with two delimiters '${RESULT[2]}'"
)
echo "ok"
echo -n "moTrimWhitespace ... "
(
CR=$'\r'
LF=$'\n'
TAB=$'\t'
moTrimWhitespace RESULT "ab cd"
printf -v QUOTED '%q' "$RESULT"
[[ "${RESULT}" == "ab cd" ]] || fail "Trimmed a string that did not need trimming $QUOTED"
moTrimWhitespace RESULT "$CR$LF$TAB ab $CR$LF$TAB cd $CR$LF$TAB $CR$LF$TAB"
printf -v QUOTED '%q' "$RESULT"
[[ "${RESULT}" == "ab $CR$LF$TAB cd" ]] || fail "Did not fully trim a string $QUOTED"
)
echo "ok"
echo -n "moStandaloneAllowed ... "
(
# Mock moIsStandalone to make things simpler
moIsStandalone() { return 1; }
moStandaloneAllowed RESULT before tag after > diagnostic.test
[[ "$(cat diagnostic.test)" == "before" ]] || fail "Did not output content before tag when not standalone '$(cat diagnostic.test)'"
[[ "$RESULT" == "after" ]] || fail "Did not set the remaining content when not standalone '$RESULT'"
moIsStandalone() { local "$1" && moIndirect "$1" "3 5"; return 0; }
moStandaloneAllowed RESULT before tag afterwards > diagnostic.test
[[ "$(cat diagnostic.test)" == "bef" ]] || fail "Did not output content before tag when standalone '$(cat diagnostic.test)'"
[[ "$RESULT" == "wards" ]] || fail "Did not set the remaining content when standalone '$RESULT'"
rm diagnostic.test
)
echo "ok"
echo -n "moFindEndTag ... "
(
LF=$'\n'
moFindEndTag RESULT "moo{{ / cow }}pasture" "cow"
[[ "${#RESULT[@]}" == "3" ]] || fail "(simple) Wrong number of elements in the array ${#RESULT[@]}"
[[ "${RESULT[0]}" == "moo" ]] || fail "(simple) Wrong left-hand '${RESULT[0]}'"
[[ "${RESULT[1]}" == "{{ / cow }}" ]] || fail "(simple) Wrong middle '${RESULT[1]}'"
[[ "${RESULT[2]}" == "pasture" ]] || fail "(simple) Wrong right-hand '${RESULT[2]}'"
moFindEndTag RESULT "moo$LF {{/cow}} $LF pasture" "cow"
[[ "${#RESULT[@]}" == "3" ]] || fail "(standalone) Wrong number of elements in the array ${#RESULT[@]}"
[[ "${RESULT[0]}" == "moo$LF" ]] || fail "(standalone) Wrong left-hand '${RESULT[0]}'"
[[ "${RESULT[1]}" == " {{/cow}} $LF" ]] || fail "(standalone) Wrong middle '${RESULT[1]}'"
[[ "${RESULT[2]}" == " pasture" ]] || fail "(standalone) Wrong right-hand '${RESULT[2]}'"
moFindEndTag RESULT "aa{{#bb}}cc{{/bb}}dd{{^bb}}ee{{/bb}}ff{{/bb}}gg" "bb"
[[ "${#RESULT[@]}" == "3" ]] || fail "(recursive) Wrong number of elements in the array ${#RESULT[@]}"
[[ "${RESULT[0]}" == "aa{{#bb}}cc{{/bb}}dd{{^bb}}ee{{/bb}}ff" ]] || fail "(recursive) Wrong left-hand '${RESULT[0]}'"
[[ "${RESULT[1]}" == "{{/bb}}" ]] || fail "(recursive) Wrong middle '${RESULT[1]}'"
[[ "${RESULT[2]}" == "gg" ]] || fail "(recursive) Wrong right-hand '${RESULT[2]}'"
moFindEndTag RESULT "aa{{#bb}}cc{{/dd}}ee" "dd"
[[ "${#RESULT[@]}" == "3" ]] || fail "(unbalanced) Wrong number of elements in the array ${#RESULT[@]}"
[[ "${RESULT[0]}" == "aa{{#bb}}cc{{/dd}}ee" ]] || fail "(unbalanced) Wrong left-hand '${RESULT[0]}'"
[[ "${RESULT[1]}" == "" ]] || fail "(unbalanced) Wrong middle '${RESULT[1]}'"
[[ "${RESULT[2]}" == "" ]] || fail "(unbalanced) Wrong right-hand '${RESULT[2]}'"
)
echo "ok"
echo -n "moLoop ... "
(
moParse() { echo "parse[$1] context[$2] flag[$3]"; }
moLoop "content" "prefix" a b c > diagnostic.test
(
echo "parse[content] context[prefix.a] flag[false]"
echo "parse[content] context[prefix.b] flag[false]"
echo "parse[content] context[prefix.c] flag[false]"
) | diff -q diagnostic.test - || fail "Result was not as expected '$(cat diagnostic.test)'"
rm diagnostic.test
)
echo "ok"
echo -n "moPartial ... "
(
NL=$'\n'
moParse() { echo "parse[$1] context[$2] flag[$3]"; }
echo "partial" > diagnostic.partial
moPartial RESULT "line$NL" ">diagnostic.partial" " ${NL}line2" false "moo" > diagnostic.test
[[ "$RESULT" == "line2" ]] || fail "Did not consume newline for standalone '$RESULT'"
printf -v QUOTED '%q' "$(cat diagnostic.test)"
[[ "$(cat diagnostic.test)" == "line${NL}parse[partial${NL}] context[moo] flag[true]" ]] || fail "Did not parse right standalone content $QUOTED"
moPartial RESULT "line" ">diagnostic.partial" "line2" false "moo" > diagnostic.test
[[ "$RESULT" == "line2" ]] || fail "Did not preserve content for non-standalone '$RESULT'"
printf -v QUOTED '%q' "$(cat diagnostic.test)"
[[ "$(cat diagnostic.test)" == "lineparse[partial${NL}] context[moo] flag[true]" ]] || fail "Did not parse right non-standalone content $QUOTED"
rm diagnostic.test diagnostic.partial
)
echo "ok"
echo -n "moShow ... "
# shellcheck disable=SC2034
(
aa() { echo "this is aa"; }
bb="BB"
cc=( zero one two )
[[ "$(moShow aa)" == "this is aa" ]] || fail "Did not load function"
[[ "$(moShow bb)" == "BB" ]] || fail "Did not show variable"
[[ "$(moShow cc.1)" == "one" ]] || fail "Did not show value from indexed array"
)
echo "ok"
echo -n "moParse ... skipped (tested by specs)"
echo ""
echo "All diagnostic tests pass"