From 824d5e3b6954407a694f5739cbeb40f66324c8d7 Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Wed, 14 Dec 2022 15:41:49 +0100 Subject: Developing Unit Testing. --- tests/test_imagoIO.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/test_imagoIO.py (limited to 'tests/test_imagoIO.py') diff --git a/tests/test_imagoIO.py b/tests/test_imagoIO.py new file mode 100644 index 0000000..c3c6fda --- /dev/null +++ b/tests/test_imagoIO.py @@ -0,0 +1,40 @@ +"""Tests for the input/output component.""" + +import unittest + +import io +import sys + +from imago.engine.imagoIO import ImagoIO + +class TestImagoIO(unittest.TestCase): + """Test ImagoIO component.""" + + @unittest.mock.patch('imago.engine.imagoIO.input', create=True) + + def testSimpleCommands(self, mocked_input): + """Test simple commands.""" + + mocked_input.side_effect = [ + 'name\n', + 'version\n', + 'protocol_version\n', + 'quit\n' + ] + + testout = io.StringIO() + imagoIO = ImagoIO(outputStream=testout) + + imagoIO.start() + value = testout.getvalue() + self.assertEqual( + '= Imago\n\n' + + '= 0.0.0\n\n' + + '= 2\n\n', + value + ) + + testout.close() + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.1 From 80c4cca827ff80c0508c27cd9b6a37ffa2ea17e5 Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Wed, 11 Jan 2023 19:20:06 +0100 Subject: 100% coverage on imagoIO module. --- tests/test_imagoIO.py | 164 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 3 deletions(-) (limited to 'tests/test_imagoIO.py') diff --git a/tests/test_imagoIO.py b/tests/test_imagoIO.py index c3c6fda..4f2d7bd 100644 --- a/tests/test_imagoIO.py +++ b/tests/test_imagoIO.py @@ -5,36 +5,194 @@ import unittest import io import sys +from imago.data.enums import DecisionAlgorithms from imago.engine.imagoIO import ImagoIO +from imago.engine.parseHelpers import parseVertex class TestImagoIO(unittest.TestCase): """Test ImagoIO component.""" @unittest.mock.patch('imago.engine.imagoIO.input', create=True) - def testSimpleCommands(self, mocked_input): """Test simple commands.""" + self.maxDiff = None + mocked_input.side_effect = [ + '\n', 'name\n', 'version\n', 'protocol_version\n', + 'clear_board\n', + '\n', + 'known_command\n', + 'known_command name\n', + 'known_command version\n', + 'known_command wrongcommand\n', + '\n', + 'boardsize\n', + 'boardsize 10\n', + '\n', + 'komi\n', + 'komi 5.5\n', + '\n', + 'play\n', + 'play 1\n', + 'play 1 2\n', + 'play b a1\n', + '\n', + 'undo\n', + 'undo\n', + '\n', + 'wrongcommand\n', + '\n', 'quit\n' ] testout = io.StringIO() - imagoIO = ImagoIO(outputStream=testout) + imagoIO = ImagoIO( + decisionAlgorithmId=DecisionAlgorithms.MONTECARLO, + outputStream=testout + ) imagoIO.start() value = testout.getvalue() self.assertEqual( '= Imago\n\n' + '= 0.0.0\n\n' + - '= 2\n\n', + '= 2\n\n' + + '= \n\n' + + '? Wrong number of arguments\n' + + '? Usage: known_command COMMAND_NAME\n\n' + + '= true\n\n' + + '= true\n\n' + + '= false\n\n' + + '? Wrong number of arguments\n' + + '? Usage: boardsize \n\n' + + '= \n\n' + + '? Wrong number of arguments\n' + + '? Usage: komi \n\n' + + '= \n\n' + + '? Wrong number of arguments\n' + + '? Usage: play \n\n' + + '? Wrong number of arguments\n' + + '? Usage: play \n\n' + + '? Invalid move: Unknown color [1].\n\n' + + '= \n\n' + + '= \n\n' + + '= \n\n' + + '? unknown command\n\n', + value + ) + + testout.close() + + + @unittest.mock.patch('imago.engine.imagoIO.input', create=True) + def testListsCommands(self, mocked_input): + """Test command for listing all commands.""" + + mocked_input.side_effect = [ + 'list_commands\n', + 'quit\n' + ] + + testout = io.StringIO() + imagoIO = ImagoIO( + decisionAlgorithmId=DecisionAlgorithms.MONTECARLO, + outputStream=testout + ) + + commandsString = "\n".join(list(map( + lambda cmd: "%s - %s" % (cmd.__name__, cmd.__doc__), + imagoIO.commands_set))) + + imagoIO.start() + value = testout.getvalue() + self.assertEqual( + '= ' + + commandsString + + '\n\n\n', value ) testout.close() + + @unittest.mock.patch('imago.engine.imagoIO.input', create=True) + def testFixedHandicap(self, mocked_input): + """Test command for setting fixed handicap stones.""" + + mocked_input.side_effect = [ + 'fixed_handicap\n', + 'fixed_handicap 2\n', + 'quit\n' + ] + + testout = io.StringIO() + imagoIO = ImagoIO( + decisionAlgorithmId=DecisionAlgorithms.MONTECARLO, + outputStream=testout + ) + + imagoIO.start() + value = testout.getvalue() + self.assertEqual( + '? Wrong number of arguments\n' + + '? Usage: fixed_handicap \n\n' + + '= A1 A2\n\n', + value + ) + + testout.close() + + +# @unittest.mock.patch('imago.engine.imagoIO.input', create=True) +# def testGenmove(self, mocked_input): +# """Test command for generating a move.""" +# +# mocked_input.side_effect = [ +# 'genmove\n', +# 'genmove w w\n', +# 'genmove 2\n', +# 'quit\n' +# ] +# +# testout = io.StringIO() +# imagoIO = ImagoIO( +# decisionAlgorithmId=DecisionAlgorithms.MONTECARLO, +# outputStream=testout +# ) +# +# imagoIO.start() +# value = testout.getvalue() +# self.assertEqual( +# '? Wrong number of arguments\n' + +# '? Usage: genmove \n\n' + +# '? Wrong number of arguments\n' + +# '? Usage: genmove \n\n' + +# '? Error: Unknown color [2].\n\n', +# value +# ) +# +# mocked_input.side_effect = [ +# 'genmove w\n', +# 'quit\n'] +# +# testout = io.StringIO() +# imagoIO = ImagoIO( +# decisionAlgorithmId=DecisionAlgorithms.MONTECARLO, +# outputStream=testout +# ) +# imagoIO.start() +# value = testout.getvalue() +# vertexValue = value.split(' ')[1].split('\n')[0] +# +# # Test parsing vertex does not raise an error +# parseVertex(vertexValue, imagoIO.gameEngine.gameState.size) +# +# testout.close() + + if __name__ == '__main__': unittest.main() -- cgit v1.2.1 From 65ac3a6b050dcb88688cdc2654b1ed6693e9a160 Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Mon, 12 Jun 2023 19:43:40 +0200 Subject: Submitted version. --- tests/test_imagoIO.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'tests/test_imagoIO.py') diff --git a/tests/test_imagoIO.py b/tests/test_imagoIO.py index 4f2d7bd..499fdb2 100644 --- a/tests/test_imagoIO.py +++ b/tests/test_imagoIO.py @@ -81,7 +81,8 @@ class TestImagoIO(unittest.TestCase): '= \n\n' + '= \n\n' + '= \n\n' + - '? unknown command\n\n', + '? unknown command\n\n' + + '= \n\n', value ) @@ -112,7 +113,8 @@ class TestImagoIO(unittest.TestCase): self.assertEqual( '= ' + commandsString + - '\n\n\n', + '\n\n' + + '= \n\n', value ) @@ -140,7 +142,8 @@ class TestImagoIO(unittest.TestCase): self.assertEqual( '? Wrong number of arguments\n' + '? Usage: fixed_handicap \n\n' + - '= A1 A2\n\n', + '= A1 A2\n\n' + + '= \n\n', value ) -- cgit v1.2.1