def test_Expand(self): with Expand(False): x = CleverDict.fromkeys(("a", 0, 1, "what?"), 1) x.add_alias(0, 2) assert x.get_aliases("a") == ["a"] assert x.get_aliases(0) == [0, 2] assert x.get_aliases(1) == [1] x = CleverDict.fromkeys(("a", 0, 1, "what?"), 1) x.add_alias(0, 2) assert x.get_aliases("a") == ["a"] assert x.get_aliases(0) == [0, "_0", "_False", 2, "_2"] assert x.get_aliases(1) == [1, "_1", "_True"] with Expand(True): x = CleverDict.fromkeys(("a", 0, 1, "what?"), 1) x.add_alias(0, 2) assert x.get_aliases("a") == ["a"] assert x.get_aliases(0) == [0, "_0", "_False", 2, "_2"] assert x.get_aliases(1) == [1, "_1", "_True"] CleverDict.expand = False x = CleverDict({1: 1}) with pytest.raises(AttributeError): x._1 CleverDict.expand = True x = CleverDict({1: 1}) x._1
def test_info(self, capsys): global c # globals are not 'seen' by info() z = b = a = c = CleverDict.fromkeys((0, 1, 2, "a", "what?", "return"), 0) c.setattr_direct("b", "B") c.info() # this prints to stdout out, err = capsys.readouterr() assert out == dedent("""\ CleverDict: a is b is z a[0] == a['_0'] == a['_False'] == a._0 == a._False == 0 a[1] == a['_1'] == a['_True'] == a._1 == a._True == 0 a[2] == a['_2'] == a._2 == 0 a['a'] == a.a == 0 a['what?'] == a['what_'] == a.what_ == 0 a['return'] == a['_return'] == a._return == 0 a.b == 'B' """) z = b = a = c = CleverDict.fromkeys(["a"], "A") assert c.info( as_str=True ) == "CleverDict:\n a is b is z\n a['a'] == a.a == 'A'" del a assert c.info( as_str=True) == "CleverDict:\n b is z\n b['a'] == b.a == 'A'" del b assert c.info(as_str=True) == "CleverDict:\n z['a'] == z.a == 'A'" del z assert c.info(as_str=True) == "CleverDict:\n x['a'] == x.a == 'A'"
def testget_key(self): x = CleverDict.fromkeys(("a", 0, 1, "what?"), 1) x.add_alias(0, "zero") for key in x.keys(): for name in x.get_aliases(key): assert x.get_key(name) == key assert x.get_key(True) == 1 assert x.get_key("_True") == 1 assert x.get_key("zero") == 0
def testupdate(self): x = CleverDict.fromkeys((0, 1, 2, "a", "what?", "return"), 0) y = CleverDict({0: 2, "c": 3}) x.update(y) assert x == CleverDict({ 0: 2, 1: 0, 2: 0, "a": 0, "what?": 0, "return": 0, "c": 3 })