~prime8/waferslim/main

« back to all changes in this revision

Viewing changes to src/waferslim/specs/protocol_spec.py

  • Committer: tim
  • Date: 2009-02-22 08:36:20 UTC
  • Revision ID: tim@lightning-20090222083620-325qbf73jn8aelm9
Initial check-in: basic protocol behaviour

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
BDD-style Lancelot specifications for the behaviour of the core library classes
 
3
'''
 
4
 
 
5
import lancelot
 
6
from waferslim.protocol import SlimProtocol, UnpackingError
 
7
 
 
8
SAMPLE_DATA = [
 
9
               ([],                 '[000000:]'),
 
10
               (['hello'],          '[000001:000005:hello:]'),
 
11
               (['hello','world'],  '[000002:000005:hello:000005:world:]'),
 
12
               ([['element']],      '[000001:000024:[000001:000007:element:]:]')
 
13
              ]
 
14
 
 
15
@lancelot.grouping
 
16
class PackBehaviour:
 
17
    ''' Group of specs for protocol pack() behaviour '''
 
18
    
 
19
    @lancelot.verifiable
 
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)
 
30
            
 
31
    @lancelot.verifiable
 
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?!
 
38
 
 
39
@lancelot.grouping
 
40
class UnpackBehaviour:
 
41
    ''' Group of specs for protocol unpack() behaviour '''
 
42
    
 
43
    @lancelot.verifiable
 
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'))
 
50
        
 
51
    @lancelot.verifiable
 
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 '['"))
 
62
        
 
63
    @lancelot.verifiable
 
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)
 
69
 
 
70
if __name__ == '__main__':
 
71
    lancelot.verify()
 
 
b'\\ No newline at end of file'