This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.sh
executable file
·125 lines (93 loc) · 3.13 KB
/
tests.sh
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
#!/bin/sh
# Program that is to be tested
pikkuwiki() {
./pikkuwiki.sh "$@"
}
# Allow errors to collect test results
set +e
# This value will be set to non-zero if any of the tests fail
TESTS_SUCCESS=0
# Change this mid script to control indentation on test messages
ASSERT_INDENT=2
print_indent() {
printf "%${ASSERT_INDENT}s"
}
# Assertion based on last commands result code
assert() {
local result="${2:-$?}"
print_indent
if [ "$result" = 0 ]; then
printf "\e[1;32mSuccess:\e[0m %s\n" "$1"
else
TESTS_SUCCESS=1
printf "\e[0;31mFailure:\e[0m %s\n" "$1"
fi
}
testitem() {
ASSERT_INDENT=2
echo ""
echo "$1:"
}
list() {
printf "%s\n" "$@"
}
start_time=$(date)
export PIKKUWIKI_DIR="examplewiki"
export PW_DEFAULT_PAGE="index"
# The tests
tests() {
local expected_files
testitem "resolve"
[ "$(pikkuwiki resolve)" = "$PIKKUWIKI_DIR/$PW_DEFAULT_PAGE.txt" ]
assert "empty context & link"
[ "$(pikkuwiki resolve "$PIKKUWIKI_DIR/" "")" = "$PIKKUWIKI_DIR/$PW_DEFAULT_PAGE.txt" ]
assert "wiki dir as context"
[ "$(pikkuwiki resolve "foo/bar")" = "$PIKKUWIKI_DIR/foo/$PW_DEFAULT_PAGE.txt" ]
assert "empty link, sub directory #1"
[ "$(pikkuwiki resolve "foo/bar/")" = "$PIKKUWIKI_DIR/foo/bar/$PW_DEFAULT_PAGE.txt" ]
assert "empty link, sub directory #2"
[ "$(pikkuwiki resolve "foo" "bar")" = "$PIKKUWIKI_DIR/bar.txt" ]
assert "context at root level"
[ "$(pikkuwiki resolve "" "cool")" = "$PIKKUWIKI_DIR/cool.txt" ]
assert "no context"
[ "$(pikkuwiki resolve "foo/bar" "nice")" = "$PIKKUWIKI_DIR/foo/nice.txt" ]
assert "context at one level deep"
[ "$(pikkuwiki resolve "foo/bar/" "nice")" = "$PIKKUWIKI_DIR/foo/bar/nice.txt" ]
assert "directory context"
[ "$(pikkuwiki resolve "ignored/nope/" "/athome")" = "$PIKKUWIKI_DIR/athome.txt" ]
assert "ignored context"
[ "$(pikkuwiki resolve "$PIKKUWIKI_DIR/my/file" "other")" = "$PIKKUWIKI_DIR/my/other.txt" ]
assert "wiki directory in context"
testitem "find"
expected_files=$(list \
"$PIKKUWIKI_DIR/groceries.txt" \
"$PIKKUWIKI_DIR/index.txt" \
"$PIKKUWIKI_DIR/Project/codeproject1.txt" \
"$PIKKUWIKI_DIR/Project/codeproject2.txt" \
"$PIKKUWIKI_DIR/Project/list.txt" \
"$PIKKUWIKI_DIR/todo.txt" \
)
[ "$(pikkuwiki find)" = "$expected_files" ]
assert "find all"
expected_files=$(list \
"$PIKKUWIKI_DIR/Project/codeproject1.txt" \
"$PIKKUWIKI_DIR/Project/codeproject2.txt" \
)
[ "$(pikkuwiki find -p code)" = "$expected_files" ]
assert "find 'code'"
testitem "show"
expected_files=$(list \
"$PIKKUWIKI_DIR/Project/codeproject1.txt" \
"$PIKKUWIKI_DIR/Project/codeproject2.txt" \
)
[ "$(pikkuwiki show -l Project/list -p code)" = "$expected_files" ]
assert "show from Project/list with filter 'code'"
[ "$(pikkuwiki show -l Project/codeproject1)" = "$PIKKUWIKI_DIR/todo.txt" ]
assert "show from Project/codeproject1"
}
time tests
end_time=$(date)
echo ""
echo "Tests started : $start_time"
echo "Tests ended : $end_time"
exit $TESTS_SUCCESS