Beispiel #1
1
 def test_string_substitute(self):
     w = Wax()
     w.foo = 1
     w.bar = Wax()
     w.bar.baz = 1
     self.assertEquals('1 = 1', '%(foo)s = %(bar.baz)s'.__mod__(w))
Beispiel #2
0
    def test_from_dict(self):
        # passing dict to constructor
        kv = {'aa': 1, 'bb': {'cc': 2, 'dd': {'ee': 3}}}
        wx = Wax(kv)
        self.assertEquals(wx.aa, 1)
        self.assertEquals(wx.bb.cc, 2)
        self.assertEquals(wx.bb.dd.ee, 3)

        # merge dict into instance with existing keys
        kv1 = {'aa': 1, 'bb': {'cc': 2}}
        kv2 = {'aa': 5, 'bb': {'cc': 6, 'dd': 7}}
        wx = Wax(kv1, kv2)
        self.assertEquals(wx.aa, 5)
        self.assertEquals(wx.bb.cc, 6)
        self.assertEquals(wx.bb.dd, 7)

        # test dotted keys in dict
        kv = {'aa.bb.cc': 1, 'aa.cc.dd': 2}
        wx = Wax(kv)
        self.assertEquals(wx.aa.bb.cc, 1)
        self.assertEquals(wx.aa.cc.dd, 2)

        # try a DictMixin
        class Foo(UserDict.DictMixin):
            def __init__(self, keyvals):
                self.kv = keyvals

            def items(self):
                return self.kv.items()

        foo = Foo({'aa': 1, 'bb.cc': 2, 'bb': Foo({'dd': 3})})
        wx = Wax(foo)
        self.assertEquals(wx.aa, 1)
        self.assertEquals(wx.bb.cc, 2)
        self.assertEquals(wx.bb.dd, 3)
Beispiel #3
0
    def test_as_kwargs(self):
        def _impl(**kw):
            return kw

        w = Wax(foo=1, bar=Wax(x=1), baz=[1, 2])
        res = _impl(**w)
        self.assertEquals(res, {'foo': 1, 'bar': Wax(x=1), 'baz': [1, 2]})
Beispiel #4
0
 def test_equals(self):
     w1 = Wax(foo=1, bar=Wax(x=1, y=2), baz=[1, 2, 3])
     w2 = Wax(foo=1, bar=Wax(x=1, y=2), baz=[1, 2, 3])
     w3 = Wax(baz=2)
     self.assertEquals(w1, w2)
     self.assertTrue(w1 == w2)
     self.assertFalse(w1 == w3)
     self.assertRaises(TypeError, w1.__eq__, {'foo': 1})
Beispiel #5
0
 def test_copy(self):
     w1 = Wax(foo=1, bar={"a":1,"b":[1,2]}, baz=[1,2,3])
     w1.sub = Wax(foo=1, bar=Wax(baz=3.1415))
     w2 = Wax(w1)
     self.assertEquals(w1, w2)
     # append some data to ensure a pure copy with no shared refs
     w2.bar['c'] = 3
     w2.baz.append(4)
     self.assertNotEquals(w1, w2)
Beispiel #6
0
 def test_basic(self):
     w = Wax()
     w.foo = 1
     w.bar = 2
     w.group1 = Wax()
     w.group1.foo = 1
     self.assertEquals(w.foo, 1)
     self.assertEquals(w.bar, 2)
     self.assertEquals(w.group1.foo, 1)
     self.assertEquals(w['group1.foo'], 1)
Beispiel #7
0
 def test_iterate(self):
     w = Wax()
     w.foo=1
     w.bar=2
     w.abc=3
     w.xyz=4
     keys = []
     for key in w:
         keys.append(key)
     self.assertEquals(['foo','bar','abc','xyz'], keys)
