5 echo "Usage:$0 <URL-to-test>"
9 echo "Error: No test URL given." >&2
16 CURL="curl --silent --show-error"
22 echo $1 | sed -e "s/./$2/g"
38 printf "%*s" $(( 52 - ${#1} )) | tr ' ' '.'
39 (( tests_total++ )) || true
42 # Result of test depends on the exit status of last command
48 (( tests_failed++ )) || true
52 # If we got an argument, append it after test result
60 # Print report of all previous test results
67 if [ "$tests_failed" == "" ]; then
68 echo "All $tests_total tests passed."
72 echo "$tests_failed of $tests_total tests failed."
78 # Does a string contain a regular expression pattern
82 # contains "All's well that ends well" "s.well"
88 # Post to a URL endpoint with optional JSON data
92 # curl_post <ENDPOINT> [data]
95 $CURL -X POST ${2:+-H 'Content-Type: application/json' -d "$2"} $URL/$1
98 # Get form a URL endpoint
102 # curl_get <ENDPOINT>
108 # Create a new game of the specified engine type
115 curl_post new/$1 | jq -r .
118 TEST_SECTION "LMNO (super-site for games)"
120 TEST_SUBSECTION "Testing home page"
121 home_page=$($CURL $URL)
123 TEST "Contains 'Join Game'"
124 contains "$home_page" "Join Game"
127 TEST "Contains 'Host a new game'"
128 contains "$home_page" "Host a new game"
131 TEST_SUBSECTION "Creating some new games"
134 empires_game_id=$(new_game empires)
135 test "$empires_game_id" != ""
136 TEST_END $empires_game_id
139 tictactoe_game_id=$(new_game tictactoe)
140 test "$tictactoe_game_id" != ""
141 TEST_END $tictactoe_game_id
143 TEST_SECTION "Empires game"
145 empires_game_path=empires/$empires_game_id
147 TEST_SUBSECTION "Empires game /register"
151 curl_post $empires_game_path/register "{\"name\": \"$1\", \"character\": \"$2\"}"
154 empires_players_string()
156 curl_get $empires_game_path/players | jq -r .[].name | tr '\n' ','
159 empires_characters_string()
161 curl_get $empires_game_path/characters | jq -r .[] | tr '\n' ','
164 TEST "Registering a player returns an ID"
165 carl_id=$(empires_register Carl "Bugs Bunny" | jq -r .)
166 test "$carl_id" = "1"
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")
178 TEST 'Verify complete players list (with "Bogus")'
179 players=$(empires_players_string)
180 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,Bogus,"
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,"
188 TEST_SUBSECTION "Empires game /deregister"
192 curl_post $empires_game_path/deregister/$1
195 TEST "Removing the bogus player"
196 empires_deregister $bogus_id
199 TEST 'Verify modified players list (w/o "Bogus")"'
200 players=$(empires_players_string)
201 test "$players" = "Carl,Richard,Kevin,Stacy,David,Nancy,"
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,"
209 TEST_SUBSECTION "Empires game /capture"
213 curl_post $empires_game_path/capture/$1/$2
216 empires_empires_string()
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' ','
222 TEST "Verify empires before any captures"
223 empires=$(empires_empires_string)
224 test "$empires" = "[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]],"
227 TEST "Perform some captures"
234 TEST "Verify empires after captures"
235 empires=$(empires_empires_string)
236 test "$empires" = "[1,[2]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
239 TEST_SUBSECTION "Empires game /liberate"
243 curl_post $empires_game_path/liberate/$1
246 TEST "Liberate a player"
250 TEST "Verify empires after liberate"
251 empires=$(empires_empires_string)
252 test "$empires" = "[1,[]],[2,[]],[3,[5,4]],[4,[6]],[5,[]],[6,[]],"
255 TEST_SUBSECTION "Empires game /reset"
259 curl_post $empires_game_path/reset
262 TEST "Reset the game"
266 TEST "Verify players is now empty"
267 players=$(empires_players_string)
271 TEST_SECTION "Tic Tac Toe game"
273 tictactoe_game_path=tictactoe/$tictactoe_game_id
277 curl_post $tictactoe_game_path/move "{\"square\": $1}"
280 TEST_SUBSECTION "Tic Tac Toe /move"
282 TEST "Move to the center square"
283 result=$(tictactoe_move 4)
284 test "$result" = "true"
287 TEST "Move to center square again is now illegal"
288 result=$(tictactoe_move 4)
289 test "$result" = "false"