<s> import platform <EOL> if platform . python_version ( ) < '<STR_LIT>' : <EOL> import unittest2 as unittest <EOL> else : <EOL> import unittest <EOL> if platform . python_version ( ) < '<STR_LIT>' : <EOL> import __builtin__ <EOL> else : <EOL> import builtins as __builtin__ <EOL> import helpers <EOL> from term2048 import board <EOL> Board = board . Board <EOL> try : <EOL> xrange <EOL> except NameError : <EOL> xrange = range <EOL> class TestBoard ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . b = Board ( ) <EOL> def test_init_dimensions ( self ) : <EOL> self . assertEqual ( len ( self . b . cells ) , Board . SIZE ) <EOL> self . assertEqual ( len ( self . b . cells [ <NUM_LIT:0> ] ) , Board . SIZE ) <EOL> if Board . SIZE > <NUM_LIT:1> : <EOL> self . assertEqual ( len ( self . b . cells [ <NUM_LIT:1> ] ) , Board . SIZE ) <EOL> def test_init_dimensions_1 ( self ) : <EOL> b = Board ( size = <NUM_LIT:1> ) <EOL> c = b . cells [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertTrue ( c in [ <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> def test_init_dimensions_3_goal_4 ( self ) : <EOL> b = Board ( size = <NUM_LIT:3> , goal = <NUM_LIT:4> ) <EOL> self . assertEqual ( b . size ( ) , <NUM_LIT:3> ) <EOL> def test_init_only_two_tiles ( self ) : <EOL> t = <NUM_LIT:0> <EOL> for x in xrange ( Board . SIZE ) : <EOL> for y in xrange ( Board . SIZE ) : <EOL> c = self . b . cells [ y ] [ x ] <EOL> if not c == <NUM_LIT:0> : <EOL> t += <NUM_LIT:1> <EOL> else : <EOL> self . assertEqual ( c , <NUM_LIT:0> , '<STR_LIT>' % ( y , x ) ) <EOL> self . assertEqual ( t , <NUM_LIT:2> ) <EOL> def test_init_not_won ( self ) : <EOL> self . assertFalse ( self . b . won ( ) ) <EOL> def test_init_not_filled ( self ) : <EOL> self . assertFalse ( self . b . filled ( ) ) <EOL> def test_size ( self ) : <EOL> s = <NUM_LIT> <EOL> b = Board ( size = s ) <EOL> self . assertEqual ( b . size ( ) , s ) <EOL> def test_goal ( self ) : <EOL> g = <NUM_LIT> <EOL> b = Board ( goal = g ) <EOL> self . assertEqual ( b . goal ( ) , g ) <EOL> def test_won ( self ) : <EOL> self . b . _Board__won = True <EOL> self . assertTrue ( self . b . won ( ) ) <EOL> self . b . _Board__won = False <EOL> self . assertFalse ( self . b . won ( ) ) <EOL> def test_canMove_no_empty_cell ( self ) : <EOL> b = Board ( size = <NUM_LIT:1> ) <EOL> b . setCell ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> ) <EOL> self . assertFalse ( b . canMove ( ) ) <EOL> def test_canMove_empty_cell ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> ) <EOL> self . assertTrue ( b . canMove ( ) ) <EOL> def test_canMove_no_empty_cell_can_collapse ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:2> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:4> , <NUM_LIT:8> ] <EOL> ] <EOL> self . assertTrue ( b . canMove ( ) ) <EOL> def test_filled ( self ) : <EOL> self . b . cells = [ [ <NUM_LIT:1> ] * Board . SIZE for _ in xrange ( Board . SIZE ) ] <EOL> self . assertTrue ( self . b . filled ( ) ) <EOL> def test_addTile ( self ) : <EOL> b = Board ( size = <NUM_LIT:1> ) <EOL> b . cells = [ [ <NUM_LIT:0> ] ] <EOL> b . addTile ( value = <NUM_LIT> ) <EOL> self . assertEqual ( b . cells [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_getCell ( self ) : <EOL> x , y = <NUM_LIT:3> , <NUM_LIT:1> <EOL> v = <NUM_LIT> <EOL> self . b . cells [ y ] [ x ] = v <EOL> self . assertEqual ( self . b . getCell ( x , y ) , v ) <EOL> def test_setCell ( self ) : <EOL> x , y = <NUM_LIT:2> , <NUM_LIT:3> <EOL> v = <NUM_LIT> <EOL> self . b . setCell ( x , y , v ) <EOL> self . assertEqual ( self . b . cells [ y ] [ x ] , v ) <EOL> def test_getLine ( self ) : <EOL> b = Board ( size = <NUM_LIT:4> ) <EOL> l = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:3> ] <EOL> b . cells = [ <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> l , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> ] <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:1> ) , l ) <EOL> def test_getCol ( self ) : <EOL> s = <NUM_LIT:4> <EOL> b = Board ( size = s ) <EOL> l = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:3> ] <EOL> b . cells = [ [ l [ i ] , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:2> ] for i in xrange ( s ) ] <EOL> self . assertSequenceEqual ( b . getCol ( <NUM_LIT:0> ) , l ) <EOL> def test_setLine ( self ) : <EOL> i = <NUM_LIT:2> <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> self . b . setLine ( i , l ) <EOL> self . assertEqual ( self . b . getLine ( i ) , l ) <EOL> def test_setCol ( self ) : <EOL> i = <NUM_LIT:2> <EOL> l = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> self . b . setCol ( i , l ) <EOL> self . assertEqual ( self . b . getCol ( i ) , l ) <EOL> def test_getEmptyCells ( self ) : <EOL> self . assertEqual ( len ( self . b . getEmptyCells ( ) ) , Board . SIZE ** <NUM_LIT:2> - <NUM_LIT:2> ) <EOL> def test_getEmptyCells_filled ( self ) : <EOL> b = Board ( size = <NUM_LIT:1> ) <EOL> b . setCell ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> ) <EOL> self . assertSequenceEqual ( b . getEmptyCells ( ) , [ ] ) <EOL> def test_move_filled ( self ) : <EOL> b = Board ( size = <NUM_LIT:1> ) <EOL> b . setCell ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> ) <EOL> b . move ( Board . UP ) <EOL> self . assertSequenceEqual ( b . cells , [ [ <NUM_LIT> ] ] ) <EOL> b . move ( Board . LEFT ) <EOL> self . assertSequenceEqual ( b . cells , [ [ <NUM_LIT> ] ] ) <EOL> b . move ( Board . RIGHT ) <EOL> self . assertSequenceEqual ( b . cells , [ [ <NUM_LIT> ] ] ) <EOL> b . move ( Board . DOWN ) <EOL> self . assertSequenceEqual ( b . cells , [ [ <NUM_LIT> ] ] ) <EOL> def test_move_add_tile_if_collapse ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> ) <EOL> b . cells = [ [ <NUM_LIT:2> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:0> ] ] <EOL> b . move ( Board . UP ) <EOL> self . assertEqual ( len ( [ e for l in b . cells for e in l if e != <NUM_LIT:0> ] ) , <NUM_LIT:2> ) <EOL> def test_move_add_tile_if_move ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> ) <EOL> b . cells = [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:0> ] ] <EOL> b . move ( Board . UP ) <EOL> self . assertEqual ( len ( [ e for l in b . cells for e in l if e != <NUM_LIT:0> ] ) , <NUM_LIT:2> ) <EOL> def test_move_dont_add_tile_if_nothing_move ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> ) <EOL> b . cells = [ [ <NUM_LIT:2> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] ] <EOL> b . move ( Board . UP ) <EOL> self . assertEqual ( len ( [ e for l in b . cells for e in l if e != <NUM_LIT:0> ] ) , <NUM_LIT:1> ) <EOL> def test_move_dont_add_tile_if_nothing_move2 ( self ) : <EOL> b = Board ( ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:8> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> ] <EOL> self . assertEqual ( b . move ( Board . UP ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( [ e for l in b . cells for e in l if e != <NUM_LIT:0> ] ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:8> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> self . assertEqual ( b . getLine ( <NUM_LIT:1> ) , [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:0> ] ) <EOL> def test_move_collapse ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:2> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> ] <EOL> b . move ( Board . LEFT , add_tile = False ) <EOL> self . assertSequenceEqual ( b . cells , [ <EOL> [ <NUM_LIT:4> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> ] ) <EOL> def test_move_collapse_triplet1 ( self ) : <EOL> b = Board ( size = <NUM_LIT:3> ) <EOL> b . setLine ( <NUM_LIT:0> , [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> b . move ( Board . LEFT , add_tile = False ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:0> ] ) <EOL> def test_move_collapse_triplet2 ( self ) : <EOL> b = Board ( size = <NUM_LIT:3> ) <EOL> b . setLine ( <NUM_LIT:0> , [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> b . move ( Board . RIGHT , add_tile = False ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> def test_move_collapse_with_empty_cell_in_between ( self ) : <EOL> b = Board ( size = <NUM_LIT:3> ) <EOL> b . setLine ( <NUM_LIT:0> , [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> b . move ( Board . RIGHT , add_tile = False ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> ] ) <EOL> def test_move_collapse_with_empty_cell_in_between2 ( self ) : <EOL> b = Board ( size = <NUM_LIT:3> ) <EOL> b . setLine ( <NUM_LIT:0> , [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:2> ] ) <EOL> b . move ( Board . LEFT , add_tile = False ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> def test_move_collapse_and_win ( self ) : <EOL> b = Board ( size = <NUM_LIT:2> , goal = <NUM_LIT:4> ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:2> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> ] <EOL> b . move ( Board . LEFT , add_tile = False ) <EOL> self . assertTrue ( b . won ( ) ) <EOL> def test_move_wrong_direction ( self ) : <EOL> self . assertEqual ( self . b . move ( <NUM_LIT> , add_tile = False ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . b . move ( None ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . b . move ( "<STR_LIT>" ) , <NUM_LIT:0> ) <EOL> def test_move_collapse_chain_col ( self ) : <EOL> b = Board ( ) <EOL> b . setCol ( <NUM_LIT:0> , [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> b . move ( Board . DOWN , add_tile = False ) <EOL> self . assertSequenceEqual ( b . getCol ( <NUM_LIT:0> ) , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:4> ] ) <EOL> def test_move_collapse_chain_line_right ( self ) : <EOL> b = Board ( ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ] , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> ] <EOL> self . assertEqual ( b . move ( Board . RIGHT , add_tile = False ) , <NUM_LIT:4> ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:4> ] ) <EOL> def test_move_collapse_chain_line_right2 ( self ) : <EOL> b = Board ( ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> ] <EOL> self . assertEqual ( b . move ( Board . RIGHT , add_tile = False ) , <NUM_LIT:4> ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:4> ] ) <EOL> def test_move_collapse_chain_line_left ( self ) : <EOL> b = Board ( ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ] , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> ] <EOL> self . assertEqual ( b . move ( Board . LEFT , add_tile = False ) , <NUM_LIT:4> ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> def test_move_collapse_chain_four_same_tiles ( self ) : <EOL> b = Board ( ) <EOL> b . cells = [ <EOL> [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> , <EOL> [ <NUM_LIT:0> ] * <NUM_LIT:4> <EOL> ] <EOL> self . assertEqual ( b . move ( Board . LEFT , add_tile = False ) , <NUM_LIT:8> ) <EOL> self . assertSequenceEqual ( b . getLine ( <NUM_LIT:0> ) , [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> class TestBoardPy3k ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> try : <EOL> self . xr = __builtin__ . xrange <EOL> delattr ( __builtin__ , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> self . xr = None <EOL> helpers . reload ( board ) <EOL> def tearDown ( self ) : <EOL> __builtin__ . xrange = self . xr <EOL> def test_xrange_fallback_on_range_on_py3k ( self ) : <EOL> self . assertEqual ( board . xrange , __builtin__ . range ) </s>
