Beispiel #1
0
class TestEffect(unittest.TestCase):

    def setUp(self):
        self.clock = Clock()

        el_id, _type, name, mod_id, reg_id = 0, 1, '', 0, 0
        self.output_element = OutputElement(el_id, _type, name, mod_id, reg_id)

        ef_id, value, _time = 0, 1, 1
        self.effect = Effect(ef_id, self.output_element, value, _time)

    def test_start(self):
        # when cause time is 1000ms
        self.effect.start(1000)

        # then effects cause time should be set to 1000ms and out_el value should be saved
        self.assertEqual(self.effect.cause_time, 1000)
        self.assertEqual(self.effect.prev_value, self.output_element.value)

    def test_run_cause_is_before_effect_time(self):
        # given effect time 1ms after cause
        self.effect.time = 1

        # when
        self.effect.start(self.clock.get_millis())
        time.sleep(0.001)

        # then
        self.assertTrue(self.effect.run())  # effect should happen
        self.assertEqual(self.output_element.desired_value, self.effect.value)

    def test_run_more_times(self):
        # given
        self.effect.time = 0
        # when
        self.effect.start(self.clock.get_millis())
        time.sleep(0.001)

        # then Effect should happen only once
        self.assertTrue(self.effect.run())  # effect should happen
        self.assertFalse(self.effect.run())  # effect should not happen
        self.assertFalse(self.effect.run())  # effect should not happen

    def test_revert_set(self):

        # when normal effect flow
        self.effect.start(self.clock.get_millis())
        time.sleep(0.001)
        self.effect.run()
        self.effect.revert()

        # then output_element desired value should be reverted
        self.assertEqual(self.output_element.desired_value, self.effect.prev_value)
Beispiel #2
0
class TestEffect(unittest.TestCase):
    def setUp(self):
        self.clock = Clock()

        el_id, _type, name, mod_id, reg_id = 0, 1, '', 0, 0
        self.output_element = OutputElement(el_id, _type, name, mod_id, reg_id)

        ef_id, value, _time = 0, 1, 1
        self.effect = Effect(ef_id, self.output_element, value, _time)

    def test_start(self):
        # when cause time is 1000ms
        self.effect.start(1000)

        # then effects cause time should be set to 1000ms and out_el value should be saved
        self.assertEqual(self.effect.cause_time, 1000)
        self.assertEqual(self.effect.prev_value, self.output_element.value)

    def test_run_cause_is_before_effect_time(self):
        # given effect time 1ms after cause
        self.effect.time = 1

        # when
        self.effect.start(self.clock.get_millis())
        time.sleep(0.001)

        # then
        self.assertTrue(self.effect.run())  # effect should happen
        self.assertEqual(self.output_element.desired_value, self.effect.value)

    def test_run_more_times(self):
        # given
        self.effect.time = 0
        # when
        self.effect.start(self.clock.get_millis())
        time.sleep(0.001)

        # then Effect should happen only once
        self.assertTrue(self.effect.run())  # effect should happen
        self.assertFalse(self.effect.run())  # effect should not happen
        self.assertFalse(self.effect.run())  # effect should not happen

    def test_revert_set(self):

        # when normal effect flow
        self.effect.start(self.clock.get_millis())
        time.sleep(0.001)
        self.effect.run()
        self.effect.revert()

        # then output_element desired value should be reverted
        self.assertEqual(self.output_element.desired_value,
                         self.effect.prev_value)
Beispiel #3
0
class TestClock(unittest.TestCase):

    def setUp(self):
        self.clock = Clock()
        self.clock.restart()

    def test_get_seconds(self):
        self.assertAlmostEqual(self.clock.get_seconds(), 0, 3)

    def test_get_milis(self):
        time.sleep(0.001)
        self.assertAlmostEqual(self.clock.get_millis(), 1, 0)

    def test_subscribe(self):

        notifiables = [Notifiable() for _ in range(3)]

        for notifiable in notifiables:
            self.clock.subscribe_for_weekday(notifiable)
            self.clock.subscribe_for_minute(notifiable)

        for notifiable in notifiables:
            self.assertIn(notifiable, self.clock.objects_to_notify_weekday)
            self.assertIn(notifiable, self.clock.objects_to_notify_time)

    def test_evaluate_time(self):

        self.clock.evaluate_time()
        self.assertEqual(self.clock.now.minute, self.clock.minute)
        self.assertEqual(self.clock.now.weekday(), self.clock.weekday)

    def test_notification(self):

        notifiable_minute = Notifiable()
        notifiable_weekday = Notifiable()

        self.clock.subscribe_for_minute(notifiable_minute)
        self.clock.subscribe_for_weekday(notifiable_weekday)

        self.clock.now = datetime.datetime.now()
        self.clock.weekday = 6

        self.clock.notify_minute()
        self.clock.notify_weekday()

        self.assertEqual(notifiable_minute.val, self.clock.now)
        self.assertEqual(notifiable_weekday.val, self.clock.weekday)
Beispiel #4
0
class TestClock(unittest.TestCase):
    def setUp(self):
        self.clock = Clock()
        self.clock.restart()

    def test_get_seconds(self):
        self.assertAlmostEqual(self.clock.get_seconds(), 0, 3)

    def test_get_milis(self):
        time.sleep(0.001)
        self.assertAlmostEqual(self.clock.get_millis(), 1, 0)

    def test_subscribe(self):

        notifiables = [Notifiable() for _ in range(3)]

        for notifiable in notifiables:
            self.clock.subscribe_for_weekday(notifiable)
            self.clock.subscribe_for_minute(notifiable)

        for notifiable in notifiables:
            self.assertIn(notifiable, self.clock.objects_to_notify_weekday)
            self.assertIn(notifiable, self.clock.objects_to_notify_time)

    def test_evaluate_time(self):

        self.clock.evaluate_time()
        self.assertEqual(self.clock.now.minute, self.clock.minute)
        self.assertEqual(self.clock.now.weekday(), self.clock.weekday)

    def test_notification(self):

        notifiable_minute = Notifiable()
        notifiable_weekday = Notifiable()

        self.clock.subscribe_for_minute(notifiable_minute)
        self.clock.subscribe_for_weekday(notifiable_weekday)

        self.clock.now = datetime.datetime.now()
        self.clock.weekday = 6

        self.clock.notify_minute()
        self.clock.notify_weekday()

        self.assertEqual(notifiable_minute.val, self.clock.now)
        self.assertEqual(notifiable_weekday.val, self.clock.weekday)