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

Source Code for Module lepl.matchers._test3.support

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