Package lepl :: Package matchers :: Package _test :: Module separators
[hide private]
[frames] | no frames]

Source Code for Module lepl.matchers._test.separators

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Tests for the lepl.matchers.separators module. 
 32  ''' 
 33   
 34  #from logging import basicConfig, DEBUG 
 35  from unittest import TestCase 
 36   
 37  from lepl import And, Optional, Space, Separator, SmartSeparator1, \ 
 38      SmartSeparator2, Eos 
 39   
 40   
 41  #basicConfig(level=DEBUG) 
 42  PRINT = False 
 43   
 44   
 45  STREAMS_3 = ['a b c ', 
 46             'a b c', 
 47             ' b c', 
 48             'b c', 
 49             'ab c', 
 50             'a c', 
 51             'a  c', 
 52             'c', 
 53             ' c', 
 54             '  c'] 
 55   
 56  STREAMS_2 = ['a b ', 
 57               'a b', 
 58               'ab', 
 59               ' b', 
 60               'b', 
 61               'a ', 
 62               'a', 
 63               '', 
 64               ' '] 
 65   
 66   
67 -class AbcSeparatorTest(TestCase):
68
69 - def _assert(self, separator, expecteds, streams=STREAMS_3):
70 self._assert_null(separator, expecteds, streams) 71 self._assert_string(separator, expecteds, streams)
72
73 - def _assert_null(self, separator, expecteds, streams=STREAMS_3):
74 with separator: 75 parser = And(Optional('a') & Optional('b') & 'c', Eos()) 76 ok = True 77 parser.config.no_full_first_match() 78 for (stream, expected) in zip(streams, expecteds): 79 parsed = parser.parse(stream) is not None 80 if PRINT: 81 print('{0!r:9} : {1!r:5} {2!r:5}' 82 .format(stream, parsed, parsed == expected)) 83 ok = ok and (parsed == expected) 84 assert ok
85
86 - def _assert_string(self, separator, expecteds, streams=STREAMS_3):
87 with separator: 88 parser = And(Optional('a') & Optional('b') & 'c', Eos()) 89 ok = True 90 parser.config.no_full_first_match() 91 for (stream, expected) in zip(streams, expecteds): 92 parsed = parser.parse_string(stream) is not None 93 if PRINT: 94 print('{0!r:9} : {1!r:5} {2!r:5}' 95 .format(stream, parsed, parsed == expected)) 96 ok = ok and (parsed == expected) 97 assert ok
98
99 - def test_separator(self):
100 if PRINT: 101 print("\nSeparator(Space())") 102 self._assert(Separator(Space()), 103 [False, True, True, False, False, False, True, False, False, True]) 104 if PRINT: 105 print("\nSeparator(Space()[:])") 106 self._assert(Separator(Space()[:]), 107 [False, True, True, True, True, True, True, True, True, True])
108
109 - def test_separator1(self):
110 if PRINT: 111 print("\nSmartSeparator1(Space())") 112 self._assert(SmartSeparator1(Space()), 113 [False, True, False, True, False, True, False, True, False, False]) 114 if PRINT: 115 print("\nSmartSeparator1(Space()[:])") 116 self._assert(SmartSeparator1(Space()[:]), 117 [False, True, False, True, True, True, True, True, False, False])
118
119 - def test_separator2(self):
120 if PRINT: 121 print("\nSmartSeparator2(Space())") 122 self._assert(SmartSeparator2(Space()), 123 [False, True, False, True, False, True, False, True, False, False]) 124 if PRINT: 125 print("\nSmartSeparator2(Space()[:])") 126 self._assert(SmartSeparator2(Space()[:]), 127 [False, True, False, True, True, True, True, True, False, False])
128 129
130 -class AbSeparatorTest(TestCase):
131
132 - def _assert(self, separator, expecteds, streams=STREAMS_2):
133 self._assert_null(separator, expecteds, streams) 134 self._assert_string(separator, expecteds, streams)
135
136 - def _assert_null(self, separator, expecteds, streams=STREAMS_2):
137 with separator: 138 parser = And(Optional('a') & Optional('b'), Eos()) 139 ok = True 140 parser.config.no_full_first_match() 141 for (stream, expected) in zip(streams, expecteds): 142 parsed = parser.parse(stream) is not None 143 if PRINT: 144 print('{0!r:9} : {1!r:5} {2!r:5}' 145 .format(stream, parsed, parsed == expected)) 146 ok = ok and (parsed == expected) 147 assert ok
148
149 - def _assert_string(self, separator, expecteds, streams=STREAMS_2):
150 with separator: 151 parser = And(Optional('a') & Optional('b'), Eos()) 152 ok = True 153 parser.config.no_full_first_match() 154 for (stream, expected) in zip(streams, expecteds): 155 parsed = parser.parse_string(stream) is not None 156 if PRINT: 157 print('{0!r:9} : {1!r:5} {2!r:5}' 158 .format(stream, parsed, parsed == expected)) 159 ok = ok and (parsed == expected) 160 assert ok
161
162 - def test_separator(self):
163 if PRINT: 164 print("\nSeparator(Space())") 165 self._assert(Separator(Space()), 166 [False, True, False, True, False, True, False, False, True]) 167 if PRINT: 168 print("\nSeparator(Space()[:])") 169 self._assert(Separator(Space()[:]), 170 [False, True, True, True, True, True, True, True, True])
171
172 - def test_separator1(self):
173 if PRINT: 174 print("\nSmartSeparator1(Space())") 175 self._assert(SmartSeparator1(Space()), 176 [False, True, False, False, True, False, True, True, False]) 177 if PRINT: 178 print("\nSmartSeparator1(Space()[:])") 179 self._assert(SmartSeparator1(Space()[:]), 180 [False, True, True, False, True, False, True, True, False])
181
182 - def test_separator2(self):
183 if PRINT: 184 print("\nSmartSeparator2(Space())") 185 self._assert(SmartSeparator2(Space()), 186 [False, True, False, False, True, False, True, True, False]) 187 if PRINT: 188 print("\nSmartSeparator2(Space()[:])") 189 self._assert(SmartSeparator2(Space()[:]), 190 [False, True, True, False, True, False, True, True, False])
191 192
193 -class AbcEosSeparatorTest(TestCase):
194
195 - def _assert(self, separator, expecteds, streams=STREAMS_3):
196 self._assert_null(separator, expecteds, streams) 197 self._assert_string(separator, expecteds, streams)
198
199 - def _assert_null(self, separator, expecteds, streams=STREAMS_3):
200 with separator: 201 parser = Optional('a') & Optional('b') & 'c' & Eos() 202 ok = True 203 parser.config.no_full_first_match() 204 for (stream, expected) in zip(streams, expecteds): 205 parsed = parser.parse(stream) is not None 206 if PRINT: 207 print('{0!r:9} : {1!r:5} {2!r:5}' 208 .format(stream, parsed, parsed == expected)) 209 ok = ok and (parsed == expected) 210 assert ok
211
212 - def _assert_string(self, separator, expecteds, streams=STREAMS_3):
213 with separator: 214 parser = Optional('a') & Optional('b') & 'c' & Eos() 215 ok = True 216 parser.config.no_full_first_match() 217 for (stream, expected) in zip(streams, expecteds): 218 parsed = parser.parse_string(stream) is not None 219 if PRINT: 220 print('{0!r:9} : {1!r:5} {2!r:5}' 221 .format(stream, parsed, parsed == expected)) 222 ok = ok and (parsed == expected) 223 assert ok
224
225 - def test_separator(self):
226 if PRINT: 227 print("\nSeparator(Space())") 228 self._assert(Separator(Space()), 229 [True, False, False, False, False, False, False, False, False, False]) 230 if PRINT: 231 print("\nSeparator(Space()[:])") 232 self._assert(Separator(Space()[:]), 233 [True, True, True, True, True, True, True, True, True, True])
234
235 - def test_separator1(self):
236 if PRINT: 237 print("\nSmartSeparator1(Space())") 238 self._assert(SmartSeparator1(Space()), 239 [False, True, False, True, False, True, False, True, False, False]) 240 if PRINT: 241 print("\nSmartSeparator1(Space()[:])") 242 self._assert(SmartSeparator1(Space()[:]), 243 [False, True, False, True, True, True, True, True, False, False])
244
245 - def test_separator2(self):
246 if PRINT: 247 print("\nSmartSeparator2(Space())") 248 self._assert(SmartSeparator2(Space()), 249 [True, False, False, False, False, False, False, False, False, False]) 250 if PRINT: 251 print("\nSmartSeparator2(Space()[:])") 252 self._assert(SmartSeparator2(Space()[:]), 253 [True, True, False, True, True, True, True, True, False, False])
254 255
256 -class AbEosSeparatorTest(TestCase):
257
258 - def _assert(self, separator, expecteds, streams=STREAMS_2):
259 self._assert_null(separator, expecteds, streams) 260 self._assert_string(separator, expecteds, streams)
261
262 - def _assert_null(self, separator, expecteds, streams=STREAMS_2):
263 with separator: 264 parser = Optional('a') & Optional('b') & Eos() 265 ok = True 266 parser.config.no_full_first_match() 267 for (stream, expected) in zip(streams, expecteds): 268 parsed = parser.parse(stream) is not None 269 if PRINT: 270 print('{0!r:9} : {1!r:5} {2!r:5}' 271 .format(stream, parsed, parsed == expected)) 272 ok = ok and (parsed == expected) 273 assert ok
274
275 - def _assert_string(self, separator, expecteds, streams=STREAMS_2):
276 with separator: 277 parser = Optional('a') & Optional('b') & Eos() 278 ok = True 279 parser.config.no_full_first_match() 280 for (stream, expected) in zip(streams, expecteds): 281 parsed = parser.parse(stream) is not None 282 if PRINT: 283 print('{0!r:9} : {1!r:5} {2!r:5}' 284 .format(stream, parsed, parsed == expected)) 285 ok = ok and (parsed == expected) 286 assert ok
287
288 - def test_separator(self):
289 if PRINT: 290 print("\nSeparator(Space())") 291 self._assert(Separator(Space()), 292 [True, False, False, False, False, False, False, False, False]) 293 if PRINT: 294 print("\nSeparator(Space()[:])") 295 self._assert(Separator(Space()[:]), 296 [True, True, True, True, True, True, True, True, True])
297
298 - def test_separator1(self):
299 if PRINT: 300 print("\nSmartSeparator1(Space())") 301 self._assert(SmartSeparator1(Space()), 302 [False, True, False, False, True, False, True, True, False]) 303 if PRINT: 304 print("\nSmartSeparator1(Space()[:])") 305 self._assert(SmartSeparator1(Space()[:]), 306 [False, True, True, False, True, False, True, True, False])
307
308 - def test_separator2(self):
309 if PRINT: 310 print("\nSmartSeparator2(Space())") 311 self._assert(SmartSeparator2(Space()), 312 [True, False, False, False, False, True, False, True, False]) 313 if PRINT: 314 print("\nSmartSeparator2(Space()[:])") 315 self._assert(SmartSeparator2(Space()[:]), 316 [True, True, True, False, True, True, True, True, False])
317