@@ -34,9 +34,10 @@ class SomeException(Exception):
34
34
35
35
36
36
class ReceiverFactory (object ):
37
- def __init__ (self , sender , parser ):
37
+ currentRule = 'initial'
38
+
39
+ def __init__ (self , sender ):
38
40
self .sender = sender
39
- self .parser = parser
40
41
self .calls = []
41
42
self .returnMap = {}
42
43
self .connected = False
@@ -47,7 +48,8 @@ def prepareParsing(self):
47
48
48
49
def __call__ (self , v ):
49
50
self .calls .append (v )
50
- return self .returnMap .get (v )
51
+ if v in self .returnMap :
52
+ self .currentRule = self .returnMap [v ]
51
53
52
54
def raiseSomething (self ):
53
55
raise SomeException ()
@@ -77,12 +79,6 @@ def test_transportPassed(self):
77
79
self .protocol .makeConnection (transport )
78
80
self .assertEqual (transport , self .protocol .sender .transport )
79
81
80
- def test_parserPassed (self ):
81
- """The protocol is passed to the receiver."""
82
- transport = object ()
83
- self .protocol .makeConnection (transport )
84
- self .assertEqual (self .protocol , self .protocol .receiver .parser )
85
-
86
82
def test_senderPassed (self ):
87
83
"""The sender is passed to the receiver."""
88
84
self .protocol .makeConnection (None )
@@ -133,29 +129,17 @@ def test_ruleSwitchingWithChunks(self):
133
129
self .protocol .dataReceived ('baa' )
134
130
self .assertEqual (self .protocol .receiver .calls , ['a' , 'b' , 'a' ])
135
131
136
- def test_ruleSwitchingViaReceiver (self ):
132
+ def test_rulesCannotBeSwitchedDuringParsing (self ):
137
133
"""
138
- The receiver is able to set the the next rule to be parsed with the
139
- parser passed to it .
134
+ One can set a new rule during parsing, but it won't change the rule
135
+ currently being parsed .
140
136
"""
141
137
self .protocol .makeConnection (None )
142
138
self .protocol .dataReceived ('aa' )
143
139
self .assertEqual (self .protocol .receiver .calls , ['a' ])
144
140
self .protocol .dataReceived ('a' )
145
141
self .assertEqual (self .protocol .receiver .calls , ['a' ])
146
- self .protocol .receiver .parser .setNextRule ('someB' )
147
- self .protocol .dataReceived ('abb' )
148
- self .assertEqual (self .protocol .receiver .calls , ['a' , 'a' , 'b' ])
149
-
150
- def test_ruleSwitchingViaReceiverGetsOverridden (self ):
151
- """Returning a new rule takes priority over calling setNextRule."""
152
- self .protocol .makeConnection (None )
153
- self .protocol .dataReceived ('aa' )
154
- self .assertEqual (self .protocol .receiver .calls , ['a' ])
155
- self .protocol .dataReceived ('a' )
156
- self .assertEqual (self .protocol .receiver .calls , ['a' ])
157
- self .protocol .receiver .parser .setNextRule ('someB' )
158
- self .protocol .receiver .returnMap ['a' ] = 'someC'
142
+ self .protocol .receiver .currentRule = 'someC'
159
143
self .protocol .dataReceived ('acc' )
160
144
self .assertEqual (self .protocol .receiver .calls , ['a' , 'a' , 'c' ])
161
145
0 commit comments