13
13
14
14
testingGrammarSource = """
15
15
16
- someA = ('a' 'a') -> state ('a')
17
- someB = ('b' 'b') -> state ('b')
18
- someC = ('c' 'c') -> state ('c')
19
- someExc = 'e' -> state .raiseSomething()
16
+ someA = ('a' 'a') -> receiver ('a')
17
+ someB = ('b' 'b') -> receiver ('b')
18
+ someC = ('c' 'c') -> receiver ('c')
19
+ someExc = 'e' -> receiver .raiseSomething()
20
20
21
21
initial = someA | someExc
22
22
@@ -33,7 +33,7 @@ class SomeException(Exception):
33
33
pass
34
34
35
35
36
- class StateFactory (object ):
36
+ class ReceiverFactory (object ):
37
37
def __init__ (self , sender , parser ):
38
38
self .sender = sender
39
39
self .parser = parser
@@ -69,7 +69,7 @@ class ParserProtocolTestCase(unittest.TestCase):
69
69
70
70
def setUp (self ):
71
71
self .protocol = ParserProtocol (
72
- testGrammar , SenderFactory , StateFactory , {})
72
+ testGrammar , SenderFactory , ReceiverFactory , {})
73
73
74
74
def test_transportPassed (self ):
75
75
"""The sender is passed the transport recieved by the protocol."""
@@ -78,93 +78,93 @@ def test_transportPassed(self):
78
78
self .assertEqual (transport , self .protocol .sender .transport )
79
79
80
80
def test_parserPassed (self ):
81
- """The protocol is passed to the state ."""
81
+ """The protocol is passed to the receiver ."""
82
82
transport = object ()
83
83
self .protocol .makeConnection (transport )
84
- self .assertEqual (self .protocol , self .protocol .state .parser )
84
+ self .assertEqual (self .protocol , self .protocol .receiver .parser )
85
85
86
86
def test_senderPassed (self ):
87
- """The sender is passed to the state ."""
87
+ """The sender is passed to the receiver ."""
88
88
self .protocol .makeConnection (None )
89
- self .assertEqual (self .protocol .sender , self .protocol .state .sender )
89
+ self .assertEqual (self .protocol .sender , self .protocol .receiver .sender )
90
90
91
91
def test_connectionEstablishes (self ):
92
- """connectionMade is called on the state after connection establishment."""
92
+ """connectionMade is called on the receiver after connection establishment."""
93
93
self .protocol .makeConnection (None )
94
- self .assert_ (self .protocol .state .connected )
94
+ self .assert_ (self .protocol .receiver .connected )
95
95
96
96
def test_basicParsing (self ):
97
97
"""Rules can be parsed multiple times for the same effect."""
98
98
self .protocol .makeConnection (None )
99
99
self .protocol .dataReceived ('aa' )
100
- self .assertEqual (self .protocol .state .calls , ['a' ])
100
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
101
101
self .protocol .dataReceived ('aa' )
102
- self .assertEqual (self .protocol .state .calls , ['a' , 'a' ])
102
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'a' ])
103
103
104
104
def test_parsingChunks (self ):
105
105
"""Any number of rules can be called from one dataRecived."""
106
106
self .protocol .makeConnection (None )
107
107
self .protocol .dataReceived ('a' )
108
- self .assertEqual (self .protocol .state .calls , [])
108
+ self .assertEqual (self .protocol .receiver .calls , [])
109
109
self .protocol .dataReceived ('aa' )
110
- self .assertEqual (self .protocol .state .calls , ['a' ])
110
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
111
111
self .protocol .dataReceived ('aaa' )
112
- self .assertEqual (self .protocol .state .calls , ['a' , 'a' , 'a' ])
112
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'a' , 'a' ])
113
113
114
114
def test_ruleSwitching (self ):
115
115
"""The rule being parsed can specify the next rule to be parsed."""
116
116
self .protocol .makeConnection (None )
117
- self .protocol .state .returnMap .update (dict (a = 'someB' , b = 'someA' ))
117
+ self .protocol .receiver .returnMap .update (dict (a = 'someB' , b = 'someA' ))
118
118
self .protocol .dataReceived ('aa' )
119
- self .assertEqual (self .protocol .state .calls , ['a' ])
119
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
120
120
self .protocol .dataReceived ('bb' )
121
- self .assertEqual (self .protocol .state .calls , ['a' , 'b' ])
121
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'b' ])
122
122
self .protocol .dataReceived ('aa' )
123
- self .assertEqual (self .protocol .state .calls , ['a' , 'b' , 'a' ])
123
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'b' , 'a' ])
124
124
125
125
def test_ruleSwitchingWithChunks (self ):
126
126
"""Any number of rules can be called even during rule switching."""
127
127
self .protocol .makeConnection (None )
128
- self .protocol .state .returnMap .update (dict (a = 'someB' , b = 'someA' ))
128
+ self .protocol .receiver .returnMap .update (dict (a = 'someB' , b = 'someA' ))
129
129
self .protocol .dataReceived ('a' )
130
- self .assertEqual (self .protocol .state .calls , [])
130
+ self .assertEqual (self .protocol .receiver .calls , [])
131
131
self .protocol .dataReceived ('ab' )
132
- self .assertEqual (self .protocol .state .calls , ['a' ])
132
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
133
133
self .protocol .dataReceived ('baa' )
134
- self .assertEqual (self .protocol .state .calls , ['a' , 'b' , 'a' ])
134
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'b' , 'a' ])
135
135
136
- def test_ruleSwitchingViaState (self ):
136
+ def test_ruleSwitchingViaReceiver (self ):
137
137
"""
138
- The state is able to set the the next rule to be parsed with the parser
139
- passed to it.
138
+ The receiver is able to set the the next rule to be parsed with the
139
+ parser passed to it.
140
140
"""
141
141
self .protocol .makeConnection (None )
142
142
self .protocol .dataReceived ('aa' )
143
- self .assertEqual (self .protocol .state .calls , ['a' ])
143
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
144
144
self .protocol .dataReceived ('a' )
145
- self .assertEqual (self .protocol .state .calls , ['a' ])
146
- self .protocol .state .parser .setNextRule ('someB' )
145
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
146
+ self .protocol .receiver .parser .setNextRule ('someB' )
147
147
self .protocol .dataReceived ('abb' )
148
- self .assertEqual (self .protocol .state .calls , ['a' , 'a' , 'b' ])
148
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'a' , 'b' ])
149
149
150
- def test_ruleSwitchingViaStateGetsOverridden (self ):
150
+ def test_ruleSwitchingViaReceiverGetsOverridden (self ):
151
151
"""Returning a new rule takes priority over calling setNextRule."""
152
152
self .protocol .makeConnection (None )
153
153
self .protocol .dataReceived ('aa' )
154
- self .assertEqual (self .protocol .state .calls , ['a' ])
154
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
155
155
self .protocol .dataReceived ('a' )
156
- self .assertEqual (self .protocol .state .calls , ['a' ])
157
- self .protocol .state .parser .setNextRule ('someB' )
158
- self .protocol .state .returnMap ['a' ] = 'someC'
156
+ self .assertEqual (self .protocol .receiver .calls , ['a' ])
157
+ self .protocol .receiver .parser .setNextRule ('someB' )
158
+ self .protocol .receiver .returnMap ['a' ] = 'someC'
159
159
self .protocol .dataReceived ('acc' )
160
- self .assertEqual (self .protocol .state .calls , ['a' , 'a' , 'c' ])
160
+ self .assertEqual (self .protocol .receiver .calls , ['a' , 'a' , 'c' ])
161
161
162
162
def test_connectionLoss (self ):
163
- """The reason for connection loss is forwarded to the state ."""
163
+ """The reason for connection loss is forwarded to the receiver ."""
164
164
self .protocol .makeConnection (None )
165
165
reason = object ()
166
166
self .protocol .connectionLost (reason )
167
- self .assertEqual (self .protocol .state .lossReason , reason )
167
+ self .assertEqual (self .protocol .receiver .lossReason , reason )
168
168
169
169
def test_parseFailure (self ):
170
170
"""
@@ -174,21 +174,21 @@ def test_parseFailure(self):
174
174
transport = FakeTransport ()
175
175
self .protocol .makeConnection (transport )
176
176
self .protocol .dataReceived ('b' )
177
- self .failIfEqual (self .protocol .state .lossReason , None )
178
- self .failUnlessIsInstance (self .protocol .state .lossReason .value ,
177
+ self .failIfEqual (self .protocol .receiver .lossReason , None )
178
+ self .failUnlessIsInstance (self .protocol .receiver .lossReason .value ,
179
179
ParseError )
180
180
self .assert_ (transport .aborted )
181
181
182
- def test_exceptionsRaisedFromState (self ):
182
+ def test_exceptionsRaisedFromReceiver (self ):
183
183
"""
184
- Raising an exception from state methods called from the grammar
184
+ Raising an exception from receiver methods called from the grammar
185
185
propagate to connectionLost.
186
186
"""
187
187
transport = FakeTransport ()
188
188
self .protocol .makeConnection (transport )
189
189
self .protocol .dataReceived ('e' )
190
- self .failIfEqual (self .protocol .state .lossReason , None )
191
- self .failUnlessIsInstance (self .protocol .state .lossReason .value ,
190
+ self .failIfEqual (self .protocol .receiver .lossReason , None )
191
+ self .failUnlessIsInstance (self .protocol .receiver .lossReason .value ,
192
192
SomeException )
193
193
self .assert_ (transport .aborted )
194
194
@@ -199,5 +199,5 @@ def test_dataIgnoredAfterDisconnection(self):
199
199
reason = object ()
200
200
self .protocol .connectionLost (reason )
201
201
self .protocol .dataReceived ('d' )
202
- self .assertEqual (self .protocol .state .lossReason , reason )
202
+ self .assertEqual (self .protocol .receiver .lossReason , reason )
203
203
self .assert_ (not transport .aborted )
0 commit comments