diff options
author | InigoGutierrez <inigogf.95@gmail.com> | 2023-06-12 20:16:04 +0200 |
---|---|---|
committer | InigoGutierrez <inigogf.95@gmail.com> | 2023-06-12 20:16:04 +0200 |
commit | d4a81490bf1396089eb3dac5955a3a8e4cb26e37 (patch) | |
tree | f96febc7950c2742bc36f04ab13bff56851f2388 /tests/test_imagoIO.py | |
parent | b08408d23186205e71dfc68634021e3236bfb45c (diff) | |
parent | 65ac3a6b050dcb88688cdc2654b1ed6693e9a160 (diff) | |
download | imago-d4a81490bf1396089eb3dac5955a3a8e4cb26e37.tar.gz imago-d4a81490bf1396089eb3dac5955a3a8e4cb26e37.zip |
Diffstat (limited to 'tests/test_imagoIO.py')
-rw-r--r-- | tests/test_imagoIO.py | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/tests/test_imagoIO.py b/tests/test_imagoIO.py new file mode 100644 index 0000000..499fdb2 --- /dev/null +++ b/tests/test_imagoIO.py @@ -0,0 +1,201 @@ +"""Tests for the input/output component.""" + +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( + decisionAlgorithmId=DecisionAlgorithms.MONTECARLO, + outputStream=testout + ) + + imagoIO.start() + value = testout.getvalue() + self.assertEqual( + '= Imago\n\n' + + '= 0.0.0\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 <newSize>\n\n' + + '= \n\n' + + '? Wrong number of arguments\n' + + '? Usage: komi <newKomi>\n\n' + + '= \n\n' + + '? Wrong number of arguments\n' + + '? Usage: play <color> <vertex>\n\n' + + '? Wrong number of arguments\n' + + '? Usage: play <color> <vertex>\n\n' + + '? Invalid move: Unknown color [1].\n\n' + + '= \n\n' + + '= \n\n' + + '= \n\n' + + '? unknown command\n\n' + + '= \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\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 <count>\n\n' + + '= A1 A2\n\n' + + '= \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 <color>\n\n' + +# '? Wrong number of arguments\n' + +# '? Usage: genmove <color>\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() |