1import unittest
2
3from solution import *
4
5
6class TestWitchMixinFactory(unittest.TestCase):
7 """Test the WitchMixinFactory"""
8
9 def test_sanity(self):
10 """Sanity test for the Witch factory."""
11 self.assertIsInstance(logic_mixin_factory(20, 'mass', 'wood', 'material', 'float'), type)
12
13
14if __name__ == '__main__':
15 unittest.main()
1import unittest
2from unittest.mock import Mock
3
4from solution import *
5
6
7def set_instance_attributes(**attribute_dict):
8 """Dynamically set an __init__ of the decorated class with appropriate values."""
9 def decorated(cls):
10 def __init__(self):
11 for name, value in attribute_dict.items():
12 setattr(self, name, value)
13 cls.__init__ = __init__
14 return cls
15 return decorated
16
17
18class TestWitchMixinFactory(unittest.TestCase):
19 """Test the WitchMixinFactory"""
20
21 RESULT_TRUE = "Burn her!"
22 RESULT_FALSE = "No, but it's a pity, cuz she looks like a witch!"
23
24 def test_realcase(self):
25 """Real test for the Witch factory."""
26 LogicMixin = logic_mixin_factory(20, 'mass', 'wood', 'material', 'float')
27
28 @set_instance_attributes()
29 class Nothing(LogicMixin): pass
30 self.assertEqual(Nothing().is_a_witch(), self.RESULT_FALSE)
31
32 @set_instance_attributes(mass=20)
33 class MassTrue(LogicMixin): pass
34 self.assertEqual(MassTrue().is_a_witch(), self.RESULT_TRUE)
35
36 @set_instance_attributes(mass=1)
37 class MassFalse(LogicMixin): pass
38 self.assertEqual(MassFalse().is_a_witch(), self.RESULT_FALSE)
39
40 @set_instance_attributes(material='wood')
41 class MaterialTrue(LogicMixin): pass
42 self.assertEqual(MaterialTrue().is_a_witch(), self.RESULT_TRUE)
43
44 @set_instance_attributes(material='random')
45 class MaterialFalse(LogicMixin): pass
46 self.assertEqual(MaterialFalse().is_a_witch(), self.RESULT_FALSE)
47
48 @set_instance_attributes(float='random')
49 class FloatFalse(LogicMixin): pass
50 self.assertEqual(FloatFalse().is_a_witch(), self.RESULT_FALSE)
51
52 mock = Mock()
53
54 @set_instance_attributes(float=lambda *args, **kwargs: mock())
55 class FloatTrue(LogicMixin): pass
56 self.assertEqual(FloatTrue().is_a_witch(), self.RESULT_TRUE)
57 mock.assert_not_called()
58
59
60if __name__ == '__main__':
61 unittest.main()
Георги Кунчев
10.11.2023 16:17За атрибути може. За методи - не, тъй като име със стойност `None` няма как да е метод.
|
Тихомир Божков
10.11.2023 15:18None може ли да бъде валидна стойност за атрибути или методи?
|