コード例 #1
0
 def test_repr_and_eq(self):
     """
     Tests that the output from __repr__ can be used to reconstruct the
     CleverDict object, and __eq__ can be used to compare CleverDict objects."""
     x = CleverDict()
     x[0] = 0
     x[False] = 1
     x[1] = 2
     x[True] = 3
     x.a = 4
     x["what?"] = 5
     x.add_alias("a", "c")
     y = eval(repr(x))
     assert x == y
     y.b = 6
     assert x != y
     x = CleverDict()
     assert eval(repr(x)) == x
     with Expand(False):
         x = CleverDict({True: 1})
         assert len(x.get_aliases()) == 1
         assert CleverDict(eval(repr(x))) == x
         # check whether _expand has been properly reset
         x = CleverDict({True: 1})
         assert len(x.get_aliases()) == 1
     # empty dict with one variable
     x = CleverDict()
     x.setattr_direct("a", 1)
     assert len(x.get_aliases()) == 0
     assert eval(repr(x)) == x
コード例 #2
0
 def test_setattr_direct(self):
     """
     Sets an attribute directly, i.e. without making it into an item.
     Attributes set via setattr_direct will
     expressly not appear in the result of repr().  They will appear in the result of str() however.
     """
     x = CleverDict()
     x.setattr_direct("a", "A")
     assert x.a == "A"
     with pytest.raises(KeyError):
         x["a"]
         x.get_key("a")
     assert x.get_aliases() == []
コード例 #3
0
    def test_add_alias_delete_alias(self):
        """
        Aliases are created automatically after expanding 1, True
        for example.  add_alias() and delete_alias() allow us to specify
        additional attribute names as aliases, such that if the value of one
        changes, the value changes for all.
        It is not possible to delete a key.
        """
        x = CleverDict({"red": "a lovely colour", "blue": "a cool colour"})
        alias_list = ["crimson", "burgundy", "scarlet", "normalise~me"]

        x.add_alias("red", alias_list[0])
        x.add_alias("red", alias_list)
        # setting an alias that is already defined for another key is not allowed
        with pytest.raises(KeyError):
            x.add_alias("blue", alias_list[0])
        # setting via an alias is also valid
        x.add_alias("scarlet", "rood")
        assert x.rood == "a lovely colour"

        for alias in alias_list[:-1]:
            assert getattr(x, alias) == "a lovely colour"
            assert x[alias] == "a lovely colour"
        assert getattr(x, "normalise_me") == "a lovely colour"
        assert x["normalise_me"] == "a lovely colour"

        # Updating one alias (or primary Key) should update all:
        x.burgundy = "A RICH COLOUR"
        assert x.scarlet == "A RICH COLOUR"

        x.delete_alias(["scarlet"])
        with pytest.raises(AttributeError):
            x.scarlet
        assert x.crimson == "A RICH COLOUR"

        x.crimson = "the best colour of all"
        assert x.burgundy == "the best colour of all"

        with pytest.raises(KeyError):
            x.delete_alias(["scarlet"])
        # can't delete the  key element
        with pytest.raises(KeyError):
            x.delete_alias("red")

        # test 'expansion' of alias
        x.add_alias("red", True)
        assert x._True is x[True] is x._1 is x[1] is x["red"]

        x = CleverDict()
        x["2"] = 1
        x.add_alias("2", True)
        assert x.get_aliases("2") == ["2", "_2", True, "_1", "_True"]
        # removes True, '_1' and '_True'
        x.delete_alias(True)
        assert x.get_aliases("2") == ["2", "_2"]

        x = CleverDict()
        x["2"] = 1
        x.add_alias("2", True)
        assert x.get_aliases("2") == ["2", "_2", True, "_1", "_True"]
        # only remove True,not '_1' and '_True'
        with Expand(False):
            x.delete_alias(True)
        assert x.get_aliases("2") == ["2", "_2", "_1", "_True"]