Beispiel #1
0
 def test_string_formatting(self):
     values_to_test = (
         "string",
         1.234567,
         100,
         date(2020, 2, 5),
     )
     for value in values_to_test:
         const = Constant(value, "foo")
         assert f"{const}" == f"{value}"
     # format spec is passed down
     value = 999.99999238823
     const = Constant(value, "foo")
     assert f"{const:,.2}" == f"{value:,.2}"
Beispiel #2
0
    def test_can_check_if_constant_is_in_constants(self):
        class Dummy(Constants):
            c1 = Constant(1, "one")

        c2 = Constant("other", "Other")
        dummy = Dummy()
        assert Dummy.c1 in dummy
        assert c2 not in dummy
Beispiel #3
0
 def test_str(self):
     const = Constant(value=123, label="Foo")
     expected_possibilities = {
         # python 3.5 doesn't maintain the order
         "value=123, label=Foo",
         "label=Foo, value=123",
     }
     assert str(const) in expected_possibilities
Beispiel #4
0
    def test_can_be_made_immutable(self):
        class Dummy(Constants):
            c1 = Constant(1, "one")
            c2 = Constant(2, "two")
            c3 = Constant(3, "three")

        dummy = Dummy()
        dummy.make_immutable()
        with self.assertRaises(AttributeError):
            dummy.c1.value = 321
        with self.assertRaises(AttributeError):
            dummy.new_attribute = Constant(4, "four")
        dummy.make_mutable()
        try:
            dummy.c1.value = 321
            dummy.new_attribute = Constant(4, "four")
        except AttributeError:
            self.fail("AttributeError raised while mutable.")
Beispiel #5
0
    def test_getting_constant_using_constant(self):
        class Dummy(Constants):
            c1 = Constant(1, "one")

        c2 = Constant("foo", "Foo bar")

        dummy = Dummy()
        assert dummy[dummy.c1] is Dummy.c1
        with self.assertRaises(KeyError):
            assert dummy[c2]
Beispiel #6
0
    def test_cannot_have_duplicate_values(self):
        class Dummy(Constants):
            c1 = Constant(1, "one")
            c2 = Constant(1, "two")

        with self.assertRaises(DuplicateConstantError):
            Dummy()

        class Dummy(Constants):
            c1 = Constant(1, "one")

        d = Dummy()
        with self.assertRaises(DuplicateConstantError):
            d.c3 = Constant(1, "one")
Beispiel #7
0
 def test_if_both_operands_are_constants_it_uses_value_of_both(self):
     c1 = Constant(1, "one")
     c2 = Constant(2, "two")
     operation = perform_on_constant(operator.add)
     assert operation(c1, c2) == 3
Beispiel #8
0
 def test_json_dumps(self):
     const = Constant(value=123, label="Foo")
     assert json.dumps(const) == json.dumps(const.value)
Beispiel #9
0
 def test_can_be_made_immutable(self):
     const = Constant(value=123, label="Foo")
     const.make_immutable()
     with self.assertRaises(AttributeError):
         const.value = 321
     with self.assertRaises(AttributeError):
         const.label = "Bar"
     with self.assertRaises(AttributeError):
         const.new_attribute = "Baz"
     const.make_mutable()
     try:
         const.value = 321
         const.label = "Bar"
         const.new_attribute = "Baz"
     except AttributeError:
         self.fail("AttributeError raised while mutable.")
Beispiel #10
0
 def test_not_equal_to_non_hashable_types(self):
     const = Constant(value=123, label="Foo")
     # comparing does not throw an error
     assert const != []
     assert const != set()
     assert const != {}
Beispiel #11
0
 def test_equality(self):
     const = Constant(value=123, label="Foo")
     assert const == const
     assert const == 123
Beispiel #12
0
 def test_optional_arguments_can_be_provided(self):
     c1 = Constant(3, "foo")
     operation = perform_on_constant(pow)
     assert operation(c1, 3, 4) == 3
Beispiel #13
0
 def test_representation(self):
     const = Constant(value=123, label="Foo")
     assert repr(const) == f"<Constant: {const} at {hex(id(const))}>"
Beispiel #14
0
 class Dummy(Constants):
     c1 = Constant(1, "one")
     c2 = Constant("foo", "Foo bar")
Beispiel #15
0
 class Dummy(Constants):
     c1 = Constant(1, "one")
     c2 = Constant(2, "two")
     c3 = Constant(3, "three")
     c4 = Constant(4, "four")
Beispiel #16
0
 class Dummy(Constants):
     c1 = Constant(1, "one")
Beispiel #17
0
 class Dummy(Constants):
     c1 = Constant(1, "one")
     c2 = Constant(1, "two")
Beispiel #18
0
 class Dummy2(Constants):
     c1 = Constant(1, "one")
     c2 = Constant(2, "two")
     c3 = Constant(3, "three")
Beispiel #19
0
class DummyChoices(Constants):
    c1 = Constant(1, "foo")
    c2 = Constant(2, "bar")
    c3 = Constant(3, "baz")
Beispiel #20
0
 def test_if_left_operand_is_not_a_constant_it_uses_it_directly(self):
     c1 = Constant(1, "one")
     operation = perform_on_constant(operator.add)
     assert operation(2, c1) == 3