2
BDD-style Lancelot specifications for the behaviour of the core library classes
6
from waferslim.protocol import SlimProtocol, UnpackingError
10
(['hello'], '[000001:000005:hello:]'),
11
(['hello','world'], '[000002:000005:hello:000005:world:]'),
12
([['element']], '[000001:000024:[000001:000007:element:]:]')
17
''' Group of specs for protocol pack() behaviour '''
20
def items_length_item_format(self):
21
''' Encoding as described in fitnesse.slim.ListSerializer Javadoc:
22
Format: [iiiiii:llllll:item...]
23
All lists (including lists within lists) begin with [ and end with ].
24
After the [ is the 6 digit number of items in the list followed by a :.
25
Then comes each item which is composed of a 6 digit length a : and
26
then the value of the item followed by a :. '''
27
spec = lancelot.Spec(SlimProtocol())
28
for unpacked, packed in SAMPLE_DATA:
29
spec.pack(unpacked).should_be(packed)
32
def pack_non_strings(self):
33
''' Use str() co-ercion for encoding non-string values, except for
34
None which encodes as "null" '''
35
spec = lancelot.Spec(SlimProtocol())
36
spec.pack([1]).should_be('[000001:000001:1:]')
37
spec.pack([None]).should_be('[000001:000004:null:]') #TODO: check this?!
40
class UnpackBehaviour:
41
''' Group of specs for protocol unpack() behaviour '''
44
def unpack_strings_only(self):
45
''' Unpacking a non-string should raise an error '''
46
spec = lancelot.Spec(SlimProtocol())
47
spec.unpack(None).should_raise(TypeError('None is not a string'))
48
spec.unpack(1).should_raise(TypeError('1 is not a string'))
49
spec.unpack([]).should_raise(TypeError('[] is not a string'))
52
def require_square_brackets(self):
53
''' Unpacking a string without a leading square bracket,
54
or a string without an ending square bracket should raise an error '''
55
spec = lancelot.Spec(SlimProtocol())
56
spec.unpack('').should_raise(
57
UnpackingError("'' has no leading '['"))
58
spec.unpack('[hello').should_raise(
59
UnpackingError("'[hello' has no trailing ']'"))
60
spec.unpack('hello]').should_raise(
61
UnpackingError("'hello]' has no leading '['"))
64
def items_length_item_format(self):
65
''' Unpacking should reverse the encoding process '''
66
spec = lancelot.Spec(SlimProtocol())
67
for unpacked, packed in SAMPLE_DATA:
68
spec.unpack(packed).should_be(unpacked)
70
if __name__ == '__main__':
b'\\ No newline at end of file'