~ubuntu-branches/debian/sid/pyrlp/sid

« back to all changes in this revision

Viewing changes to tests/test_countablelist.py

  • Committer: Package Import Robot
  • Author(s): Ben Finney
  • Date: 2017-07-15 05:25:42 UTC
  • Revision ID: package-import@ubuntu.com-20170715052542-a20zzsypt1qfecq1
Tags: upstream-0.5.1
ImportĀ upstreamĀ versionĀ 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import unicode_literals
 
2
 
 
3
import pytest
 
4
import rlp
 
5
from rlp.sedes import big_endian_int
 
6
from rlp.sedes.lists import CountableList
 
7
from rlp import SerializationError, DeserializationError
 
8
 
 
9
 
 
10
def test_countable_list():
 
11
    l1 = CountableList(big_endian_int)
 
12
    serializable = [(), (1, 2), tuple(range(500))]
 
13
    for s in serializable:
 
14
        r = l1.serialize(s)
 
15
        assert l1.deserialize(r) == s
 
16
    not_serializable = ([1, 'asdf'], ['asdf'], [1, [2]], [[]])
 
17
    for n in not_serializable:
 
18
        with pytest.raises(SerializationError):
 
19
            l1.serialize(n)
 
20
 
 
21
    l2 = CountableList(CountableList(big_endian_int))
 
22
    serializable = ((), ((),), ((1, 2, 3), (4,)), ((5,), (6, 7, 8)), ((), (),
 
23
                    (9, 0)))
 
24
    for s in serializable:
 
25
        r = l2.serialize(s)
 
26
        assert l2.deserialize(r) == s
 
27
    not_serializable = ([[[]]], [1, 2], [1, ['asdf'], ['fdsa']])
 
28
    for n in not_serializable:
 
29
        with pytest.raises(SerializationError):
 
30
            l2.serialize(n)
 
31
 
 
32
    l3 = CountableList(big_endian_int, max_length=3)
 
33
    serializable = [(), (1,), (1, 2), (1, 2, 3)]
 
34
    for s in serializable:
 
35
        r = l3.serialize(s)
 
36
        assert r == l1.serialize(s)
 
37
        assert l3.deserialize(r) == s
 
38
    not_serializable = [(1, 2, 3, 4), (1, 2, 3, 4, 5, 6, 7), range(500)]
 
39
    for s in not_serializable:
 
40
        with pytest.raises(SerializationError):
 
41
            l3.serialize(s)
 
42
        r = l1.serialize(s)
 
43
        with pytest.raises(DeserializationError):
 
44
            l3.deserialize(r)
 
45
        ll = rlp.decode_lazy(rlp.encode(r))
 
46
        with pytest.raises(DeserializationError):
 
47
            l3.deserialize(ll)
 
48
        assert len(ll._elements) == 3 + 1  # failed early, did not consume fully