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

Source Code for Module lepl.matchers._test.derived

  1  from lepl.stream.core import s_next 
  2   
  3  # The contents of this file are subject to the Mozilla Public License 
  4  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  5  # in compliance with the License. You may obtain a copy of the License 
  6  # at http://www.mozilla.org/MPL/ 
  7  # 
  8  # Software distributed under the License is distributed on an "AS IS" 
  9  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 10  # the License for the specific language governing rights and 
 11  # limitations under the License. 
 12  #LogMixin 
 13  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 14  # The Initial Developer of the Original Code is Andrew Cooke. 
 15  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 16  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 17  # 
 18  # Alternatively, the contents of this file may be used under the terms 
 19  # of the LGPL license (the GNU Lesser General Public License, 
 20  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 21  # of the LGPL License are applicable instead of those above. 
 22  # 
 23  # If you wish to allow use of your version of this file only under the 
 24  # terms of the LGPL License and not to allow others to use your version 
 25  # of this file under the MPL, indicate your decision by deleting the 
 26  # provisions above and replace them with the notice and other provisions 
 27  # required by the LGPL License.  If you do not delete the provisions 
 28  # above, a recipient may use your version of this file under either the 
 29  # MPL or the LGPL License. 
 30   
 31  ''' 
 32  Tests for the lepl.matchers.derived module. 
 33  ''' 
 34   
 35  #from logging import basicConfig, DEBUG 
 36  from unittest import TestCase 
 37   
 38  from lepl.matchers.monitor import Trace 
 39  from lepl.matchers.derived import Repeat, Space, SkipTo, Newline, String, \ 
 40      SkipString, SingleLineString, UnsignedInteger, SignedInteger, \ 
 41      UnsignedReal, SignedReal, UnsignedEReal, SignedEReal, UnsignedFloat, \ 
 42      SignedFloat, UnsignedEFloat, SignedEFloat, Optional, Add, Reduce, Star 
 43  from lepl.stream.maxdepth import FullFirstMatchException 
 44  from lepl.matchers.core import Any 
 45  from lepl._test.base import BaseTest 
 46  from lepl.matchers.support import OperatorMatcher 
 47  from lepl.core.parser import tagged 
 48  from lepl.support.warn import silence 
 49   
 50  silence('UnsignedFloat') 
 51  silence('SignedFloat') 
 52  silence('UnsignedEFloat') 
 53  silence('SignedEFloat') 
