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
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() == []
def test_use_as_dict(self): d = dict.fromkeys((0, 1, 23, "what?", "a"), "test") x = CleverDict(d) x.setattr_direct("b", 2) assert dict(x) == d