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 /tests | |
parent | 962156c74418699ad93185f402e88f833779a42d (diff) |
Refactoring tests and re-enabling linting.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_app.py | 60 | ||||
-rw-r--r-- | tests/test_board.py | 112 |
2 files changed, 119 insertions, 53 deletions
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) |