Beispiel #8
0
    def test_delete(self):
        w1 = Wax(foo=1, bar=2)
        w2 = Wax(foo=1)
        del w1['bar']
        self.assertEquals(w1, w2)

        w1 = Wax(foo=1, bar=2)
        w2 = Wax(foo=1)
        delattr(w1, 'bar')
        self.assertEquals(w1, w2)
Beispiel #9
0
    def test_add(self):
        w1 = Wax(foo=1)
        w2 = Wax(bar=2)
        w3 = w1 + w2
        exp = Wax(foo=1, bar=2)
        self.assertEquals(exp, w3)

        w1 = Wax(foo=1)
        w2 = Wax(foo=2)
        w3 = w1 + w2
        self.assertEquals(str(w3).strip(), "foo = 2")
Beispiel #10
0
 def test_key_order(self):
     w = Wax()
     w.zz = Wax()
     w.aa = Wax()
     w.aa.xx = Wax()
     w.aa.bb = Wax()
     w.zz.cc = Wax()
     w.zz.yy = Wax()
     expected = ['[zz.cc]','[zz.yy]','[aa.xx]','[aa.bb]']
     res = str(w).split()
     self.assertEquals(res, expected)
Beispiel #11
0
 def test_key_order(self):
     w = Wax()
     w.zz = Wax()
     w.aa = Wax()
     w.aa.xx = Wax()
     w.aa.bb = Wax()
     w.zz.cc = Wax()
     w.zz.yy = Wax()
     expected = ['[zz.cc]', '[zz.yy]', '[aa.xx]', '[aa.bb]']
     res = str(w).split()
     self.assertEquals(res, expected)
Beispiel #12
0
    def test_annotations(self):
        inp = '; one\n; two\n[foo]\n; three\nval1 = 123'
        w = parse_wax(inp)
        self.assertEquals(w._get_annotation('foo'), ' one\n two')
        self.assertEquals(w._annotations['foo'], ' one\n two')
        del w.foo.val1
        self.assertTrue('val1' not in w.foo._annotations)

        w = parse_wax(WELLFORMED)
        self.assertEquals(w.one._get_annotation('bar'), ' C 1\n C 2')
        self.assertEquals(w.one._annotations['bar'], ' C 1\n C 2')

        # make sure annotations are formatted in output
        out = str(w)
        self.assertTrue('; C 1\n; C 2\n' in out)

        # test for missing annotations
        self.assertEquals(w._get_annotation('xxxxxxx'), None)
        self.assertEquals(w._get_annotation('zzzzzzz', 'foo'), 'foo')

        # test for bad annotation type
        self.assertRaises(WaxError, w._set_annotation, 'foo', 123)

        # check removal
        w = Wax()
        w.a = 123
        w._set_annotation('a', 'hi there')
        self.assertEquals(w._get_annotation('a'), 'hi there')
        w._remove_annotation('a')
        self.assertEquals(w._annotations, {})
Beispiel #13
0
 def test_add_with_comments(self):
     w1 = Wax(a=1)
     w1._add_comment('foo\nbar')
     w1.b = 'a string'
     w1._add_comment('abc\ndef\nghi')
     self.assertEquals(w1.keys(), ['a', 'b'])
     w2 = Wax()
     w2 += w1
     self.assertEquals(w2._comments[0], 'foo\nbar')
     out = str(w2)
     self.assertTrue('# foo\n# bar\n' in out)
Beispiel #14
0
 def test_add_with_annotations(self):
     w1 = Wax(a=1)
     w1._set_annotation('a', 'foo\nbar')
     w1.b = 'a string'
     w1._set_annotation('b', 'abc\ndef\nghi')
     self.assertEquals(w1.keys(), ['a', 'b'])
     w2 = Wax()
     w2 += w1
     self.assertEquals(w2._annotations['a'], 'foo\nbar')
     self.assertEquals(w2._annotations['b'], 'abc\ndef\nghi')
     out = str(w2)
     self.assertTrue('; foo\n; bar\n' in out)
