Exemplo n.º 1
0
def test_repr():
    """
    repr(AttrDict)
    """
    from lsattrdict.dictionary import AttrDict

    assert_equals(repr(AttrDict()), "AttrDict({})")
    assert_equals(repr(AttrDict({'foo': 'bar'})), "AttrDict({'foo': 'bar'})")
    assert_equals(repr(AttrDict({1: {
        'foo': 'bar'
    }})), "AttrDict({1: {'foo': 'bar'}})")
    assert_equals(repr(AttrDict({1: AttrDict({'foo': 'bar'})})),
                  "AttrDict({1: AttrDict({'foo': 'bar'})})")
Exemplo n.º 2
0
def test_copy():
    """
    Make a dict copy of an AttrDict.
    """
    from lsattrdict.dictionary import AttrDict

    mapping_a = AttrDict({'foo': {'bar': 'baz'}})
    mapping_b = mapping_a.copy()
    mapping_c = mapping_b

    mapping_b['foo']['lorem'] = 'ipsum'

    assert_equals(mapping_a, mapping_b)
    assert_equals(mapping_b, mapping_c)
Exemplo n.º 3
0
    def test_has_key():
        """
        The now-depricated has_keys method
        """
        from lsattrdict.dictionary import AttrDict

        assert_false(hasattr(AttrDict(), 'has_key'))
Exemplo n.º 4
0
    def constructor(items=None, sequence_type=tuple):
        """
        Build a new AttrDict.
        """
        if items is None:
            items = {}

        return AttrDict._constructor(items, sequence_type)
Exemplo n.º 5
0
def test_fromkeys():
    """
    make a new sequence from a set of keys.
    """
    from lsattrdict.dictionary import AttrDict

    # default value
    assert_equals(AttrDict.fromkeys(()), {})
    assert_equals(AttrDict.fromkeys({
        'foo': 'bar',
        'baz': 'qux'
    }), {
        'foo': None,
        'baz': None
    })
    assert_equals(AttrDict.fromkeys(('foo', 'baz')), {
        'foo': None,
        'baz': None
    })

    # custom value
    assert_equals(AttrDict.fromkeys((), 0), {})
    assert_equals(AttrDict.fromkeys({
        'foo': 'bar',
        'baz': 'qux'
    }, 0), {
        'foo': 0,
        'baz': 0
    })
    assert_equals(AttrDict.fromkeys(('foo', 'baz'), 0), {'foo': 0, 'baz': 0})
Exemplo n.º 6
0
    def test_has_key():
        """
        The now-depricated has_keys method
        """
        from lsattrdict.dictionary import AttrDict

        mapping = AttrDict({'foo': 'bar', frozenset((1, 2, 3)): 'abc', 1: 2})
        empty = AttrDict()

        assert_true(mapping.has_key('foo'))
        assert_false(empty.has_key('foo'))

        assert_true(mapping.has_key(frozenset((1, 2, 3))))
        assert_false(empty.has_key(frozenset((1, 2, 3))))

        assert_true(mapping.has_key(1))
        assert_false(empty.has_key(1))

        assert_false(mapping.has_key('banana'))
        assert_false(empty.has_key('banana'))
Exemplo n.º 7
0
def test_init():
    """
    Create a new AttrDict.
    """
    from lsattrdict.dictionary import AttrDict

    # empty
    assert_equals(AttrDict(), {})
    assert_equals(AttrDict(()), {})
    assert_equals(AttrDict({}), {})

    # with items
    assert_equals(AttrDict({'foo': 'bar'}), {'foo': 'bar'})
    assert_equals(AttrDict((('foo', 'bar'), )), {'foo': 'bar'})
    assert_equals(AttrDict(foo='bar'), {'foo': 'bar'})

    # non-overlapping
    assert_equals(AttrDict({}, foo='bar'), {'foo': 'bar'})
    assert_equals(AttrDict((), foo='bar'), {'foo': 'bar'})

    assert_equals(AttrDict({'alpha': 'bravo'}, foo='bar'), {
        'foo': 'bar',
        'alpha': 'bravo'
    })

    assert_equals(AttrDict((('alpha', 'bravo'), ), foo='bar'), {
        'foo': 'bar',
        'alpha': 'bravo'
    })

    # updating
    assert_equals(AttrDict({'alpha': 'bravo'}, foo='bar', alpha='beta'), {
        'foo': 'bar',
        'alpha': 'beta'
    })

    assert_equals(AttrDict((('alpha', 'bravo'), ('alpha', 'beta')), foo='bar'),
                  {
                      'foo': 'bar',
                      'alpha': 'beta'
                  })

    assert_equals(
        AttrDict((('alpha', 'bravo'), ('alpha', 'beta')), alpha='bravo'),
        {'alpha': 'bravo'})