From f2a233474409eb0f39fd5e4906bafb92eebe93d4 Mon Sep 17 00:00:00 2001 From: Aineopintojen-harjoitustyo-Algoritmit-j Date: Mon, 5 Feb 2024 09:52:02 +0200 Subject: Fixing delay testing not to rely on actual time elapsed. --- tests/test_app.py | 24 ++++++++++++++---------- tui/tui.py | 5 +++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index d82d3a2..a8a63d2 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -2,7 +2,6 @@ # pylint: disable = missing-class-docstring, too-few-public-methods from io import StringIO -from time import time import unittest from unittest.mock import patch @@ -10,6 +9,7 @@ from app import App from tui import Action + class KbdTest: # pylint: disable = unused-argument, missing-function-docstring def __init__(self, actions): @@ -206,18 +206,22 @@ class TestAppClass(unittest.TestCase): """ Hidastus toimii """ class args(self.default_args): board = self.dssp_win_board - autoplay = 2 delay = 5 app = App(args) - app.ui.kbd=KbdTest([ - (Action.OPEN,0,0), - (Action.HINT,0,0), - ]) - duration = time() - self.assertTrue(app.run()) - duration = time() - duration + with patch('time.sleep') as patched_sleep: + self.assertTrue(app.run()) + del app + patched_sleep.assert_called() + + def test_delay_can_be_off(self): + """ Hidastus ei ole aina päälle """ + class args(self.default_args): + board = self.dssp_win_board + app = App(args) + with patch('time.sleep') as patched_sleep: + self.assertTrue(app.run()) del app - self.assertTrue( duration > 0.5 ) + patched_sleep.assert_not_called() def test_botless_play(self): """ Hidastus toimii """ diff --git a/tui/tui.py b/tui/tui.py index 6b3eb33..9e4c9c5 100644 --- a/tui/tui.py +++ b/tui/tui.py @@ -1,6 +1,7 @@ """ tui/tui.py - runko käyttöliittymälle """ # pylint: disable = multiple-imports -from time import sleep +#from time import sleep +import time from .static import Action from .kbd import Kbd, NoKbd from .ansi_draw import AnsiDraw, SuppressDraw @@ -60,7 +61,7 @@ class Tui(): if action != Action.NOOP: if self.delay: self.draw.matrix(matrix, x, y) - sleep(self.delay/100) + time.sleep(self.delay/100) return Action.OPEN if action==Action.SAFE else action, x, y -- cgit v1.2.3