Beispiel #15
0
    def test_roundtrip(self):
        # construct
        w = Wax(foo=1, bar=[1, 2, 3])
        sub1 = Wax(state="AZ", city="Phoenix")
        sub2 = Wax(title=u"\u2018this is single quoted\u2019")
        sub3 = Wax(dict={"abc": {"key": "val", "float": 3.14159}})
        w.sub = sub1
        sub1.sub = sub2
        sub2.sub = sub3

        # serialize
        js = str(w)

        # parse and compare
        res = parse_wax(js)
        self.assertEquals(res, w)
Beispiel #16
0
 def test_copy(self):
     w1 = Wax(foo=1, bar={"a": 1, "b": [1, 2]}, baz=[1, 2, 3])
     w1.sub = Wax(foo=1, bar=Wax(baz=3.1415))
     w2 = Wax(w1)
     self.assertEquals(w1, w2)
     # append some data to ensure a pure copy with no shared refs
     w2.bar['c'] = 3
     w2.baz.append(4)
     self.assertNotEquals(w1, w2)
Beispiel #17
0
 def test_bad_keys(self):
     w = Wax()
     for key in (1, ('a', ), ['a'], True, None):
         self.assertRaises(WaxError, w.__setattr__, key, "a")
         self.assertRaises(WaxError, w.__delattr__, key)
         self.assertRaises(WaxError, w.__setitem__, key, "a")
         self.assertRaises(WaxError, w.__getitem__, key)
         self.assertRaises(WaxError, w.__delitem__, key)
Beispiel #18
0
    def test_rewrites(self):
        for v1, v2 in T_REWRITES:
            w = Wax()
            w.key = v1
            w.key = v2

        # ensure that values that are instances of builtin types can be
        # replaced with classes which inherit from a compatible type, and vice
        # versa.
        class Foo(str): 
            pass

        fstr = Foo("hi")
        w = Wax()
        w.foo = "hello"
        w.foo = fstr
        w.foo = "goodbye"
Beispiel #19
0
 def test_keys(self):
     w = Wax()
     w.a = 1
     w.z = 2
     w.c = 3
     w.b = 4
     self.assertEquals(w.keys(), ['a', 'z', 'c', 'b'])
Beispiel #20
0
    def test_annotations(self):
        inp = '; one\n; two\n[foo]\n; three\nval1 = 123'
        w = parse_wax(inp)
        self.assertEquals(w._get_annotation('foo'), ' one\n two')
        self.assertEquals(w._annotations['foo'], ' one\n two')
        del w.foo.val1
        self.assertTrue('val1' not in w.foo._annotations)

        w = parse_wax(WELLFORMED)
        self.assertEquals(w.one._get_annotation('bar'), ' C 1\n C 2')
        self.assertEquals(w.one._annotations['bar'], ' C 1\n C 2')

        # make sure annotations are formatted in output
        out = str(w)
        self.assertTrue('; C 1\n; C 2\n' in out)

        # test for missing annotations
        self.assertEquals(w._get_annotation('xxxxxxx'), None)
        self.assertEquals(w._get_annotation('zzzzzzz', 'foo'), 'foo')

        # test for bad annotation type
        self.assertRaises(WaxError, w._set_annotation, 'foo', 123)

        # check removal
        w = Wax()
        w.a = 123
        w._set_annotation('a', 'hi there')
        self.assertEquals(w._get_annotation('a'), 'hi there')
        w._remove_annotation('a')
        self.assertEquals(w._annotations, {})
Beispiel #21
0
 def test_double_dotted_add(self):
     w = Wax(key='abc')
     w['foo.bar'] = 1
     w['foo.bar'] = 1
     lines = [l for l in str(w).split('\n') if l]
     self.assertEquals(lines[0], 'key = "abc"')
     self.assertEquals(lines[1], '[foo]')
     self.assertEquals(lines[2], 'bar = 1')
     self.assertEquals(len(lines), 3)
