]> git.cworth.org Git - lmno-server/blob - test
Add message string to the return value of add_move
[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_SUBSECTION "Test redirects"
144
145 TEST "Redirect of /GAMEID at top level"
146 redirect=$(curl_get $empires_game_id)
147 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
148 TEST_END
149
150 TEST "Redirect of lowercase /gameid at top level"
151 empires_game_id_lower=$(tr '[:upper:]' '[:lower:]' <<< $empires_game_id)
152 redirect=$(curl_get $empires_game_id_lower)
153 test "$redirect" = "Moved Permanently. Redirecting to /$empires_game_id/"
154 TEST_END
155
156 TEST "Redirect of lowercase /empires/gameid"
157 redirect=$(curl_get empires/$empires_game_id_lower)
158 test "$redirect" = "Moved Permanently. Redirecting to /empires/$empires_game_id/"
159 TEST_END
160
161 TEST_SECTION "Empires game"
162
163 empires_game_path=empires/$empires_game_id
164
165 TEST_SUBSECTION "Empires game /register"
166
167 empires_register()
168 {
169     curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
170 }
171
172 empires_players_string()
173 {
174     curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
175 }
176
177 empires_characters_string()
178 {
179     curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
180 }
181
182 TEST "Registering a player returns an ID"
183 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
184 test "$carl_id" = "1"
185 TEST_END
186
187 TEST "Registering several more players"
188 empires_register Richard "Bob Hope" > /dev/null
189 empires_register Kevin "Elvis Presley" > /dev/null
190 empires_register Stacy Phineas > /dev/null
191 empires_register David "Red Power Ranger" > /dev/null
192 empires_register Nancy "Audrey Hepburn" > /dev/null
193 bogus_id=$(empires_register Bogus "Mr. Bogus")
194 TEST_END
195
196 TEST 'Verify complete players list (with "Bogus")'
197 players=$(empires_players_string)
198 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
199 TEST_END
200
201 TEST 'Verify complete players list (with "Mr. Bogus")'
202 characters=$(empires_characters_string)
203 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,Mr. Bogus,"
204 TEST_END
205
206 TEST_SUBSECTION "Empires game /deregister"
207
208 empires_deregister()
209 {
210     curl_post $empires_game_path/deregister/$1
211 }
212
213 TEST "Removing the bogus player"
214 empires_deregister $bogus_id
215 TEST_END
216
217 TEST 'Verify modified players list (w/o "Bogus")"'
218 players=$(empires_players_string)
219 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
220 TEST_END
221
222 TEST 'Verify modified characters list (w/o "Mr. Bogus")'
223 characters=$(empires_characters_string)
224 test "$characters" = "Bugs Bunny,Bob Hope,Elvis Presley,Phineas,Red Power Ranger,Audrey Hepburn,"
225 TEST_END
226
227 TEST_SUBSECTION "Empires game /capture"
228
229 empires_capture()
230 {
231     curl_post $empires_game_path/capture/$1/$2
232 }
233
234 empires_empires_string()
235 {
236     # Get empires as a compact string (much more compact than JSON)
237     curl_get $empires_game_path/empires | jq -c '.[] | [.id,.captures]' | tr '\n' ','
238 }
239
240 TEST "Verify empires before any captures"
241 empires=$(empires_empires_string)
242 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
243 TEST_END
244
245 TEST "Perform some captures"
246 empires_capture 1 2
247 empires_capture 3 5
248 empires_capture 4 6
249 empires_capture 3 4
250 TEST_END
251
252 TEST "Verify empires after captures"
253 empires=$(empires_empires_string)
254 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
255 TEST_END
256
257 TEST_SUBSECTION "Empires game /liberate"
258
259 empires_liberate()
260 {
261     curl_post $empires_game_path/liberate/$1
262 }
263
264 TEST "Liberate a player"
265 empires_liberate 2
266 TEST_END
267
268 TEST "Verify empires after liberate"
269 empires=$(empires_empires_string)
270 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
271 TEST_END
272
273 TEST_SUBSECTION "Empires game /reset"
274
275 empires_reset()
276 {
277     curl_post $empires_game_path/reset
278 }
279
280 TEST "Reset the game"
281 empires_reset
282 TEST_END
283
284 TEST "Verify players is now empty"
285 players=$(empires_players_string)
286 test "$players" = ""
287 TEST_END
288
289 TEST_SECTION "Tic Tac Toe game"
290
291 tictactoe_game_path=tictactoe/$tictactoe_game_id
292
293 tictactoe_move()
294 {
295     curl_post $tictactoe_game_path/move "{ \"move\": $1 }"
296 }
297
298 TEST_SUBSECTION "Tic Tac Toe /move"
299
300 TEST "Move to the center square"
301 result=$(tictactoe_move 4)
302 test "$result" = '{"legal":true}'
303 TEST_END
304
305 TEST "Move to center square again is now illegal"
306 result=$(tictactoe_move 4)
307 test "$result" = '{"legal":false,"message":"Square is already occupied"}'
308 TEST_END
309
310 TEST_REPORT