Example #1
0
def test_simple_value_equals():
    tag1 = CBORSimpleValue(1)
    tag2 = CBORSimpleValue(1)
    tag3 = CBORSimpleValue(21)
    assert tag1 == tag2
    assert tag1 == 1
    assert not tag1 == tag3
    assert not tag1 == 21
    assert not tag2 == "21"
Example #2
0
def decode_special(decoder, subtype, shareable_index=None):
    # Simple value
    if subtype < 20:
        return CBORSimpleValue(subtype)

    # Major tag 7
    return special_decoders[subtype](decoder)
Example #3
0
    ('9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff', list(range(1, 26))),
    ('bf61610161629f0203ffff', {'a': 1, 'b': [2, 3]}),
    ('826161bf61626163ff', ['a', {'b': 'c'}]),
    ('bf6346756ef563416d7421ff', {'Fun': True, 'Amt': -2}),
])
def test_streaming(payload, expected):
    decoded = loads(unhexlify(payload))
    assert decoded == expected


@pytest.mark.parametrize('payload, expected', [
    ('e0', 0),
    ('e2', 2),
    ('f3', 19),
    ('f820', 32),
    ('e0', CBORSimpleValue(0)),
    ('e2', CBORSimpleValue(2)),
    ('f3', CBORSimpleValue(19)),
    ('f820', CBORSimpleValue(32))
])
def test_simple_value(payload, expected):
    decoded = loads(unhexlify(payload))
    assert decoded == expected


#
# Tests for extension tags
#


@pytest.mark.parametrize('payload, expected', [
Example #4
0
                                             (u'\u00fc', '62c3bc'),
                                             (u'\u6c34', '63e6b0b4')])
def test_string(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected


@pytest.mark.parametrize('value, expected', [(False, 'f4'), (True, 'f5'),
                                             (None, 'f6'), (undefined, 'f7')],
                         ids=['false', 'true', 'null', 'undefined'])
def test_special(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected


@pytest.mark.parametrize('value, expected', [(CBORSimpleValue(0), 'e0'),
                                             (CBORSimpleValue(2), 'e2'),
                                             (CBORSimpleValue(19), 'f3'),
                                             (CBORSimpleValue(32), 'f820')])
def test_simple_value(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected


#
# Tests for extension tags
#


@pytest.mark.parametrize(
    'value, as_timestamp, expected',
Example #5
0
def decode_simple_value(decoder, shareable_index=None):
    return CBORSimpleValue(struct.unpack('>B', decoder.read(1))[0])
Example #6
0
    assert dumps(value) == expected


@pytest.mark.parametrize('value, expected', [
    (False, 'f4'),
    (True, 'f5'),
    (None, 'f6'),
    (undefined, 'f7')
], ids=['false', 'true', 'null', 'undefined'])
def test_special(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected


@pytest.mark.parametrize('value, expected', [
    (CBORSimpleValue(0), 'e0'),
    (CBORSimpleValue(2), 'e2'),
    (CBORSimpleValue(19), 'f3'),
    (CBORSimpleValue(32), 'f820')
])
def test_simple_value(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected


#
# Tests for extension tags
#

@pytest.mark.parametrize('value, as_timestamp, expected', [
    (datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc), False,
Example #7
0
def test_simple_value_repr():
    assert repr(CBORSimpleValue(1)) == "CBORSimpleValue(1)"