Beispiel #22
0
 def test_get_method(self):
     w = Wax(a=1, b=Wax(c=Wax(d=1)))
     self.assertEquals(w.get('a'), 1)
     self.assertEquals(w.get('a', 2), 1)
     self.assertEquals(w.get('a'), w['a'])
     self.assertEquals(w.get('a'), w.a)
     self.assertEquals(w.get('x', 1), 1)
     self.assertEquals(w.get('x', 2), 2)
     # dotted keys, possibly missing intermediate levels
     self.assertEquals(w.get('b.c.d'), 1)
     self.assertEquals(w.get('b.c.d.e'), None)
     self.assertEquals(w.get('b.c.d.e', 1), 1)
Beispiel #23
0
 def test_keys(self):
     w = Wax()
     w.a = 1
     w.z = 2
     w.c = 3
     w.b = 4
     self.assertEquals(w.keys(), ['a','z','c','b'])
Beispiel #24
0
 def test_add_with_comments(self):
     w1 = Wax(a=1)
     w1._add_comment('foo\nbar')
     w1.b = 'a string'
     w1._add_comment('abc\ndef\nghi')
     self.assertEquals(w1.keys(), ['a', 'b'])
     w2 = Wax()
     w2 += w1
     self.assertEquals(w2._comments[0], 'foo\nbar')
     out = str(w2)
     self.assertTrue('# foo\n# bar\n' in out)
Beispiel #25
0
 def test_notequals(self):
     w1 = Wax(foo=1, bar=Wax(x=1, y=2), baz=[1, 2, 3])
     w2 = Wax(foo=1, bar=Wax(x=1, y=2), baz=[1, 2, 3])
     w3 = Wax(foo=1, bar=Wax(x=2, z=2), baz=[1, 2, 3])
     self.assertNotEquals(w1, w3)
     self.assertTrue(w1 != w3)
     self.assertFalse(w1 != w2)
     self.assertRaises(TypeError, w1.__ne__, {'foo': 1})
Beispiel #26
0
 def test_add_with_annotations(self):
     w1 = Wax(a=1)
     w1._set_annotation('a', 'foo\nbar')
     w1.b = 'a string'
     w1._set_annotation('b', 'abc\ndef\nghi')
     self.assertEquals(w1.keys(), ['a', 'b'])
     w2 = Wax()
     w2 += w1
     self.assertEquals(w2._annotations['a'], 'foo\nbar')
     self.assertEquals(w2._annotations['b'], 'abc\ndef\nghi')
     out = str(w2)
     self.assertTrue('; foo\n; bar\n' in out)
Beispiel #27
0
 def test_basic(self):
     w = Wax()
     w.foo = 1
     w.bar = 2
     w.group1 = Wax()
     w.group1.foo = 1
     self.assertEquals(w.foo, 1)
     self.assertEquals(w.bar, 2)
     self.assertEquals(w.group1.foo, 1)
     self.assertEquals(w['group1.foo'], 1)
Beispiel #28
0
 def test_iterate(self):
     w = Wax()
     w.foo = 1
     w.bar = 2
     w.abc = 3
     w.xyz = 4
     keys = []
     for key in w:
         keys.append(key)
     self.assertEquals(['foo', 'bar', 'abc', 'xyz'], keys)
Beispiel #29
0
    def test_rewrites(self):
        for v1, v2 in T_REWRITES:
            w = Wax()
            w.key = v1
            w.key = v2

        # ensure that values that are instances of builtin types can be
        # replaced with classes which inherit from a compatible type, and vice
        # versa.
        class Foo(str):
            pass

        fstr = Foo("hi")
        w = Wax()
        w.foo = "hello"
        w.foo = fstr
        w.foo = "goodbye"
Beispiel #30
0
    def test_roundtrip(self):
        # construct
        w = Wax(foo=1, bar=[1,2,3])
        sub1 = Wax(state="AZ", city="Phoenix")
        sub2 = Wax(title=u"\u2018this is single quoted\u2019")
        sub3 = Wax(dict={"abc": {"key": "val", "float": 3.14159}})
        w.sub = sub1
        sub1.sub = sub2
        sub2.sub = sub3

        # serialize
        js = str(w)

        # parse and compare
        res = parse_wax(js)
        self.assertEquals(res, w)
