<s> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> import os <EOL> import sys <EOL> from os import remove <EOL> from tempfile import NamedTemporaryFile <EOL> import helpers <EOL> from term2048 import ui <EOL> from term2048 . game import Game <EOL> _argv = sys . argv <EOL> _os_system = os . system <EOL> _game_loop = Game . loop <EOL> _store_file = Game . STORE_FILE <EOL> class TestUI ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> store = NamedTemporaryFile ( delete = False ) <EOL> self . exit_status = None <EOL> def fake_exit ( s ) : <EOL> self . exit_status = s <EOL> raise helpers . FakeExit ( ) <EOL> self . exit = sys . exit <EOL> sys . exit = fake_exit <EOL> sys . argv = _argv <EOL> self . stdout = sys . stdout <EOL> self . output = helpers . DevNull ( ) <EOL> self . _game_loop_started = False <EOL> def _loop ( * args , ** kwargs ) : <EOL> self . _game_loop_started = True <EOL> Game . loop = _loop <EOL> Game . STORE_FILE = store . name <EOL> sys . stdout = self . output <EOL> def tearDown ( self ) : <EOL> sys . exit = self . exit <EOL> sys . stdout = self . stdout <EOL> Game . loop = _game_loop <EOL> remove ( Game . STORE_FILE ) <EOL> Game . STORE_FILE = _store_file <EOL> def test_print_version ( self ) : <EOL> try : <EOL> ui . print_version_and_exit ( ) <EOL> except helpers . FakeExit : <EOL> pass <EOL> else : <EOL> self . assertFalse ( True , "<STR_LIT>" ) <EOL> self . assertEqual ( self . exit_status , <NUM_LIT:0> ) <EOL> def test_print_rules ( self ) : <EOL> try : <EOL> ui . print_rules_and_exit ( ) <EOL> except helpers . FakeExit : <EOL> pass <EOL> else : <EOL> self . assertFalse ( True , "<STR_LIT>" ) <EOL> self . assertEqual ( self . exit_status , <NUM_LIT:0> ) <EOL> def test_parse_args_no_args ( self ) : <EOL> sys . argv = [ '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertEqual ( args , { <EOL> '<STR_LIT:version>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> } ) <EOL> def test_parse_args_version_long ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT:version>' ] ) <EOL> def test_parse_args_version_short ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT:version>' ] ) <EOL> def test_parse_args_azmode ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT>' ] ) <EOL> def test_parse_args_azmode_version ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( args [ '<STR_LIT:version>' ] ) <EOL> def test_parse_args_rules_version ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( args [ '<STR_LIT:version>' ] ) <EOL> def test_parse_args_rules_short ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT>' ] ) <EOL> def test_parse_args_rules_version_short_rv ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( args [ '<STR_LIT:version>' ] ) <EOL> def test_parse_args_rules_version_short_vr ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertTrue ( args [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( args [ '<STR_LIT:version>' ] ) <EOL> def test_parse_args_dark_mode ( self ) : <EOL> m = '<STR_LIT>' <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' , m ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertEqual ( args [ '<STR_LIT>' ] , m ) <EOL> def test_parse_args_light_mode ( self ) : <EOL> m = '<STR_LIT>' <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' , m ] <EOL> args = ui . parse_cli_args ( ) <EOL> self . assertEqual ( args [ '<STR_LIT>' ] , m ) <EOL> def test_start_game_print_version ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> ui . start_game ( ) <EOL> except helpers . FakeExit : <EOL> pass <EOL> else : <EOL> self . assertFalse ( True , "<STR_LIT>" ) <EOL> self . assertEqual ( self . exit_status , <NUM_LIT:0> ) <EOL> self . assertRegexpMatches ( self . output . read ( ) , <EOL> r'<STR_LIT>' ) <EOL> def test_start_game_print_version_over_rules ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> ui . start_game ( ) <EOL> except helpers . FakeExit : <EOL> pass <EOL> else : <EOL> self . assertFalse ( True , "<STR_LIT>" ) <EOL> self . assertEqual ( self . exit_status , <NUM_LIT:0> ) <EOL> self . assertRegexpMatches ( self . output . read ( ) , <EOL> r'<STR_LIT>' ) <EOL> def test_start_game_print_rules ( self ) : <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> ui . start_game ( ) <EOL> except helpers . FakeExit : <EOL> pass <EOL> else : <EOL> self . assertFalse ( True , "<STR_LIT>" ) <EOL> self . assertEqual ( self . exit_status , <NUM_LIT:0> ) <EOL> self . assertRegexpMatches ( self . output . read ( ) , r'<STR_LIT>' ) <EOL> def test_start_game_loop ( self ) : <EOL> sys . argv = [ '<STR_LIT>' ] <EOL> self . assertFalse ( self . _game_loop_started ) <EOL> ui . start_game ( ) <EOL> self . assertTrue ( self . _game_loop_started ) <EOL> def test_start_game_no_resume ( self ) : <EOL> g1 = Game ( scores_file = None ) <EOL> g1 . board . setCell ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:16> ) <EOL> self . assertTrue ( g1 . store ( ) ) <EOL> sys . argv = [ '<STR_LIT>' ] <EOL> g2 = ui . start_game ( debug = True ) <EOL> self . assertIn ( g2 . board . getCell ( <NUM_LIT:0> , <NUM_LIT:0> ) , [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> def test_start_game_resume ( self ) : <EOL> cellvalue = <NUM_LIT:2> <EOL> g1 = Game ( scores_file = None ) <EOL> g1 . board . setCell ( <NUM_LIT:0> , <NUM_LIT:0> , cellvalue ) <EOL> g1 . score = <NUM_LIT> <EOL> self . assertTrue ( g1 . store ( ) ) <EOL> sys . argv = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> g2 = ui . start_game ( debug = True ) <EOL> self . assertEqual ( cellvalue , g2 . board . getCell ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . assertEqual ( g1 . score , g2 . score ) </s>