54 55 # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102, C0321, W0141, R0201, R0913, R0901, R0904 56 # (dude this is just a test) 57 58 -class RepeatTest(TestCase):
59
60 - def test_simple(self):
61 #basicConfig(level=DEBUG) 62 self.assert_simple([1], 1, 1, 'd', ['0']) 63 self.assert_simple([1], 1, 2, 'd', ['0']) 64 self.assert_simple([2], 1, 1, 'd', ['0','1']) 65 self.assert_simple([2], 1, 2, 'd', ['0','1']) 66 self.assert_simple([2], 0, 2, 'd', ['0','1','']) 67 self.assert_simple([1,2], 1, 1, 'd', ['0']) 68 self.assert_simple([1,2], 1, 2, 'd', ['00','01','0']) 69 self.assert_simple([1,2], 2, 2, 'd', ['00','01']) 70 self.assert_simple([1,2], 1, 2, 'b', ['0','00','01']) 71 self.assert_simple([1,2], 1, 2, 'g', ['00','01','0'])
72
73 - def assert_simple(self, stream, start, stop, algorithm, target):
74 matcher = Repeat(RangeMatch(), start, stop, algorithm=algorithm) 75 matcher.config.no_full_first_match() 76 result = list(matcher.match_list(stream)) 77 #print(result) 78 result = [''.join(map(str, l)) for (l, _s) in result] 79 assert target == result, result
80
81 - def test_mixin(self):
82 #basicConfig(level=DEBUG) 83 r = RangeMatch() 84 self.assert_mixin(r[1:1], [1], ['0']) 85 self.assert_mixin(r[1:2], [1], ['0']) 86 self.assert_mixin(r[1:1], [2], ['0','1']) 87 self.assert_mixin(r[1:2], [2], ['0','1']) 88 self.assert_mixin(r[0:], [2], ['0','1', '']) 89 self.assert_mixin(r[:], [2], ['0','1', '']) 90 self.assert_mixin(r[0:2], [2], ['0','1', '']) 91 self.assert_mixin(r[1], [1,2], ['0']) 92 self.assert_mixin(r[1:2], [1,2], ['00','01', '0']) 93 self.assert_mixin(r[2], [1,2], ['00','01']) 94 self.assert_mixin(r[1:2:'b'], [1,2], ['0', '00','01']) 95 self.assert_mixin(r[1:2:'d'], [1,2], ['00', '01','0']) 96 try: 97 self.assert_mixin(r[1::'x'], [1,2,3], []) 98 assert False, 'expected error' 99 except KeyError: 100 pass
101
102 - def assert_mixin(self, match, stream, target):
103 match.config.no_full_first_match() 104 result = [''.join(map(str, l)) 105 for (l, _s) in match.match_list(stream)] 106 assert target == result, result
107
108 - def test_separator(self):
109 #basicConfig(level=DEBUG) 110 self.assert_separator('a', 1, 1, 'd', ['a']) 111 self.assert_separator('a', 1, 1, 'b', ['a']) 112 self.assert_separator('a,a', 1, 2, 'd', ['a,a', 'a']) 113 self.assert_separator('a,a', 1, 2, 'b', ['a', 'a,a']) 114 self.assert_separator('a,a,a,a', 2, 3, 'd', ['a,a,a', 'a,a']) 115 self.assert_separator('a,a,a,a', 2, 3, 'b', ['a,a', 'a,a,a'])
116
117 - def assert_separator(self, stream, start, stop, algorithm, target):
118 matcher = Repeat(Any('abc'), start, stop, 119 algorithm=algorithm, separator=Any(',')) 120 matcher.config.no_full_first_match() 121 result = [''.join(l) 122 for (l, _s) in matcher.match_string(stream)] 123 assert target == result, result
124
125 - def test_separator_mixin(self):
126 #basicConfig(level=DEBUG) 127 abc = Any('abc') 128 self.assert_separator_mixin(abc[1:1:'d',','], 'a', ['a']) 129 self.assert_separator_mixin(abc[1:1:'b',','], 'a', ['a']) 130 self.assert_separator_mixin(abc[1:2:'d',','], 'a,b', ['a,b', 'a']) 131 self.assert_separator_mixin(abc[1:2:'b',','], 'a,b', ['a', 'a,b']) 132 self.assert_separator_mixin(abc[2:3:'d',','], 'a,b,c,a', ['a,b,c', 'a,b']) 133 self.assert_separator_mixin(abc[2:3:'b',','], 'a,b,c,a', ['a,b', 'a,b,c'])
134
135 - def assert_separator_mixin(self, matcher, stream, target):
136 matcher.config.no_full_first_match() 137 result = [''.join(map(str, l)) for (l, _s) in matcher.match_string(stream)] 138 assert target == result, result
139
140 141 -class RangeMatch(OperatorMatcher):
142 ''' 143 We test repetition by looking at "strings" of integers, where the 144 matcher for any particular value returns all values less than the 145 current value. 146 ''' 147
148 - def __init__(self):
149 super(RangeMatch, self).__init__()
150 151 @tagged
152 - def _match(self, stream):
153 (value, next_stream) = s_next(stream) 154 for i in range(value[0]): 155 yield ([i], next_stream)
156
157 158 -class AddReduceTest(BaseTest):
159
160 - def test_add(self):
161 self.assert_direct('123', Add(Star(Any())), [['123'], ['12'], ['1'], []])
162
163 - def test_reduce(self):
164 self.assert_direct('123', Reduce(Star(Any()), ''), [['123'], ['12'], ['1'], ['']])
165
166 167 -class SpaceTest(BaseTest):
168
169 - def test_space(self):
170 self.assert_direct(' ', Space(), [[' ']]) 171 self.assert_direct(' ', Space()[0:], [[' ', ' '], [' '], []]) 172 self.assert_direct(' ', Space()[0:,...], [[' '], [' '], []])
173
174 - def test_slash(self):
175 ab = Any('ab') 176 self.assert_direct('ab', ab / ab, [['a', 'b']]) 177 self.assert_direct('a b', ab / ab, [['a', ' ', 'b']]) 178 self.assert_direct('a b', ab / ab, [['a', ' ', 'b']]) 179 self.assert_direct('ab', ab // ab, []) 180 self.assert_direct('a b', ab // ab, [['a', ' ', 'b']]) 181 self.assert_direct('a b', ab // ab, [['a', ' ', 'b']])
182
183 184 -class SkipToTest(BaseTest):
185
186 - def test_skip(self):
187 self.assert_direct('aabcc', SkipTo('b'), [['aab']]) 188 self.assert_direct('aabcc', SkipTo('b', False), [['aa']])
189
190 - def test_next_line(self):
191 #basicConfig(level=DEBUG) 192 self.assert_direct('''abc 193 d''', Trace(~SkipTo(Newline()) + 'd'), [['d']])
194
195 196 -class StringTest(BaseTest):
197
198 - def test_simple(self):
199 self.assert_direct('"abc"', String(), [['abc']]) 200 self.assert_direct('"abc"d', String(), [['abc']]) 201 self.assert_direct('"abc"', SingleLineString(), [['abc']]) 202 self.assert_direct('"abc"d', SingleLineString(), [['abc']]) 203 self.assert_direct('"abc"', SkipString(), [['abc']]) 204 self.assert_direct('"abc"d', SkipString(), [['abc']])
205
206 - def test_empty(self):
207 self.assert_direct('""', String(), [['']]) 208 self.assert_direct("''", String(quote="'"), [['']])
209
210 - def test_escape(self):
211 self.assert_direct('"ab\\"c"', String(), [['ab"c'], ['ab\\']])
212
213 - def test_multiple_lines(self):
214 self.assert_direct('"ab\nc"', String(), [['ab\nc']]) 215 self.assert_direct('"ab\nc"', SingleLineString(), []) 216 self.assert_direct('"ab\nc"', SkipString(), [['abc']])
217
218 - def test_ints(self):
219 self.assert_direct([1,2,3,1], String(quote=[1], empty=[]), [[[2,3]]])
220
221 222 -class OptionalTest(BaseTest):
223
224 - def test_optional(self):
225 self.assert_direct('a', Optional('a'), [['a'], []])
226
227 228 -class NumberTest(BaseTest):
229
230 - def do_test(self, matcher, good, bad):
231 def test(value, ok): 232 try: 233 matcher.parse(value) 234 assert ok, value 235 except FullFirstMatchException: 236 assert not ok, value
237 for value in good: test(value, True) 238 for value in bad: test(value, False)
239
240 - def test_numbers(self):
241 self.do_test(UnsignedInteger(), 242 ['1'], 243 ['-1', 244 '1.', '-1.', 245 '1.2', '-1.2', 246 '1e3', '-1e3', '1e-3', '-1e-3', 247 '1.e3', '-1.e3', '1.e-3', '-1.e-3', 248 '1.2e3', '-1.2e3', '1.2e-3', '-1.2e-3']) 249 self.do_test(SignedInteger(), 250 ['1', '-1'], 251 ['1.', '-1.', 252 '1.2', '-1.2', 253 '1e3', '-1e3', '1e-3', '-1e-3', 254 '1.e3', '-1.e3', '1.e-3', '-1.e-3', 255 '1.2e3', '-1.2e3', '1.2e-3', '-1.2e-3']) 256 self.do_test(UnsignedReal(), 257 ['1', '1.', '1.2'], 258 ['-1', '-1.', '-1.2', 259 '1e3', '-1e3', '1e-3', '-1e-3', 260 '1.e3', '-1.e3', '1.e-3', '-1.e-3', 261 '1.2e3', '-1.2e3', '1.2e-3', '-1.2e-3']) 262 self.do_test(SignedReal(), 263 ['1', '-1', 264 '1.', '-1.', 265 '1.2', '-1.2'], 266 ['1e3', '-1e3', '1e-3', '-1e-3', 267 '1.e3', '-1.e3', '1.e-3', '-1.e-3', 268 '1.2e3', '-1.2e3', '1.2e-3', '-1.2e-3']) 269 self.do_test(UnsignedEReal(), 270 ['1', '1.', '1.2', 271 '1e3', '1e-3', '1.e3', '1.e-3', 272 '1.2e3', '1.2e-3'], 273 ['-1', '-1.', '-1.2', 274 '-1e3', '-1e-3', '-1.e3', '-1.e-3', 275 '-1.2e3', '-1.2e-3']) 276 self.do_test(SignedEReal(), 277 ['1', '1.', '1.2', 278 '1e3', '1e-3', '1.e3', '1.e-3', 279 '1.2e3', '1.2e-3', 280 '-1', '-1.', '-1.2', 281 '-1e3', '-1e-3', '-1.e3', '-1.e-3', 282 '-1.2e3', '-1.2e-3'], 283 []) 284 self.do_test(UnsignedFloat(), 285 ['1.', '1.2'], 286 ['1', '-1', '-1.', '-1.2', 287 '1e3', '-1e3', '1e-3', '-1e-3', 288 '1.e3', '-1.e3', '1.e-3', '-1.e-3', 289 '1.2e3', '-1.2e3', '1.2e-3', '-1.2e-3']) 290 self.do_test(SignedFloat(), 291 ['1.', '-1.', 292 '1.2', '-1.2'], 293 ['1', '-1', 294 '1e3', '-1e3', '1e-3', '-1e-3', 295 '1.e3', '-1.e3', '1.e-3', '-1.e-3', 296 '1.2e3', '-1.2e3', '1.2e-3', '-1.2e-3']) 297 self.do_test(UnsignedEFloat(), 298 ['1.', '1.2', 299 '1e3', '1e-3', '1.e3', '1.e-3', 300 '1.2e3', '1.2e-3'], 301 ['1', '-1', '-1.', '-1.2', 302 '-1e3', '-1e-3', '-1.e3', '-1.e-3', 303 '-1.2e3', '-1.2e-3']) 304 self.do_test(SignedEFloat(), 305 ['1.', '1.2', 306 '1e3', '1e-3', '1.e3', '1.e-3', 307 '1.2e3', '1.2e-3', 308 '-1.', '-1.2', 309 '-1e3', '-1e-3', '-1.e3', '-1.e-3', 310 '-1.2e3', '-1.2e-3'], 311 ['1', '-1'])
312