diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-09 07:49:06 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-09 07:50:15 +0200 |
commit | 110dc9d9debd99dfda2f11773d6e1bce6f622ed3 (patch) | |
tree | b3ce3ee99c2a9bfb0adf8fbf934952fd21c27340 | |
parent | 962156c74418699ad93185f402e88f833779a42d (diff) |
Refactoring tests and re-enabling linting.
-rw-r--r-- | __main__.py | 32 | ||||
-rw-r--r-- | pyproject.toml | 7 | ||||
-rw-r--r-- | tests/test_app.py | 60 | ||||
-rw-r--r-- | tests/test_board.py | 112 |
4 files changed, 138 insertions, 73 deletions
diff --git a/__main__.py b/__main__.py index c98633b..253e699 100644 --- a/__main__.py +++ b/__main__.py @@ -8,42 +8,42 @@ vars(args)['board'] = None if args.count is None and args.file is None: app = App(args) - is_win = app.run() + IS_WIN = app.run() del app - sys.exit(not is_win) # Exit koodeissa 0 on onnistunut suoritus + sys.exit(not IS_WIN) # Exit koodeissa 0 on onnistunut suoritus -win_count = 0 +WIN_COUNT = 0 if args.file is None: args.autoplay = 2 - run_count = args.count - for i in range(run_count): - print(end=f" \rSuoritus {i+1:>6}/{run_count} ") - print(end=f"({100*win_count/(i if i else 1):.1f}%)..") + RUN_COUNT = args.count + for i in range(RUN_COUNT): + print(end=f" \rSuoritus {i+1:>6}/{RUN_COUNT} ") + print(end=f"({100*WIN_COUNT/(i if i else 1):.1f}%)..") if not args.quiet: print() app = App(args) - win_count+=app.run() + WIN_COUNT+=app.run() del app else: - run_count = 0 + RUN_COUNT = 0 with open(args.file, "r", encoding="utf-8") as bfile: board = [] while True: line = bfile.readline() if not line or (line[0]!='.' and line[0]!='@'): if board: - win_percent = (100*win_count/run_count) if run_count else 0 + WIN_PERCENT = (100*WIN_COUNT/RUN_COUNT) if RUN_COUNT else 0 print(end= - f" \rAjo ...{args.file[-18:]:} ({run_count+1}): " - f"({win_percent:.1f}%).." + f" \rAjo ...{args.file[-18:]:} ({RUN_COUNT+1}): " + f"({WIN_PERCENT:.1f}%).." ) if not args.quiet: print() args.board = board app = App(args) - win_count += app.run() - run_count += 1 + WIN_COUNT += app.run() + RUN_COUNT += 1 del app board = [] if not line: @@ -52,6 +52,6 @@ else: board.append([x=='@' for x in line if x in ('.', '@')]) print( - f"\n## Voittoja {win_count}/{run_count} " - f"({(100*win_count/run_count) if run_count else 0:.1f}%)" + f"\n## Voittoja {WIN_COUNT}/{RUN_COUNT} " + f"({(100*WIN_COUNT/RUN_COUNT) if RUN_COUNT else 0:.1f}%)" ) diff --git a/pyproject.toml b/pyproject.toml index e459042..0dfb3e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,11 +20,10 @@ build-backend = "poetry.core.masonry.api" [tool.pylint.main] recursive = true source-roots = ["./"] +# Jostain syystä paikallisesti b._Board__tiles privatti tietojen lukemisen +# aiheuttamaa linttausvirhettä ei voi disabloida test_board.py:ssä. +ignore-patterns = 'test_board' [tool.pylint.basic] -docstring-min-length = 0 [tool.pylint.messages] -# Jostain syystä paikallisesti b._Board__tiles privatti tietojen lukemisen -# aiheuttamaa linttausvirhettä ei voinut disabloida. -disable = "invalid-name" diff --git a/tests/test_app.py b/tests/test_app.py index a8a63d2..1d13a71 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -26,7 +26,7 @@ class KbdTest: class TestAppClass(unittest.TestCase): """ Testit itse appille """ - class default_args: + class DefaultArgs: autoplay = 2 intermediate = None expert = None @@ -81,7 +81,7 @@ class TestAppClass(unittest.TestCase): def test_run(self): """ Testataan että edes pyörähtää """ - app = App(self.default_args) + app = App(self.DefaultArgs) app.run() del app @@ -97,77 +97,77 @@ class TestAppClass(unittest.TestCase): def test_many_games(self): """ Varman voiton lauta palauttaa true """ - class args(self.default_args): + class Args(self.DefaultArgs): quiet = True for _ in range(50): - app = App(args) + app = App(Args) app.run() del app - args.intermediate = True + Args.intermediate = True for _ in range(20): - app = App(args) + app = App(Args) app.run() del app - args.expert = True + Args.expert = True for _ in range(10): - app = App(args) + app = App(Args) app.run() del app def test_sure_win(self): """ Varman voiton lauta palauttaa true """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.sure_win_board quiet = True - app = App(args) + app = App(Args) self.assertTrue(app.run()) del app def test_dssp_win(self): """ Varman voiton lauta palauttaa true """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.dssp_win_board - app = App(args) + app = App(Args) self.assertTrue(app.run()) del app def test_no_dssp_win_with_simple(self): """ Varman voiton lauta palauttaa true """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.dssp_win_board quiet = True bot = 1 while True: - app = App(args) + app = App(Args) if not app.run(): break del app def test_sure_lose(self): """ Varman häviön lauta palauttaa false """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.sure_lose_board - app = App(args) + app = App(Args) self.assertFalse(app.run()) del app def test_custom_size(self): """ Varman häviön lauta palauttaa false """ - class args(self.default_args): + class Args(self.DefaultArgs): size = (4, 4) with patch('sys.stdout', new = StringIO()) as captured: - app = App(args) + app = App(Args) app.run() self.assertIn("Mukautettu (4x4", captured.getvalue()) del app def test_sure_win_with_actions(self): """ Varman voiton lauta palauttaa true """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.sure_win_board autoplay = 0 bot = 0 - app = App(args) + app = App(Args) app.ui.kbd=KbdTest([ (Action.SAFE,0,0), (Action.OPEN,0,0) @@ -177,10 +177,10 @@ class TestAppClass(unittest.TestCase): def test_sure_lose_with_actions(self): """ Varman voiton lauta palauttaa true """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.sure_lose_board autoplay = 0 - app = App(args) + app = App(Args) app.ui.kbd=KbdTest([ (Action.FLAG,0,0), (Action.MINE,0,0), @@ -191,10 +191,10 @@ class TestAppClass(unittest.TestCase): def test_auto_play_hints(self): """ Vihjeiden automaattipelaaminen toimii """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.dssp_win_board autoplay = 1 - app = App(args) + app = App(Args) app.ui.kbd=KbdTest([ (Action.OPEN,0,0), (Action.HINT,0,0), @@ -204,10 +204,10 @@ class TestAppClass(unittest.TestCase): def test_delay(self): """ Hidastus toimii """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.dssp_win_board delay = 5 - app = App(args) + app = App(Args) with patch('time.sleep') as patched_sleep: self.assertTrue(app.run()) del app @@ -215,9 +215,9 @@ class TestAppClass(unittest.TestCase): def test_delay_can_be_off(self): """ Hidastus ei ole aina päälle """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.dssp_win_board - app = App(args) + app = App(Args) with patch('time.sleep') as patched_sleep: self.assertTrue(app.run()) del app @@ -225,11 +225,11 @@ class TestAppClass(unittest.TestCase): def test_botless_play(self): """ Hidastus toimii """ - class args(self.default_args): + class Args(self.DefaultArgs): board = self.mini_board autoplay = 0 delay = 50000 - app = App(args) + app = App(Args) app.ui.kbd=KbdTest([ (Action.OPEN,0,0), (Action.HINT,0,0), diff --git a/tests/test_board.py b/tests/test_board.py index f664e9c..b7678c8 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -1,14 +1,19 @@ """test_board.py - Testit pelilaudalle""" -# pylint: disable = protected-access +# Tämä tiedosto on jätettty pylint testien ulkopuolelle, koska tässä tehdään +# paljon "kiellettyjä asioita", kuten käydään lukemassa luokan "privaatteja" +# muuttujia import unittest from board import Board, Level, LevelSpecs class TestBoardClass(unittest.TestCase): - """ pelilauden testit""" - def test_init(self): - """ olion luominen onnistuu """ + """ pelilauden testit kattava luokka """ + def test_init_works_and_defaults_beginner(self): + """ pelilautaolion luominen onnistuu ja defaulttaa aloittelijaksi """ b = Board() + self.assertEqual(b.get_width(), LevelSpecs[Level.BEGINNER][0]) + self.assertEqual(b.get_height(), LevelSpecs[Level.BEGINNER][1]) + self.assertEqual(b.get_mines(), LevelSpecs[Level.BEGINNER][2]) self.assertTrue(b.get_width()>0) def test_init_with_level(self): @@ -25,23 +30,90 @@ class TestBoardClass(unittest.TestCase): self.assertEqual(b.get_height(), LevelSpecs[Level.BEGINNER][1]) self.assertEqual(b.get_mines(), LevelSpecs[Level.BEGINNER][2]) - def test_get_view_and_guess(self): - """ laudan näkymä on oikein senkin jälkeen kun on arvattu""" - b = Board(width=3, height=3) - b._Board__tiles=[[0,0,0],[0,1,1],[0,1,9]] + def matrixs_equals(self, m1, m2): + """ apufunktio testaa onko matriisit samat """ + # onko edes samaa kokoa ? + if len(m1)!=len(m2): + return False + for i in range(len(m1)): + if m1[i] != m2[i]: + return False + return True + + + def test_init_with_valid_board(self): + """ Pelilaudan luominen onnistuu kelvollisella asettelulla """ + t = [ + [0,0,0,0], + [0,0,0,1], + [0,0,0,0] + ] + b = Board(board = t) + self.assertEqual(b.get_width(), 4) + self.assertEqual(b.get_height(), 3) + self.assertEqual(b.get_mines(), 1) - v = b.get_view() - t = [[12,12,12],[12,12,12],[12,12,12]] - for i in range(3): - self.assertEqual(v[i],t[i]) + def test_init_board_is_masked_right(self): + """ Luodun pelilaudan laatat ja peitteet on asetettu oikein """ + t = [ + [0,0,0,0], + [0,0,0,1], + [0,0,0,0] + ] + b = Board(board = t) + self.assertEqual(b.get_width(), 4) + self.assertEqual(b.get_height(), 3) + self.assertEqual(b.get_mines(), 1) + + # testataan onko laatat tallennettu oikein luokkaan + t = [ + [0,0,0], + [0,0,0], + [1,1,1], + [1,9,1] + ] + self.assertTrue(self.matrixs_equals(b._Board__tiles, t)) + + # onko maksit asetettu oikein + t = [ + [12,12,12], + [12,12,12], + [12,12,12], + [12,12,12] + ] + self.assertTrue(self.matrixs_equals(b._Board__masked, t)) + def test_get_view_and_guess(self): + """ laudan näkymä on oikein senkin jälkeen kun on arvattu """ + + t = [ + [0,0,1], + [0,0,0], + [0,0,0] + ] + b = Board(board=t) + + # Antaahan pelikenttä pelkkää maskia aluksi + t = [ + [12,12,12], + [12,12,12], + [12,12,12] + ] + self.assertTrue(self.matrixs_equals(b.get_view(), t)) + + # avataan yläkulma -> palatuu True self.assertTrue(b.guess(0,0)) - v = b.get_view() - t = [[0,0,0],[0,1,1],[0,1,12]] - for i in range(3): - self.assertEqual(v[i],t[i]) - self.assertFalse(b.guess(2,2)) + # onko näkymä nyt oikein + t = [ + [0,0,0], + [1,1,0], + [12,1,0] + ] + self.assertTrue(self.matrixs_equals(b.get_view(), t)) + + # avataan alakulma jossa miina -> palautuu False + self.assertFalse(b.guess(2,0)) def test_is_winning(self): """ toimiiko voittotilanteen tunnistus """ @@ -121,9 +193,3 @@ class TestBoardClass(unittest.TestCase): b = Board(board=[[0,0,0,0],[0,0,0,0],[0,0,0,0]]) self.assertIn(LevelSpecs[Level.BEGINNER][3], b.get_level_name()) - def test_board_valid(self): - """ Luodaan peli kelvollisella laudalla """ - b = Board(board=[[0,0,0,0],[0,0,0,1],[0,0,0,0]]) - self.assertEqual(b.get_width(), 4) - self.assertEqual(b.get_height(), 3) - self.assertEqual(b.get_mines(), 1) |