Beispiel #31
0
 def test_wax_to_dict(self):
     w = Wax(a=Wax(b=Wax(c=Wax(d=1))))
     wd = wax_to_dict(w)
     self.assertEquals(wd, {'a': {'b': {'c': {'d': 1}}}})
Beispiel #32
0
 def test_dotted_keys(self):
     js = "foo.bar = 123\n"
     w = parse_wax(js)
     self.assertEquals(Wax(foo=Wax(bar=123)), w)
Beispiel #33
0
 def test_parse_append(self):
     w = Wax()
     w.foo = 1
     w += parse_wax('bar = 2\n[one.two]\nkey = "val"')
     self.assertEquals(w.bar, 2)
     self.assertEquals(w.one.two.key, "val")
Beispiel #34
0
 def test_parse_append(self):
     w = Wax()
     w.foo = 1
     w += parse_wax('bar = 2\n[one.two]\nkey = "val"')
     self.assertEquals(w.bar, 2)
     self.assertEquals(w.one.two.key, "val")
Beispiel #35
0
 def test_dotted_setattr(self):
     w = Wax()
     setattr(w, "foo.bar.baz", 123)
     self.assertEquals(w.foo.bar.baz, 123)
Beispiel #36
0
 def test_bad_key(self):
     w = Wax()
     for key in T_BAD_KEYS:
         tmp = key.encode('utf-8')
         self.assertRaises(WaxError, setattr, w, tmp, 1)
         self.assertRaises(WaxError, w.__setitem__, tmp, 1)
Beispiel #37
0
 def test_bad_select(self):
     w = Wax()
     w.foo = 1
     self.assertRaises(WaxError, w.__getitem__, 'foo.bar')
Beispiel #38
0
 def test_bad_add(self):
     w = Wax(foo=1)
     self.assertRaises(WaxError, w.__add__, {"abc": 123})
     self.assertRaises(WaxError, w.__iadd__, [1, 2, 3])
Beispiel #39
0
 def test_bad_delete(self):
     w = Wax(foo=1)
     self.assertRaises(WaxError, w.__delitem__, 'foo.bar')
Beispiel #40
0
 def test_getitem(self):
     w = Wax(a=1, b="foo")
     self.assertEquals(w['a'], 1)
     self.assertEquals(w['b'], 'foo')
Beispiel #41
0
 def test_merge(self):
     w1 = Wax(foo=1)
     s = "bar=2\n[sub]\nbaz=3\n"
     parse_wax(s, w1)
     self.assertEquals(w1.bar, 2)
     self.assertEquals(w1.sub.baz, 3)
Beispiel #42
0
 def test_bad_select(self):
     w = Wax()
     w.foo = 1
     self.assertRaises(WaxError, w.__getitem__, 'foo.bar')
Beispiel #43
0
 def test_get_method(self):
     w = Wax(a=1, b=Wax(c=Wax(d=1)))
     self.assertEquals(w.get('a'), 1)
     self.assertEquals(w.get('a', 2), 1)
     self.assertEquals(w.get('a'), w['a'])
     self.assertEquals(w.get('a'), w.a)
     self.assertEquals(w.get('x', 1), 1)
     self.assertEquals(w.get('x', 2), 2)
     # dotted keys, possibly missing intermediate levels
     self.assertEquals(w.get('b.c.d'), 1)
     self.assertEquals(w.get('b.c.d.e'), None)
     self.assertEquals(w.get('b.c.d.e', 1), 1)
Beispiel #44
0
 def test_link(self):
     w1 = Wax(bar=2, sub=Wax(baz=3))
     w2 = Wax(foo=1, **w1)
     w3 = Wax(foo=1, bar=2, sub=Wax(baz=3))
     self.assertEquals(w2, w3)