@@ -21,12 +21,17 @@ def __init__(self, transport):
21
21
22
22
23
23
class StateFactory (object ):
24
- def __init__ (self , sender ):
24
+ def __init__ (self , sender , parser ):
25
25
self .sender = sender
26
+ self .parser = parser
26
27
self .calls = []
27
28
self .returnMap = {}
29
+ self .connected = False
28
30
self .lossReason = None
29
31
32
+ def connectionMade (self ):
33
+ self .connected = True
34
+
30
35
def __call__ (self , v ):
31
36
self .calls .append (v )
32
37
return self .returnMap .get (v )
@@ -46,11 +51,22 @@ def test_transportPassed(self):
46
51
self .protocol .makeConnection (transport )
47
52
self .assertEqual (transport , self .protocol .sender .transport )
48
53
54
+ def test_parserPassed (self ):
55
+ """The protocol is passed to the state."""
56
+ transport = object ()
57
+ self .protocol .makeConnection (transport )
58
+ self .assertEqual (self .protocol , self .protocol .state .parser )
59
+
49
60
def test_senderPassed (self ):
50
61
"""The sender is passed to the state."""
51
62
self .protocol .makeConnection (None )
52
63
self .assertEqual (self .protocol .sender , self .protocol .state .sender )
53
64
65
+ def test_connectionEstablishes (self ):
66
+ """connectionMade is called on the state after connection establishment."""
67
+ self .protocol .makeConnection (None )
68
+ self .assert_ (self .protocol .state .connected )
69
+
54
70
def test_basicParsing (self ):
55
71
"""Rules can be parsed multiple times for the same effect."""
56
72
self .protocol .makeConnection (None )
@@ -91,6 +107,20 @@ def test_ruleSwitchingWithChunks(self):
91
107
self .protocol .dataReceived ('baa' )
92
108
self .assertEqual (self .protocol .state .calls , ['a' , 'b' , 'a' ])
93
109
110
+ def test_ruleSwitchingViaState (self ):
111
+ """
112
+ The state is able to set the the next rule to be parsed with the parser
113
+ passed to it.
114
+ """
115
+ self .protocol .makeConnection (None )
116
+ self .protocol .dataReceived ('aa' )
117
+ self .assertEqual (self .protocol .state .calls , ['a' ])
118
+ self .protocol .dataReceived ('a' )
119
+ self .assertEqual (self .protocol .state .calls , ['a' ])
120
+ self .protocol .state .parser .setNextRule ('someB' )
121
+ self .protocol .dataReceived ('abb' )
122
+ self .assertEqual (self .protocol .state .calls , ['a' , 'a' , 'b' ])
123
+
94
124
def test_connectionLoss (self ):
95
125
"""The reason for connection loss is forwarded to the state."""
96
126
self .protocol .makeConnection (None )
0 commit comments