|
| 1 | +import unittest |
| 2 | + |
| 3 | +from Options import Choice, DefaultOnToggle, Toggle |
| 4 | + |
| 5 | + |
| 6 | +class TestNumericOptions(unittest.TestCase): |
| 7 | + def test_numeric_option(self) -> None: |
| 8 | + """Tests the initialization and equivalency comparisons of the base Numeric Option class.""" |
| 9 | + class TestChoice(Choice): |
| 10 | + option_zero = 0 |
| 11 | + option_one = 1 |
| 12 | + option_two = 2 |
| 13 | + alias_three = 1 |
| 14 | + non_option_attr = 2 |
| 15 | + |
| 16 | + class TestToggle(Toggle): |
| 17 | + pass |
| 18 | + |
| 19 | + class TestDefaultOnToggle(DefaultOnToggle): |
| 20 | + pass |
| 21 | + |
| 22 | + with self.subTest("choice"): |
| 23 | + choice_option_default = TestChoice.from_any(TestChoice.default) |
| 24 | + choice_option_string = TestChoice.from_any("one") |
| 25 | + choice_option_int = TestChoice.from_any(2) |
| 26 | + choice_option_alias = TestChoice.from_any("three") |
| 27 | + choice_option_attr = TestChoice.from_any(TestChoice.option_two) |
| 28 | + |
| 29 | + self.assertEqual(choice_option_default, TestChoice.option_zero, |
| 30 | + "assigning default didn't match default value") |
| 31 | + self.assertEqual(choice_option_string, "one") |
| 32 | + self.assertEqual(choice_option_int, 2) |
| 33 | + self.assertEqual(choice_option_alias, TestChoice.alias_three) |
| 34 | + self.assertEqual(choice_option_attr, TestChoice.non_option_attr) |
| 35 | + |
| 36 | + self.assertRaises(KeyError, TestChoice.from_any, "four") |
| 37 | + |
| 38 | + self.assertIn(choice_option_int, [1, 2, 3]) |
| 39 | + self.assertIn(choice_option_int, {2}) |
| 40 | + self.assertIn(choice_option_int, (2,)) |
| 41 | + |
| 42 | + self.assertIn(choice_option_string, ["one", "two", "three"]) |
| 43 | + # this fails since the hash is derived from the value |
| 44 | + self.assertNotIn(choice_option_string, {"one"}) |
| 45 | + self.assertIn(choice_option_string, ("one",)) |
| 46 | + |
| 47 | + with self.subTest("toggle"): |
| 48 | + toggle_default = TestToggle.from_any(TestToggle.default) |
| 49 | + toggle_string = TestToggle.from_any("false") |
| 50 | + toggle_int = TestToggle.from_any(0) |
| 51 | + toggle_alias = TestToggle.from_any("off") |
| 52 | + |
| 53 | + self.assertFalse(toggle_default) |
| 54 | + self.assertFalse(toggle_string) |
| 55 | + self.assertFalse(toggle_int) |
| 56 | + self.assertFalse(toggle_alias) |
| 57 | + |
| 58 | + with self.subTest("on toggle"): |
| 59 | + toggle_default = TestDefaultOnToggle.from_any(TestDefaultOnToggle.default) |
| 60 | + toggle_string = TestDefaultOnToggle.from_any("true") |
| 61 | + toggle_int = TestDefaultOnToggle.from_any(1) |
| 62 | + toggle_alias = TestDefaultOnToggle.from_any("on") |
| 63 | + |
| 64 | + self.assertTrue(toggle_default) |
| 65 | + self.assertTrue(toggle_string) |
| 66 | + self.assertTrue(toggle_int) |
| 67 | + self.assertTrue(toggle_alias) |
0 commit comments