def test_read_state_not_valid(self): self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world> <species>3</species></world><organisms></organisms></life>""" self.input_file = StringIO(self.xml_string) self.io_handler = GameIOHandler(self.input_file) with self.assertRaises(ReadStateError): state = self.io_handler.read_state()
def test_read_state_not_valid(self): self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world><cat>1</cat> </world></life>""" self.input_file = StringIO(self.xml_string) self.io_handler = GameIOHandler(self.input_file, keep_out_file_open=False) with self.assertRaises(ReadStateError): state = self.io_handler.read_state()
def setUp(self): self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world><cells>5</cells> <species>3</species><iterations>3</iterations></world><organisms><organism><x_pos>0</x_pos> <y_pos>0</y_pos><species>2</species></organism><organism><x_pos>1</x_pos><y_pos>0</y_pos> <species>2</species></organism><organism><x_pos>4</x_pos><y_pos>0</y_pos><species>1</species> </organism><organism><x_pos>0</x_pos><y_pos>1</y_pos><species>2</species></organism><organism> <x_pos>2</x_pos><y_pos>1</y_pos><species>1</species></organism><organism><x_pos>3</x_pos> <y_pos>1</y_pos><species>1</species></organism></organisms></life>""" self.input_file = StringIO(self.xml_string) self.io_handler = GameIOHandler(self.input_file, self.OUT_FILE, keep_out_file_open=False) self.original_state = State( 5, 4, 3, [Organism(3, 2, 1), Organism(3, 1, 2)])
def test_usage_success_custom_message(self): info = 'Test' info_test = '! Execute: <python run.py input_file.xml> in order to run the game.\n! ' \ 'Error: %s' % info message = GameIOHandler.usage(info) self.assertEqual(message, info_test)
def test_check_input_success(self): original_file = 'samples/test.xml' arguments = ['run.py', 'samples/test.xml'] input_file = GameIOHandler.check_input(arguments) self.assertEqual(input_file, original_file)
def setUp(self): self.original_organism_l = [ Organism(2, 0, 1), Organism(1, 1, 1), Organism(2, 1, 1), Organism(3, 1, 2), Organism(1, 2, 2), Organism(2, 2, 2), Organism(3, 2, 2), Organism(0, 3, 2), Organism(1, 3, 2), Organism(3, 3, 2) ] self.initial_state = State(5, 2, 2, self.original_organism_l) # we set out file as input file in order to check final state of the game self.io_handler = GameIOHandler('dummy.xml', self.OUT_FILE) self.game = Game(self.io_handler, self.initial_state)
class TestGame(unittest.TestCase): OUT_FILE = 'test-out.xml' def setUp(self): self.original_organism_l = [ Organism(2, 0, 1), Organism(1, 1, 1), Organism(2, 1, 1), Organism(3, 1, 2), Organism(1, 2, 2), Organism(2, 2, 2), Organism(3, 2, 2), Organism(0, 3, 2), Organism(1, 3, 2), Organism(3, 3, 2) ] self.initial_state = State(5, 2, 2, self.original_organism_l) # we set out file as input file in order to check final state of the game self.io_handler = GameIOHandler('dummy.xml', self.OUT_FILE) self.game = Game(self.io_handler, self.initial_state) def test_start_success(self): self.game.start() state = self.io_handler.read_state(self.OUT_FILE) self.assertEqual(state.cells_cnt, self.initial_state.cells_cnt) self.assertEqual(state.species_cnt, self.initial_state.species_cnt) self.assertEqual(state.iterations_cnt, 0) self.assertEqual(len(state.organism_l), 14) # tests for concrete organisms are in test_world.py def test_start_not_existing_organisms_provided(self): self.initial_state.organism_l = [ Organism(100, -20, 1), Organism(1, -10, 2) ] self.game = Game(self.io_handler, self.initial_state) with self.assertRaises(GameRuntimeError): self.game.start() def tearDown(self): try: os.remove(self.OUT_FILE) except (OSError, IOError) as err: print 'Can not remove test file: %s' % err.message
def test_usage_success_default_message(self): info = '! Execute: <python run.py input_file.xml> in order to run the game.\n! ' \ 'Error: The input XML file must be specified.' message = GameIOHandler.usage() self.assertEqual(message, info)
def test_check_input_file_does_not_exist(self): original_file = 'not-existing.xml' arguments = ['run.py', original_file] with self.assertRaises(IOValidationError): input_file = GameIOHandler.check_input(arguments)
def test_check_input_file_wrong_number_of_arguments(self): arguments = ['run.py', 'test.xml', 'extra_argument'] with self.assertRaises(IOValidationError): input_file = GameIOHandler.check_input(arguments)
class TestGameIOHandler(unittest.TestCase): OUT_FILE = 'test-ioout.xml' def setUp(self): self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world><cells>5</cells> <species>3</species><iterations>3</iterations></world><organisms><organism><x_pos>0</x_pos> <y_pos>0</y_pos><species>2</species></organism><organism><x_pos>1</x_pos><y_pos>0</y_pos> <species>2</species></organism><organism><x_pos>4</x_pos><y_pos>0</y_pos><species>1</species> </organism><organism><x_pos>0</x_pos><y_pos>1</y_pos><species>2</species></organism><organism> <x_pos>2</x_pos><y_pos>1</y_pos><species>1</species></organism><organism><x_pos>3</x_pos> <y_pos>1</y_pos><species>1</species></organism></organisms></life>""" self.input_file = StringIO(self.xml_string) self.io_handler = GameIOHandler(self.input_file, self.OUT_FILE, keep_out_file_open=False) self.original_state = State( 5, 4, 3, [Organism(3, 2, 1), Organism(3, 1, 2)]) def test_check_input_success(self): original_file = 'samples/test.xml' arguments = ['run.py', 'samples/test.xml'] input_file = GameIOHandler.check_input(arguments) self.assertEqual(input_file, original_file) def test_check_input_file_wrong_number_of_arguments(self): arguments = ['run.py', 'test.xml', 'extra_argument'] with self.assertRaises(IOValidationError): input_file = GameIOHandler.check_input(arguments) def test_check_input_file_does_not_exist(self): original_file = 'not-existing.xml' arguments = ['run.py', original_file] with self.assertRaises(IOValidationError): input_file = GameIOHandler.check_input(arguments) def test_usage_success_default_message(self): info = '! Execute: <python run.py input_file.xml> in order to run the game.\n! ' \ 'Error: The input XML file must be specified.' message = GameIOHandler.usage() self.assertEqual(message, info) def test_usage_success_custom_message(self): info = 'Test' info_test = '! Execute: <python run.py input_file.xml> in order to run the game.\n! ' \ 'Error: %s' % info message = GameIOHandler.usage(info) self.assertEqual(message, info_test) def test_read_state_success(self): state = self.io_handler.read_state() self.assertTrue(state) # no need to check all arguments as we did in the XML handler tests def test_read_state_not_valid(self): self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world> <species>3</species></world><organisms></organisms></life>""" self.input_file = StringIO(self.xml_string) self.io_handler = GameIOHandler(self.input_file) with self.assertRaises(ReadStateError): state = self.io_handler.read_state() def test_read_state_not_valid(self): self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world><cat>1</cat> </world></life>""" self.input_file = StringIO(self.xml_string) self.io_handler = GameIOHandler(self.input_file, keep_out_file_open=False) with self.assertRaises(ReadStateError): state = self.io_handler.read_state() def test_write_state_success(self): self.io_handler.opened_output_file = self.io_handler._open_file() self.io_handler.write_state(self.original_state, 5) self.io_handler.clean() state = self.io_handler.read_state(self.OUT_FILE) self.assertEqual(state.cells_cnt, self.original_state.cells_cnt) self.assertEqual(state.species_cnt, self.original_state.species_cnt) self.assertEqual(state.iterations_cnt, 5) self.assertEqual(len(state.organism_l), 2) self.clean() def clean(self): try: os.remove(self.OUT_FILE) except (OSError, IOError) as err: print 'Can not remove test file: %s' % err.message
def stop_with_error(error): """Stops the game with error status and message.""" print GameIOHandler.usage(error.message) sys.exit(EXIT_FAILURE)
if __name__ == '__main__': """Main method to run the game of life. Example: Second argument is a XML file which contains the initial state for the game. $ python run.py /path/to/input_file.xml """ print '* The game has started. \n' print '* Checking the input provided. \n' try: input_file = GameIOHandler.check_input(sys.argv) io_handler = GameIOHandler(input_file) except IOValidationError as err: stop_with_error(err) print '* Reading a state from the input file. \n' try: initial_state = io_handler.read_state() except ReadStateError as err: stop_with_error(err) print '* Initializing the game. \n' game = Game(io_handler, initial_state) print '* Starting the game. \n' try: