Ejemplo n.º 1
0
                  r"|"
                  r"'(_?cbor2\.)?CBORTag' objects are unhashable)")
        assert isinstance(exc, TypeError)


def test_set(impl):
    payload = unhexlify('d9010283616361626161')
    value = impl.loads(payload)
    assert type(value) is set
    assert value == {u'a', u'b', u'c'}


@pytest.mark.parametrize(
    'payload, expected',
    [('a1a1616161626163', {
        FrozenDict({'a': 'b'}): 'c'
    }),
     ('A1A1A10101A1666E6573746564F5A1666E6573746564F4', {
         FrozenDict({FrozenDict({1: 1}): FrozenDict({"nested": True})}): {
             "nested": False
         }
     }), ('a182010203', {
         (1, 2): 3
     }), ('a1d901028301020304', {
         frozenset({1, 2, 3}): 4
     }), ('A17f657374726561646d696e67ff01', {
         "streaming": 1
     }),
     ('d9010282d90102820102d90102820304',
      {frozenset({1, 2}), frozenset({3, 4})})])
def test_immutable_keys(impl, payload, expected):
Ejemplo n.º 2
0
def test_dict_key(impl):
    assert impl.dumps({FrozenDict({2: 1}): u''}) == unhexlify('a1a1020160')
Ejemplo n.º 3
0
        impl.loads(unhexlify('A1D9177082010201'))
        exc.match(
            r"(unhashable type: '(_?cbor2\.)?CBORTag'"
            r"|"
            r"'(_?cbor2\.)?CBORTag' objects are unhashable)")


def test_set(impl):
    payload = unhexlify('d9010283616361626161')
    value = impl.loads(payload)
    assert type(value) is set
    assert value == {u'a', u'b', u'c'}


@pytest.mark.parametrize('payload, expected', [
    ('a1a1616161626163', {FrozenDict({'a': 'b'}): 'c'}),
    ('A1A1A10101A1666E6573746564F5A1666E6573746564F4',
        {FrozenDict({FrozenDict({1: 1}): FrozenDict({"nested": True})}): {"nested": False}}),
    ('a182010203', {(1, 2): 3}),
    ('a1d901028301020304', {frozenset({1, 2, 3}): 4}),
    ('A17f657374726561646d696e67ff01', {"streaming": 1}),
    ('d9010282d90102820102d90102820304', {frozenset({1, 2}), frozenset({3, 4})})
])
def test_immutable_keys(impl, payload, expected):
    value = impl.loads(unhexlify(payload))
    assert value == expected


@pytest.mark.parametrize('payload, expected', [
    ('0001', [0, 1]),
    ('a20102030400', [{1: 2, 3: 4}, 0]),
Ejemplo n.º 4
0
    with path.open('wb') as fp:
        impl.dump([1, 10], fp)

    assert path.read_binary() == b'\x82\x01\x0a'


@pytest.mark.parametrize(
    'value, expected',
    [({}, 'a0'), (OrderedDict([(b'a', b''), (b'b', b'')]), 'A2416140416240'),
     (OrderedDict([(b'b', b''), (b'a', b'')]), 'A2416140416240'),
     (OrderedDict([(u'a', u''), (u'b', u'')]), 'a2616160616260'),
     (OrderedDict([(u'b', u''), (u'a', u'')]), 'a2616160616260'),
     (OrderedDict([(b'00001', u''),
                   (b'002', u'')]), 'A2433030326045303030303160'),
     (OrderedDict([(255, 0), (2, 0)]), 'a2020018ff00'),
     (FrozenDict([(b'a', b''), (b'b', b'')]), 'A2416140416240')],
    ids=[
        'empty', 'bytes in order', 'bytes out of order', 'text in order',
        'text out of order', 'byte length', 'integer keys', 'frozendict'
    ])
def test_ordered_map(impl, value, expected):
    expected = unhexlify(expected)
    assert impl.dumps(value, canonical=True) == expected


@pytest.mark.parametrize('value, expected',
                         [(3.5, 'F94300'), (100000.0, 'FA47C35000'),
                          (3.8, 'FB400E666666666666'),
                          (float('inf'), 'f97c00'), (float('nan'), 'f97e00'),
                          (float('-inf'), 'f9fc00'),
                          (float.fromhex('0x1.0p-24'), 'f90001'),
Ejemplo n.º 5
0
def test_frozendict():
    assert len(FrozenDict({1: 2, 3: 4})) == 2
    assert repr(FrozenDict({1: 2})) == "FrozenDict({1: 2})"