Best Python code snippet using slash
gtp_state_tests.py
Source:gtp_state_tests.py
1"""Tests for gtp_state.py."""2from textwrap import dedent3from gomill import boards4from gomill import gtp_engine5from gomill import gtp_states6from gomill.common import format_vertex7from gomill_tests import test_framework8from gomill_tests import gomill_test_support9from gomill_tests import gtp_engine_test_support10from gomill_tests import gtp_state_test_support11def make_tests(suite):12 suite.addTests(gomill_test_support.make_simple_tests(globals()))13class Gtp_state_fixture(test_framework.Fixture):14 """Fixture for managing a Gtp_state.15 The move generator comes from gtp_state_test_support.Player16 Adds a type equality function for History_move.17 """18 def __init__(self, tc):19 self.tc = tc20 self.player = gtp_state_test_support.Player()21 self.gtp_state = gtp_state_test_support.Testing_gtp_state(22 move_generator=self.player.genmove,23 acceptable_sizes=(9, 11, 13, 19))24 self.engine = gtp_engine.Gtp_engine_protocol()25 self.engine.add_protocol_commands()26 self.engine.add_commands(self.gtp_state.get_handlers())27 self.tc.addTypeEqualityFunc(28 gtp_states.History_move, self.assertHistoryMoveEqual)29 def assertHistoryMoveEqual(self, hm1, hm2, msg=None):30 t1 = (hm1.colour, hm1.move, hm1.comments, hm1.cookie)31 t2 = (hm2.colour, hm2.move, hm2.comments, hm2.cookie)32 self.tc.assertEqual(t1, t2, "History_moves differ")33 def check_command(self, *args, **kwargs):34 """Check a single GTP command.35 parameters as for gtp_engine_test_support.check_engine()36 """37 gtp_engine_test_support.check_engine(38 self.tc, self.engine, *args, **kwargs)39 def check_board_empty_9(self):40 self.check_command('showboard', [], dedent("""41 9 . . . . . . . . .42 8 . . . . . . . . .43 7 . . . . . . . . .44 6 . . . . . . . . .45 5 . . . . . . . . .46 4 . . . . . . . . .47 3 . . . . . . . . .48 2 . . . . . . . . .49 1 . . . . . . . . .50 A B C D E F G H J"""))51def test_gtp_state(tc):52 fx = Gtp_state_fixture(tc)53 fx.check_command('nonsense', [''], "unknown command",54 expect_failure=True)55 fx.check_command('protocol_version', [''], "2")56 fx.player.set_next_move("A3", "preprogrammed move 0")57 fx.check_command('genmove', ['B'], "A3")58 game_state = fx.player.last_game_state59 # default board size is min(acceptable_sizes)60 tc.assertEqual(game_state.size, 9)61 b = boards.Board(9)62 b.play(2, 0, 'b')63 tc.assertEqual(game_state.board, b)64 tc.assertEqual(game_state.komi, 0.0)65 tc.assertEqual(game_state.history_base, boards.Board(9))66 tc.assertEqual(game_state.move_history, [])67 tc.assertIsNone(game_state.ko_point)68 tc.assertIsNone(game_state.handicap)69 tc.assertIs(game_state.for_regression, False)70 tc.assertIsNone(game_state.time_settings)71 tc.assertIsNone(game_state.time_remaining)72 tc.assertIsNone(game_state.canadian_stones_remaining)73 fx.check_command('gomill-explain_last_move', [], "preprogrammed move 0")74 fx.check_command('play', ['W', 'A4'], "")75 fx.check_command('komi', ['5.5'], "")76 fx.player.set_next_move("C9")77 fx.check_command('genmove', ['B'], "C9")78 game_state = fx.player.last_game_state79 tc.assertEqual(game_state.komi, 5.5)80 tc.assertEqual(game_state.history_base, boards.Board(9))81 tc.assertEqual(len(game_state.move_history), 2)82 tc.assertEqual(game_state.move_history[0],83 gtp_states.History_move('b', (2, 0), "preprogrammed move 0"))84 tc.assertEqual(game_state.move_history[1],85 gtp_states.History_move('w', (3, 0)))86 fx.check_command('genmove', ['B'], "pass")87 fx.check_command('gomill-explain_last_move', [], "")88 fx.check_command('genmove', ['W'], "pass")89 fx.check_command('showboard', [], dedent("""90 9 . . # . . . . . .91 8 . . . . . . . . .92 7 . . . . . . . . .93 6 . . . . . . . . .94 5 . . . . . . . . .95 4 o . . . . . . . .96 3 # . . . . . . . .97 2 . . . . . . . . .98 1 . . . . . . . . .99 A B C D E F G H J"""))100 fx.player.set_next_move_resign()101 fx.check_command('genmove', ['B'], "resign")102 fx.check_command('quit', [''], "", expect_end=True)103def test_clear_board_and_boardsize(tc):104 fx = Gtp_state_fixture(tc)105 fx.check_command('play', ['W', 'A4'], "")106 fx.check_command('boardsize', ['7'], "unacceptable size",107 expect_failure=True)108 fx.check_command('showboard', [], dedent("""109 9 . . . . . . . . .110 8 . . . . . . . . .111 7 . . . . . . . . .112 6 . . . . . . . . .113 5 . . . . . . . . .114 4 o . . . . . . . .115 3 . . . . . . . . .116 2 . . . . . . . . .117 1 . . . . . . . . .118 A B C D E F G H J"""))119 fx.check_command('clear_board', [], "")120 fx.check_board_empty_9()121 fx.check_command('play', ['W', 'A4'], "")122 fx.check_command('boardsize', ['11'], "")123 fx.check_command('showboard', [], dedent("""124 11 . . . . . . . . . . .125 10 . . . . . . . . . . .126 9 . . . . . . . . . . .127 8 . . . . . . . . . . .128 7 . . . . . . . . . . .129 6 . . . . . . . . . . .130 5 . . . . . . . . . . .131 4 . . . . . . . . . . .132 3 . . . . . . . . . . .133 2 . . . . . . . . . . .134 1 . . . . . . . . . . .135 A B C D E F G H J K L"""))136def test_play(tc):137 fx = Gtp_state_fixture(tc)138 fx.check_command('play', ['B', "E5"], "")139 fx.check_command('play', ['w', "e4"], "")140 fx.check_command('play', [], "invalid arguments", expect_failure=True)141 fx.check_command('play', ['B'], "invalid arguments", expect_failure=True)142 # additional arguments are ignored (following gnugo)143 fx.check_command('play', ['B', "F4", "E5"], "")144 fx.check_command('play', ['white', "f5"], "")145 fx.check_command('play', ['W', "K3"], "vertex is off board: 'k3'",146 expect_failure=True)147 fx.check_command('play', ['X', "A4"], "invalid colour: 'X'",148 expect_failure=True)149 fx.check_command('play', ['BLACK', "e4"], "illegal move",150 expect_failure=True)151 fx.check_command('play', ['B', "pass"], "")152 fx.check_command('play', ['W', "PASS"], "")153 fx.check_command('showboard', [], dedent("""154 9 . . . . . . . . .155 8 . . . . . . . . .156 7 . . . . . . . . .157 6 . . . . . . . . .158 5 . . . . # o . . .159 4 . . . . o # . . .160 3 . . . . . . . . .161 2 . . . . . . . . .162 1 . . . . . . . . .163 A B C D E F G H J"""))164def test_komi(tc):165 fx = Gtp_state_fixture(tc)166 fx.check_command('genmove', ['B'], "pass")167 tc.assertEqual(fx.player.last_game_state.komi, 0.0)168 fx.check_command('komi', ['1'], "")169 fx.check_command('genmove', ['B'], "pass")170 tc.assertEqual(fx.player.last_game_state.komi, 1.0)171 fx.check_command('komi', ['1.0'], "")172 fx.check_command('genmove', ['B'], "pass")173 tc.assertEqual(fx.player.last_game_state.komi, 1.0)174 fx.check_command('komi', ['7.5'], "")175 fx.check_command('genmove', ['B'], "pass")176 tc.assertEqual(fx.player.last_game_state.komi, 7.5)177 fx.check_command('komi', ['-3.5'], "")178 fx.check_command('genmove', ['B'], "pass")179 tc.assertEqual(fx.player.last_game_state.komi, -3.5)180 fx.check_command('komi', ['20000'], "")181 fx.check_command('genmove', ['B'], "pass")182 tc.assertEqual(fx.player.last_game_state.komi, 625.0)183 fx.check_command('komi', ['-20000'], "")184 fx.check_command('genmove', ['B'], "pass")185 tc.assertEqual(fx.player.last_game_state.komi, -625.0)186 fx.check_command('komi', ['nonsense'], "invalid float: 'nonsense'",187 expect_failure=True)188 fx.check_command('komi', ['NaN'], "invalid float: 'NaN'",189 expect_failure=True)190 fx.check_command('komi', ['inf'], "invalid float: 'inf'",191 expect_failure=True)192 fx.check_command('komi', ['-1e400'], "invalid float: '-1e400'",193 expect_failure=True)194def test_undo(tc):195 fx = Gtp_state_fixture(tc)196 fx.player.set_next_move("A3", "preprogrammed move A3")197 fx.check_command('genmove', ['B'], "A3")198 fx.check_command('gomill-explain_last_move', [], "preprogrammed move A3")199 fx.check_command('play', ['W', 'A4'], "")200 fx.check_command('showboard', [], dedent("""201 9 . . . . . . . . .202 8 . . . . . . . . .203 7 . . . . . . . . .204 6 . . . . . . . . .205 5 . . . . . . . . .206 4 o . . . . . . . .207 3 # . . . . . . . .208 2 . . . . . . . . .209 1 . . . . . . . . .210 A B C D E F G H J"""))211 fx.check_command('undo', [], "")212 fx.check_command('showboard', [], dedent("""213 9 . . . . . . . . .214 8 . . . . . . . . .215 7 . . . . . . . . .216 6 . . . . . . . . .217 5 . . . . . . . . .218 4 . . . . . . . . .219 3 # . . . . . . . .220 2 . . . . . . . . .221 1 . . . . . . . . .222 A B C D E F G H J"""))223 fx.player.set_next_move("D4", "preprogrammed move D4")224 fx.check_command('genmove', ['W'], "D4")225 fx.check_command('showboard', [], dedent("""226 9 . . . . . . . . .227 8 . . . . . . . . .228 7 . . . . . . . . .229 6 . . . . . . . . .230 5 . . . . . . . . .231 4 . . . o . . . . .232 3 # . . . . . . . .233 2 . . . . . . . . .234 1 . . . . . . . . .235 A B C D E F G H J"""))236 fx.check_command('gomill-explain_last_move', [], "preprogrammed move D4")237 fx.check_command('undo', [], "")238 fx.check_command('showboard', [], dedent("""239 9 . . . . . . . . .240 8 . . . . . . . . .241 7 . . . . . . . . .242 6 . . . . . . . . .243 5 . . . . . . . . .244 4 . . . . . . . . .245 3 # . . . . . . . .246 2 . . . . . . . . .247 1 . . . . . . . . .248 A B C D E F G H J"""))249 fx.check_command('gomill-explain_last_move', [], "preprogrammed move A3")250 fx.check_command('undo', [], "")251 fx.check_board_empty_9()252 fx.check_command('gomill-explain_last_move', [], "")253 fx.check_command('undo', [], "cannot undo", expect_failure=True)254def test_fixed_handicap(tc):255 fx = Gtp_state_fixture(tc)256 fx.check_command('fixed_handicap', ['3'], "C3 G7 C7")257 fx.check_command('showboard', [], dedent("""258 9 . . . . . . . . .259 8 . . . . . . . . .260 7 . . # . . . # . .261 6 . . . . . . . . .262 5 . . . . . . . . .263 4 . . . . . . . . .264 3 . . # . . . . . .265 2 . . . . . . . . .266 1 . . . . . . . . .267 A B C D E F G H J"""))268 fx.check_command('genmove', ['B'], "pass")269 tc.assertEqual(fx.player.last_game_state.handicap, 3)270 fx.check_command('boardsize', ['19'], "")271 fx.check_command('fixed_handicap', ['7'], "D4 Q16 D16 Q4 D10 Q10 K10")272 fx.check_command('fixed_handicap', ['7'], "board not empty",273 expect_failure=True)274 fx.check_command('boardsize', ['9'], "")275 fx.check_command('play', ['B', 'B2'], "")276 fx.check_command('fixed_handicap', ['2'], "board not empty",277 expect_failure=True)278 fx.check_command('clear_board', [], "")279 fx.check_command('fixed_handicap', ['0'], "invalid number of stones",280 expect_failure=True)281 fx.check_command('fixed_handicap', ['1'], "invalid number of stones",282 expect_failure=True)283 fx.check_command('fixed_handicap', ['10'], "invalid number of stones",284 expect_failure=True)285 fx.check_command('fixed_handicap', ['2.5'], "invalid int: '2.5'",286 expect_failure=True)287 fx.check_command('fixed_handicap', [], "invalid arguments",288 expect_failure=True)289def test_place_free_handicap(tc):290 # See gtp_state_test_support.Testing_gtp_state for description of the choice291 # of points.292 fx = Gtp_state_fixture(tc)293 fx.check_command('place_free_handicap', ['3'], "C3 G7 C7")294 fx.check_command('showboard', [], dedent("""295 9 . . . . . . . . .296 8 . . . . . . . . .297 7 . . # . . . # . .298 6 . . . . . . . . .299 5 . . . . . . . . .300 4 . . . . . . . . .301 3 . . # . . . . . .302 2 . . . . . . . . .303 1 . . . . . . . . .304 A B C D E F G H J"""))305 fx.check_command('genmove', ['B'], "pass")306 tc.assertEqual(fx.player.last_game_state.handicap, 3)307 fx.check_command('boardsize', ['19'], "")308 fx.check_command('place_free_handicap', ['7'], "D4 Q16 D16 Q4 D10 Q10 K10")309 fx.check_command('place_free_handicap', ['7'], "board not empty",310 expect_failure=True)311 fx.check_command('boardsize', ['9'], "")312 fx.check_command('play', ['B', 'B2'], "")313 fx.check_command('place_free_handicap', ['2'], "board not empty",314 expect_failure=True)315 fx.check_command('clear_board', [], "")316 fx.check_command('place_free_handicap', ['0'], "invalid number of stones",317 expect_failure=True)318 fx.check_command('place_free_handicap', ['1'], "invalid number of stones",319 expect_failure=True)320 fx.check_command('place_free_handicap', ['2.5'], "invalid int: '2.5'",321 expect_failure=True)322 fx.check_command('place_free_handicap', [], "invalid arguments",323 expect_failure=True)324 fx.check_command('place_free_handicap', ['10'],325 "C3 G7 C7 G3 C5 G5 E3 E7 E5")326 fx.check_command('clear_board', [''], "")327 fx.check_command('place_free_handicap', ['5'],328 "A1 A2 A3 A4 A5")329 fx.check_command('showboard', [], dedent("""330 9 . . . . . . . . .331 8 . . . . . . . . .332 7 . . . . . . . . .333 6 . . . . . . . . .334 5 # . . . . . . . .335 4 # . . . . . . . .336 3 # . . . . . . . .337 2 # . . . . . . . .338 1 # . . . . . . . .339 A B C D E F G H J"""))340 fx.check_command('clear_board', [''], "")341 fx.check_command('place_free_handicap', ['6'],342 "invalid result from move generator: A1,A2,A3,A4,A5,A1",343 expect_failure=True)344 fx.check_board_empty_9()345 fx.check_command('place_free_handicap', ['2'],346 "invalid result from move generator: A1,A2,A3",347 expect_failure=True)348 fx.check_board_empty_9()349 fx.check_command('place_free_handicap', ['4'],350 "invalid result from move generator: A1,A2,A3,pass",351 expect_failure=True)352 fx.check_board_empty_9()353 fx.check_command('place_free_handicap', ['8'],354 "ValueError: need more than 1 value to unpack",355 expect_internal_error=True)356 fx.check_board_empty_9()357def test_set_free_handicap(tc):358 fx = Gtp_state_fixture(tc)359 fx.check_command('set_free_handicap', ["C3", "E5", "C7"], "")360 fx.check_command('showboard', [], dedent("""361 9 . . . . . . . . .362 8 . . . . . . . . .363 7 . . # . . . . . .364 6 . . . . . . . . .365 5 . . . . # . . . .366 4 . . . . . . . . .367 3 . . # . . . . . .368 2 . . . . . . . . .369 1 . . . . . . . . .370 A B C D E F G H J"""))371 fx.check_command('genmove', ['B'], "pass")372 tc.assertEqual(fx.player.last_game_state.handicap, 3)373 fx.check_command('boardsize', ['9'], "")374 fx.check_command('play', ['B', 'B2'], "")375 fx.check_command('set_free_handicap', ["C3", "E5"], "board not empty",376 expect_failure=True)377 fx.check_command('clear_board', [], "")378 fx.check_command('set_free_handicap', ["C3"], "invalid number of stones",379 expect_failure=True)380 fx.check_command('set_free_handicap', [], "invalid number of stones",381 expect_failure=True)382 all_points = [format_vertex((i, j)) for i in range(9) for j in range(9)]383 fx.check_command('set_free_handicap', all_points,384 "invalid number of stones", expect_failure=True)385 fx.check_command('set_free_handicap', ["C3", "asdasd"],386 "invalid vertex: 'asdasd'", expect_failure=True)387 fx.check_board_empty_9()388 fx.check_command('set_free_handicap', ["C3", "pass"],389 "'pass' not permitted", expect_failure=True)390 fx.check_board_empty_9()391 fx.check_command('set_free_handicap', ["C3", "E5", "C3"],392 "engine error: C3 is occupied", expect_failure=True)393 fx.check_board_empty_9()394def test_loadsgf(tc):395 fx = Gtp_state_fixture(tc)396 fx.gtp_state._register_file("invalid.sgf", "non-SGF data")397 fx.gtp_state._register_file(398 "test1.sgf",399 "(;SZ[9];B[ee];W[eg];B[dg];W[dh];B[df];W[fh];B[];W[])")400 fx.gtp_state._register_file(401 "test2.sgf",402 "(;SZ[9]AB[fe:ff]AW[gf:gg]PL[W];W[eh];B[ge])")403 fx.check_command('loadsgf', ["unknown.sgf"],404 "cannot load file", expect_failure=True)405 fx.check_command('loadsgf', ["invalid.sgf"],406 "cannot load file", expect_failure=True)407 fx.check_command('loadsgf', ["test1.sgf"], "")408 fx.check_command('showboard', [], dedent("""409 9 . . . . . . . . .410 8 . . . . . . . . .411 7 . . . . . . . . .412 6 . . . . . . . . .413 5 . . . . # . . . .414 4 . . . # . . . . .415 3 . . . # o . . . .416 2 . . . o . o . . .417 1 . . . . . . . . .418 A B C D E F G H J"""))419 fx.check_command('loadsgf', ["test1.sgf", "4"], "")420 # position _before_ move 4421 fx.check_command('showboard', [], dedent("""422 9 . . . . . . . . .423 8 . . . . . . . . .424 7 . . . . . . . . .425 6 . . . . . . . . .426 5 . . . . # . . . .427 4 . . . . . . . . .428 3 . . . # o . . . .429 2 . . . . . . . . .430 1 . . . . . . . . .431 A B C D E F G H J"""))432 fx.check_command('undo', [], "")433 fx.check_command('showboard', [], dedent("""434 9 . . . . . . . . .435 8 . . . . . . . . .436 7 . . . . . . . . .437 6 . . . . . . . . .438 5 . . . . # . . . .439 4 . . . . . . . . .440 3 . . . . o . . . .441 2 . . . . . . . . .442 1 . . . . . . . . .443 A B C D E F G H J"""))444 fx.check_command('loadsgf', ["test2.sgf"], "")445 fx.check_command('showboard', [], dedent("""446 9 . . . . . . . . .447 8 . . . . . . . . .448 7 . . . . . . . . .449 6 . . . . . . . . .450 5 . . . . . # # . .451 4 . . . . . # o . .452 3 . . . . . . o . .453 2 . . . . o . . . .454 1 . . . . . . . . .455 A B C D E F G H J"""))456 fx.check_command('undo', [], "")457 fx.check_command('undo', [], "")458 fx.check_command('undo', [], "cannot undo", expect_failure=True)459 fx.check_command('showboard', [], dedent("""460 9 . . . . . . . . .461 8 . . . . . . . . .462 7 . . . . . . . . .463 6 . . . . . . . . .464 5 . . . . . # . . .465 4 . . . . . # o . .466 3 . . . . . . o . .467 2 . . . . . . . . .468 1 . . . . . . . . .469 A B C D E F G H J"""))470def test_savesgf(tc):471 scrub_sgf = gomill_test_support.scrub_sgf472 fx = Gtp_state_fixture(tc)473 fx.check_command("play", ['B', 'D4'], "")474 fx.player.set_next_move("C3", "preprogrammed move C3")475 fx.check_command('genmove', ['W'], "C3")476 fx.check_command('gomill-savesgf', ['out1.sgf'], "")477 tc.assertEqual(478 scrub_sgf(fx.gtp_state._load_file('out1.sgf')).replace("\n", ""),479 "(;FF[4]AP[gomill:VER]CA[UTF-8]DT[***]GM[1]KM[0]"480 "SZ[9];B[df];C[preprogrammed move C3]W[cg])")481 fx.check_command(482 'gomill-savesgf',483 ['out2.sgf', "PB=testplayer", "PW=GNU\_Go:3.8", "RE=W+3.5"],484 "")485 tc.assertEqual(486 scrub_sgf(fx.gtp_state._load_file('out2.sgf')).replace("\n", ""),487 "(;FF[4]AP[gomill:VER]CA[UTF-8]DT[***]GM[1]KM[0]"488 "PB[testplayer]PW[GNU Go:3.8]RE[W+3.5]SZ[9];B[df]"489 ";C[preprogrammed move C3]W[cg])")490 fx.check_command("boardsize", ['19'], "")491 fx.check_command("fixed_handicap", ['3'], "D4 Q16 D16")492 fx.check_command("komi", ['5.5'], "")493 fx.check_command("play", ['W', 'A2'], "")494 fx.check_command('gomill-savesgf', ['out3.sgf'], "")495 tc.assertEqual(496 scrub_sgf(fx.gtp_state._load_file('out3.sgf')).replace("\n", ""),497 "(;FF[4]AB[dd][dp][pd]AP[gomill:VER]CA[UTF-8]DT[***]"498 "GM[1]HA[3]KM[5.5]SZ[19];W[ar])")499 fx.check_command(500 'gomill-savesgf', ['force_fail'],501 "error writing file: [Errno 2] "502 "No such file or directory: '/nonexistent_directory/foo.sgf'",503 expect_failure=True)504def test_get_last_move(tc):505 fx = Gtp_state_fixture(tc)506 fx.player.set_next_move("A3", "preprogrammed move A3")507 fx.check_command('genmove', ['B'], "A3")508 history_moves = fx.player.last_game_state.move_history509 tc.assertEqual(gtp_states.get_last_move(history_moves, 'b'), (False, None))510 tc.assertEqual(gtp_states.get_last_move(history_moves, 'w'), (False, None))511 fx.player.set_next_move("B3", "preprogrammed move B3")512 fx.check_command('genmove', ['W'], "B3")513 history_moves = fx.player.last_game_state.move_history514 tc.assertEqual(gtp_states.get_last_move(history_moves, 'b'), (False, None))515 move_is_available, move = gtp_states.get_last_move(history_moves, 'w')516 tc.assertIs(move_is_available, True)517 tc.assertEqual(format_vertex(move), "A3")518 fx.check_command('genmove', ['B'], "pass")519 history_moves = fx.player.last_game_state.move_history520 move_is_available, move = gtp_states.get_last_move(history_moves, 'b')521 tc.assertIs(move_is_available, True)522 tc.assertEqual(format_vertex(move), "B3")523 tc.assertEqual(gtp_states.get_last_move(history_moves, 'w'), (False, None))524def test_get_last_move_and_cookie(tc):525 fx = Gtp_state_fixture(tc)526 fx.player.set_next_move("A3", "preprogrammed move A3", "COOKIE 1")527 fx.check_command('genmove', ['B'], "A3")528 history_moves = fx.player.last_game_state.move_history529 tc.assertEqual(gtp_states.get_last_move_and_cookie(history_moves, 'b'),530 (False, None, None))531 tc.assertEqual(gtp_states.get_last_move_and_cookie(history_moves, 'w'),532 (False, None, None))533 fx.check_command('play', ['W', 'B3'], "")534 fx.check_command('genmove', ['B'], "pass")535 history_moves = fx.player.last_game_state.move_history536 move_is_available, move, cookie = gtp_states.get_last_move_and_cookie(537 history_moves, 'b')538 tc.assertIs(move_is_available, True)539 tc.assertEqual(format_vertex(move), "B3")540 tc.assertIs(cookie, "COOKIE 1")541 tc.assertEqual(gtp_states.get_last_move_and_cookie(history_moves, 'w'),...
test_json.py
Source:test_json.py
1"""2This file covers testing validity of JSON schemata, plus the fields in the3schemta themselves.4"""5import sys, os6from pprint import pprint7import jsonschema8from jsonschema.exceptions import SchemaError, ValidationError9from unittest import TestCase, main10from athlib.utils import _rootdir, localpath, schema_valid, valid_against_schema11class JsonSchemaValidityTests(TestCase):12 def test_metaschema_valid(self):13 self.assertTrue(schema_valid("json/metaschema.json"))14 self.assertTrue(schema_valid("json/metaschema.json",15 validator=jsonschema.Draft4Validator))16 def test_athlete_schema_valid(self):17 # Required properties not valid in V318 with self.assertRaises(SchemaError):19 schema_valid("json/athlete.json", expect_failure=True)20 self.assertTrue(schema_valid("json/athlete.json",21 validator=jsonschema.Draft4Validator))22 def test_combined_performance_schema_valid(self):23 # Required properties not valid in V324 with self.assertRaises(SchemaError):25 schema_valid("json/combined_performance.json",26 expect_failure=True)27 self.assertTrue(schema_valid("json/combined_performance.json",28 validator=jsonschema.Draft4Validator))29 def test_competition_schema_valid(self):30 # Required properties not valid in V331 with self.assertRaises(SchemaError):32 schema_valid("json/competition.json", expect_failure=True)33 self.assertTrue(schema_valid("json/competition.json",34 validator=jsonschema.Draft4Validator))35 def test_event_schema_valid(self):36 # Required properties not valid in V337 with self.assertRaises(SchemaError):38 schema_valid("json/event.json", expect_failure=True)39 self.assertTrue(schema_valid("json/event.json",40 validator=jsonschema.Draft4Validator))41 def test_performance_schema_valid(self):42 self.assertTrue(schema_valid("json/performance.json"))43 self.assertTrue(schema_valid("json/performance.json",44 validator=jsonschema.Draft4Validator))45 def test_race_schema_valid(self):46 self.assertTrue(schema_valid("json/race.json"))47 self.assertTrue(schema_valid("json/race.json",48 validator=jsonschema.Draft4Validator))49 def test_athlete_valid_against_schema(self):50 self.assertTrue(valid_against_schema(51 "sample-jsons/athlete.json",52 "json/athlete.json")53 )54 self.assertTrue(valid_against_schema(55 "sample-jsons/athlete_minimal.json",56 "json/athlete.json")57 )58 def test_athlete_invalid_against_schema(self):59 with self.assertRaises(ValidationError):60 valid_against_schema("sample-jsons/athlete_invalid.json",61 "json/athlete.json",62 expect_failure=True)63 def test_combined_performance_valid_against_schema(self):64 self.assertTrue(valid_against_schema(65 "sample-jsons/combined_performance.json",66 "json/combined_performance.json")67 )68 self.assertTrue(valid_against_schema(69 "sample-jsons/combined_performance_minimal.json",70 "json/combined_performance.json")71 )72 def test_combined_performance_invalid_against_schema(self):73 with self.assertRaises(ValidationError):74 valid_against_schema(75 "sample-jsons/combined_performance_invalid.json",76 "json/combined_performance.json",77 expect_failure=True78 )79 with self.assertRaises(ValidationError):80 valid_against_schema(81 "sample-jsons/combined_performance_invalid2.json",82 "json/combined_performance.json",83 expect_failure=True84 )85 def test_competition_valid_against_schema(self):86 self.assertTrue(valid_against_schema(87 "sample-jsons/competition.json",88 "json/competition.json")89 )90 self.assertTrue(valid_against_schema(91 "sample-jsons/competition_minimal.json",92 "json/competition.json")93 )94 def test_competition_invalid_against_schema(self):95 with self.assertRaises(ValidationError):96 valid_against_schema("sample-jsons/competition_invalid.json",97 "json/competition.json",98 expect_failure=True)99 def test_event_valid_against_schema(self):100 self.assertTrue(valid_against_schema("sample-jsons/event.json",101 "json/event.json"))102 self.assertTrue(valid_against_schema("sample-jsons/event_minimal.json",103 "json/event.json"))104 def test_event_invalid_against_schema(self):105 with self.assertRaises(ValidationError):106 valid_against_schema("sample-jsons/event_invalid.json",107 "json/event.json",108 expect_failure=True)109 def test_performance_valid_against_schema(self):110 self.assertTrue(valid_against_schema("sample-jsons/performance.json",111 "json/performance.json"))112 self.assertTrue(valid_against_schema(113 "sample-jsons/performance_minimal.json",114 "json/performance.json")115 )116 def test_performance_invalid_against_schema(self):117 with self.assertRaises(ValidationError):118 valid_against_schema("sample-jsons/performance_invalid.json",119 "json/performance.json",120 expect_failure=True)121 with self.assertRaises(ValidationError):122 valid_against_schema("sample-jsons/performance_invalid2.json",123 "json/performance.json",124 expect_failure=True)125 def test_race_valid_against_schema(self):126 self.assertTrue(valid_against_schema(127 "sample-jsons/race_iffleymiles_2016_600mA.json",128 "json/race.json"))129 self.assertTrue(valid_against_schema(130 "sample-jsons/race_iffleymiles_2016_600mB.json",131 "json/race.json"))132 self.assertTrue(valid_against_schema(133 "sample-jsons/race_iffleymiles_2016_mileA.json",134 "json/race.json"))135 self.assertTrue(valid_against_schema(136 "sample-jsons/race_iffleymiles_2016_mileB.json",137 "json/race.json"))138 def test_race_invalid_against_schema(self):139 with self.assertRaises(ValidationError):140 valid_against_schema(141 "sample-jsons/race_invalid_athlete_internal.json",142 "json/race.json",143 expect_failure=True)144 with self.assertRaises(ValidationError):145 valid_against_schema(146 "sample-jsons/race_invalid_athlete_external.json",147 "json/race.json",148 expect_failure=True)149 with self.assertRaises(ValidationError):150 valid_against_schema("sample-jsons/race_invalid_position.json",151 "json/race.json",152 expect_failure=True)153 with self.assertRaises(ValidationError):154 valid_against_schema("sample-jsons/race_invalid_result.json",155 "json/race.json",156 expect_failure=True)157 def test_valid_against_metaschema(self):158 self.assertTrue(valid_against_schema("sample-jsons/athlete.json",159 "json/metaschema.json"))160 self.assertTrue(valid_against_schema(161 "sample-jsons/combined_performance.json",162 "json/metaschema.json")163 )164 self.assertTrue(valid_against_schema("sample-jsons/competition.json",165 "json/metaschema.json"))166 self.assertTrue(valid_against_schema("sample-jsons/event.json",167 "json/metaschema.json"))168 self.assertTrue(valid_against_schema("sample-jsons/performance.json",169 "json/metaschema.json"))170 def test_invalid_against_metaschema(self):171 with self.assertRaises(ValidationError):172 valid_against_schema("sample-jsons/athlete_invalid.json",173 "json/metaschema.json",174 expect_failure=True)175 with self.assertRaises(ValidationError):176 valid_against_schema(177 "sample-jsons/combined_performance_invalid.json",178 "json/metaschema.json",179 expect_failure=True)180 with self.assertRaises(ValidationError):181 valid_against_schema(182 "sample-jsons/competition_invalid.json",183 "json/metaschema.json",184 expect_failure=True)185 with self.assertRaises(ValidationError):186 valid_against_schema("sample-jsons/event_invalid.json",187 "json/metaschema.json",188 expect_failure=True)189 with self.assertRaises(ValidationError):190 valid_against_schema("sample-jsons/performance_invalid.json",191 "json/metaschema.json",192 expect_failure=True)193if __name__ == '__main__':...
001-basic.py
Source:001-basic.py
...11 if line.strip() not in output_lines:12 logger.error("'%s': Expected output line not found:\n %r"13 % (" ".join(argv), line))14 return output_lines15def expect_failure(argv, expected_output_lines=[]):16 try:17 instance.criticctl(argv)18 except testing.CriticctlError as error:19 output_lines = set(map(str.strip, error.stderr.splitlines()))20 for line in expected_output_lines:21 if line.strip() not in output_lines:22 logger.error("'%s': Expected output line not found:\n %r"23 % (" ".join(argv), line))24 return output_lines25 else:26 logger.error("'criticctl %s': incorrect criticctl usage did not fail"27 % " ".join(argv))28 return []29try:30 instance.execute(["criticctl"])31except testing.NotSupported:32 # When testing with --quickstart, we can run criticctl, but we're33 # not running it as root, so this particular check is irrelevant.34 pass35except testing.virtualbox.GuestCommandError as error:36 output_lines = set(map(str.strip, error.stderr.splitlines()))37 if "ERROR: Failed to set UID = critic. Run as root?" not in output_lines:38 logger.error("Running 'criticctl' as non-root failed with unexpected "39 "error message")40else:41 logger.error("Running 'criticctl' as non-root did not fail")42# Test -h/--help argument.43usage_lines = expect_success([],44 ["Critic administration interface",45 "Available commands are:"])46expect_success(["-h"],47 usage_lines)48expect_success(["--help"],49 usage_lines)50# Test --etc-dir/-e argument.51expect_success(["--etc-dir", instance.etc_dir],52 usage_lines)53expect_success(["--etc-dir=" + instance.etc_dir],54 usage_lines)55expect_success(["-e", instance.etc_dir],56 usage_lines)57expect_success(["-e" + instance.etc_dir],58 usage_lines)59lines = expect_failure(["--etc-dir", "/etc/wrong"],60 ["ERROR: Directory is inaccessible: /etc/wrong"])61expect_failure(["-e", "/etc/wrong"],62 lines)63lines = expect_failure(["--etc-dir"],64 ["criticctl: error: argument --etc-dir/-e: "65 "expected one argument"])66expect_failure(["-e"],67 lines)68# Test --identity/-i argument.69expect_success(["--identity", "main"],70 usage_lines)71expect_success(["--identity=main"],72 usage_lines)73expect_success(["-i", "main"],74 usage_lines)75expect_success(["-imain"],76 usage_lines)77lines = expect_failure(["--identity", "wrong"],78 ["ERROR: Invalid identity: wrong"])79expect_failure(["-i", "wrong"],80 lines)81lines = expect_failure(["--identity"],82 ["criticctl: error: argument --identity/-i: "83 "expected one argument"])84expect_failure(["-i"],85 lines)86# Test unknown arguments.87expect_failure(["-x"],88 ["criticctl: error: unrecognized arguments: -x"])89expect_failure(["--xxx"],90 ["criticctl: error: unrecognized arguments: --xxx"])91# Test unknown command.92lines = expect_failure(["foo"],93 ["ERROR: Invalid command: foo"])94expect_failure(["-e", instance.etc_dir, "foo"],95 lines)96expect_failure(["-e" + instance.etc_dir, "foo"],97 lines)98expect_failure(["-i", "main", "foo"],99 lines)100expect_failure(["-imain", "foo"],101 lines)102expect_failure(["-e", instance.etc_dir, "-i", "main", "foo"],103 lines)104expect_failure(["-e" + instance.etc_dir, "-imain", "foo"],105 lines)106expect_failure(["-i", "main", "-e", instance.etc_dir, "foo"],107 lines)108expect_failure(["-imain", "-e" + instance.etc_dir, "foo"],...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!