]> git.cworth.org Git - lmno-server/blob - test
test: Add simple testing for tictactoe as well
[lmno-server] / test
1 #!/bin/bash
2
3 usage ()
4 {
5     echo "Usage:$0 <URL-to-test>"
6 }
7
8 if [ $# -lt 1 ]; then
9     echo "Error: No test URL given." >&2
10     echo "" >&2
11     usage >&2
12     exit 1
13 fi
14
15 URL=$1
16 CURL="curl --silent --show-error"
17
18 _TEST_SECTION()
19 {
20     echo ""
21     echo $1
22     echo $1 | sed -e "s/./$2/g"
23 }
24
25 TEST_SECTION()
26 {
27     _TEST_SECTION "$1" =
28 }
29
30 TEST_SUBSECTION()
31 {
32     _TEST_SECTION "$1" -
33 }
34
35 TEST()
36 {
37     printf "    $1"
38     printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
39     (( tests_total++ )) || true
40 }
41
42 # Result of test depends on the exit status of last command
43 TEST_END()
44 {
45     if [ $? -eq 0 ]; then
46         echo -n "OK"
47     else
48         (( tests_failed++ )) || true
49         echo -n "FAIL"
50     fi
51
52     # If we got an argument, append it after test result
53     if [ -n "$1" ]; then
54         echo " $1"
55     else
56         echo ""
57     fi
58 }
59
60 # Print report of all previous test results
61 TEST_REPORT()
62 {
63     echo ""
64     echo "Test Report"
65     echo "==========="
66
67     if [ "$tests_failed" == "" ]; then
68         echo "All $tests_total tests passed."
69         echo ""
70         return 0
71     else
72         echo "$tests_failed of $tests_total tests failed."
73         echo ""
74         return 1
75     fi
76 }
77
78 # Does a string contain a regular expression pattern
79 #
80 # Example:
81 #
82 #    contains "All's well that ends well" "s.well"
83 contains()
84 {
85     grep -q "$2" <<< $1
86 }
87
88 # Post to a URL endpoint with optional JSON data
89 #
90 # Usage:
91 #
92 # curl_post <ENDPOINT> [data]
93 curl_post()
94 {
95     $CURL -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
96 }
97
98 # Get form a URL endpoint
99 #
100 # Usage:
101 #
102 # curl_get <ENDPOINT>
103 curl_get()
104 {
105     $CURL $URL/$1
106 }
107
108 # Create a new game of the specified engine type
109 #
110 # Usage:
111 #
112 # new_game <ENGINE>
113 new_game()
114 {
115     curl_post new/$1 | jq -r .
116 }
117
118 TEST_SECTION "LMNO (super-site for games)"
119
120 TEST_SUBSECTION "Testing home page"
121 home_page=$($CURL $URL)
122
123 TEST "Contains 'Join Game'"
124 contains "$home_page" "Join Game"
125 TEST_END
126
127 TEST "Contains 'Host a new game'"
128 contains "$home_page" "Host a new game"
129 TEST_END
130
131 TEST_SUBSECTION "Creating some new games"
132
133 TEST "Empires"
134 empires_game_id=$(new_game empires)
135 test "$empires_game_id" != ""
136 TEST_END $empires_game_id
137
138 TEST "Tic Tac Toe"
139 tictactoe_game_id=$(new_game tictactoe)
140 test "$tictactoe_game_id" != ""
141 TEST_END $tictactoe_game_id
142
143 TEST_SECTION "Empires game"
144
145 empires_game_path=empires/$empires_game_id
146
147 TEST_SUBSECTION "Empires game /register"
148
149 empires_register()
150 {
151     curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
152 }
153
154 empires_players_string()
155 {
156     curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
157 }
158
159 empires_characters_string()
160 {
161     curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
162 }
163
164 TEST "Registering a player returns an ID"
165 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
166 test "$carl_id" = "1"
167 TEST_END
168
169 TEST "Registering several more players"
170 empires_register Richard "Bob Hope" > /dev/null
171 empires_register Kevin "Elvis Presley" > /dev/null
172 empires_register Stacy Phineas > /dev/null
173 empires_register David "Red Power Ranger" > /dev/null
174 empires_register Nancy "Audrey Hepburn" > /dev/null
175 bogus_id=$(empires_register Bogus "Mr. Bogus")
176 TEST_END
177
178 TEST 'Verify complete players list (with "Bogus")'
179 players=$(empires_players_string)
180 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
181 TEST_END
182
183 TEST 'Verify complete players list (with "Mr. Bogus")'
184 characters=$(empires_characters_string)
185 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
186 TEST_END
187
188 TEST_SUBSECTION "Empires game /deregister"
189
190 empires_deregister()
191 {
192     curl_post $empires_game_path/deregister/$1
193 }
194
195 TEST "Removing the bogus player"
196 empires_deregister $bogus_id
197 TEST_END
198
199 TEST 'Verify modified players list (w/o "Bogus")"'
200 players=$(empires_players_string)
201 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
202 TEST_END
203
204 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
205 characters=$(empires_characters_string)
206 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
207 TEST_END
208
209 TEST_SUBSECTION "Empires game /capture"
210
211 empires_capture()
212 {
213     curl_post $empires_game_path/capture/$1/$2
214 }
215
216 empires_empires_string()
217 {
218     # Get empires as a compact string (much more compact than JSON)
219     curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
220 }
221
222 TEST "Verify empires before any captures"
223 empires=$(empires_empires_string)
224 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
225 TEST_END
226
227 TEST "Perform some captures"
228 empires_capture 1 2
229 empires_capture 3 5
230 empires_capture 4 6
231 empires_capture 3 4
232 TEST_END
233
234 TEST "Verify empires after captures"
235 empires=$(empires_empires_string)
236 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
237 TEST_END
238
239 TEST_SUBSECTION "Empires game /liberate"
240
241 empires_liberate()
242 {
243     curl_post $empires_game_path/liberate/$1
244 }
245
246 TEST "Liberate a player"
247 empires_liberate 2
248 TEST_END
249
250 TEST "Verify empires after liberate"
251 empires=$(empires_empires_string)
252 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
253 TEST_END
254
255 TEST_SUBSECTION "Empires game /reset"
256
257 empires_reset()
258 {
259     curl_post $empires_game_path/reset
260 }
261
262 TEST "Reset the game"
263 empires_reset
264 TEST_END
265
266 TEST "Verify players is now empty"
267 players=$(empires_players_string)
268 test "$players" = ""
269 TEST_END
270
271 TEST_SECTION "Tic Tac Toe game"
272
273 tictactoe_game_path=tictactoe/$tictactoe_game_id
274
275 tictactoe_move()
276 {
277     curl_post $tictactoe_game_path/move "{\"square\": $1}"
278 }
279
280 TEST_SUBSECTION "Tic Tac Toe /move"
281
282 TEST "Move to the center square"
283 result=$(tictactoe_move 4)
284 test "$result" = "true"
285 TEST_END
286
287 TEST "Move to center square again is now illegal"
288 result=$(tictactoe_move 4)
289 test "$result" = "false"
290 TEST_END
291
292 TEST_REPORT
293