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

Source Code for Module lepl.matchers._test.support

  1  from lepl.stream.core import DUMMY_HELPER, 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  # 
 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.support module 
 33  ''' 
 34   
 35  #from logging import basicConfig, DEBUG 
 36  from unittest import TestCase 
 37   
 38  from lepl.matchers.support import function_matcher_factory, function_matcher, \ 
 39      sequence_matcher_factory, sequence_matcher 
40 41 42 @function_matcher 43 -def char(support, stream):
44 (char, stream) = s_next(stream) 45 return ([char], stream)
46
47 @function_matcher_factory() 48 -def char_in(chars):
49 def match(support, stream): 50 (char, next_stream) = s_next(stream) 51 if char in chars: 52 return ([char], next_stream)
53 return match 54
55 @sequence_matcher 56 -def any_char(support, stream):
57 while True: 58 (char, stream) = s_next(stream) 59 yield ([char], stream)
60
61 @sequence_matcher_factory() 62 -def any_char_in(chars):
63 def match(support, stream): 64 while True: 65 (char, stream) = s_next(stream) 66 if char in chars: 67 yield ([char], stream)
68 return match 69
70 71 -def mks(head, offset):
72 return (offset, DUMMY_HELPER)
73
74 75 -class DecoratorTest(TestCase):
76
77 - def test_char(self):
78 #basicConfig(level=DEBUG) 79 matcher = char() 80 matcher.config.no_full_first_match() 81 result = list(matcher.match_sequence('ab')) 82 assert result == [(['a'], mks('ab', 1))], result 83 matcher = char()[2:,...] 84 matcher.config.no_full_first_match() 85 result = list(matcher.match_sequence('abcd')) 86 assert result == [(['abcd'], mks('abcd', 4)), 87 (['abc'], mks('abcd', 3)), 88 (['ab'], mks('abcd', 2))], result 89 assert char()[:,...].parse('ab') == ['ab']
90
91 - def test_char_in(self):
92 #basicConfig(level=DEBUG) 93 matcher = char_in('abc') 94 matcher.config.no_full_first_match() 95 result = list(matcher.match_sequence('ab')) 96 assert result == [(['a'], mks('ab', 1))], result 97 result = list(matcher.match_sequence('pqr')) 98 assert result == [], result 99 matcher = char_in('abc')[2:,...] 100 matcher.config.no_full_first_match() 101 result = list(matcher.match_sequence('abcd')) 102 assert result == [(['abc'], mks('abcd', 3)), 103 (['ab'], mks('abcd', 2))], result
104
105 - def test_any_char(self):
106 #basicConfig(level=DEBUG) 107 matcher = any_char() 108 # with this set we have an extra eos that messes things up 109 matcher.config.no_full_first_match() 110 result = list(matcher.match_sequence('ab')) 111 assert result == [(['a'], mks('ab', 1)), 112 (['b'], mks('ab', 2))], result 113 matcher = any_char()[2:,...] 114 matcher.config.no_full_first_match() 115 result = list(matcher.match_sequence('abcd')) 116 assert result == [(['abcd'], mks('abcd', 4)), 117 (['abc'], mks('abcd', 3)), 118 (['abd'], mks('abcd', 4)), 119 (['ab'], mks('abcd', 2)), 120 (['acd'], mks('abcd', 4)), 121 (['ac'], mks('abcd', 3)), 122 (['ad'], mks('abcd', 4)), 123 (['bcd'], mks('abcd', 4)), 124 (['bc'], mks('abcd', 3)), 125 (['bd'], mks('abcd', 4)), 126 (['cd'], mks('abcd', 4))], result
127
128 - def test_any_char_in(self):
129 matcher = any_char_in('abc') 130 matcher.config.no_full_first_match() 131 result = list(matcher.match_sequence('ab')) 132 assert result == [(['a'], mks('ab', 1)), 133 (['b'], mks('ab', 2))], result 134 result = list(matcher.match_sequence('pqr')) 135 assert result == [], result 136 matcher = any_char_in('abc')[2:,...] 137 matcher.config.no_full_first_match() 138 result = list(matcher.match_sequence('abcd')) 139 assert result == [(['abc'], mks('abcd', 3)), 140 (['ab'], mks('abcd', 2)), 141 (['ac'], mks('abcd', 3)), 142 (['bc'], mks('abcd', 3))], result
143
144 - def test_bad_args(self):
145 #basicConfig(level=DEBUG) 146 try: 147 char(foo='abc') 148 assert False, 'expected error' 149 except TypeError: 150 pass 151 try: 152 char('abc') 153 assert False, 'expected error' 154 except TypeError: 155 pass 156 try: 157 char_in() 158 assert False, 'expected error' 159 except TypeError: 160 pass 161 try: 162 @function_matcher 163 def foo(a): return 164 assert False, 'expected error' 165 except TypeError: 166 pass
167
168 169 -class FunctionMatcherBugTest(TestCase):
170
171 - def test_bug(self):
172 #basicConfig(level=DEBUG) 173 from string import ascii_uppercase 174 @function_matcher 175 def capital(support, stream): 176 (char, next_stream) = s_next(stream) 177 if char in ascii_uppercase: 178 return ([char], next_stream)
179 parser = capital()[3] 180 assert parser.parse_string('ABC')
181