Package lepl :: Package core :: Package _test :: Module config
[hide private]
[frames] | no frames]

Source Code for Module lepl.core._test.config

  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.core.config module. 
 32  ''' 
 33   
 34  #from logging import basicConfig, DEBUG 
 35  from unittest import TestCase 
 36       
 37   
 38  from lepl.matchers.variables import TraceVariables 
 39  from lepl.matchers.operators import DroppedSpace 
 40  from lepl.matchers.derived import Drop, Word, String 
 41  from lepl.matchers.core import Any, Lookahead 
 42  from lepl._test.base import assert_str 
 43  from lepl.stream.maxdepth import FullFirstMatchException 
 44  from lepl.regexp.core import RegexpError 
 45  from lepl.support.lib import StringIO 
 46   
 47   
48 -class ParseTest(TestCase):
49
50 - def run_test(self, name, text, parse, match2, match3, error, 51 config=lambda x: None, **kargs):
52 matcher = Any()[:, ...] 53 config(matcher) 54 parser = getattr(matcher, 'parse' + name) 55 result = str(parser(text, **kargs)) 56 assert_str(result, parse) 57 matcher = Any()[2, ...] 58 matcher.config.no_full_first_match() 59 config(matcher) 60 parser = getattr(matcher, 'match' + name) 61 result = str(list(parser(text, **kargs))) 62 assert_str(result, match2) 63 matcher = Any()[3, ...] 64 matcher.config.no_full_first_match() 65 config(matcher) 66 parser = getattr(matcher, 'match' + name) 67 result = str(list(parser(text, **kargs))) 68 assert_str(result, match3) 69 matcher = Any() 70 config(matcher) 71 parser = getattr(matcher, 'parse' + name) 72 try: 73 parser(text, **kargs) 74 except FullFirstMatchException as e: 75 assert_str(e, error)
76
77 - def test_string(self):
78 self.run_test('_string', 'abc', 79 "['abc']", 80 "[(['ab'], (2, <helper>))]", 81 "[(['abc'], (3, <helper>))]", 82 "The match failed in <string> at 'bc' (line 1, character 2).") 83 self.run_test('', 'abc', 84 "['abc']", 85 "[(['ab'], (2, <helper>))]", 86 "[(['abc'], (3, <helper>))]", 87 "The match failed in <string> at 'bc' (line 1, character 2).") 88 self.run_test('_sequence', 'abc', 89 "['abc']", 90 "[(['ab'], (2, <helper>))]", 91 "[(['abc'], (3, <helper>))]", 92 "The match failed in <str> at 'bc' (offset 1, value 'b').")
93
94 - def test_string_list(self):
95 self.run_test('_list', ['a', 'b', 'c'], 96 "[['a', 'b', 'c']]", 97 "[([['a', 'b']], (2, <helper>))]", 98 "[([['a', 'b', 'c']], (3, <helper>))]", 99 "The match failed in <list<str>> at ['b', 'c'] (offset 1, value 'b').", 100 config=lambda m: m.config.no_compile_to_regexp())
101
102 - def test_int_list(self):
103 #basicConfig(level=DEBUG) 104 try: 105 # this fails for python2 because it relies on 106 # comparison between types failing 107 self.run_test('_list', [1, 2, 3], [], [], [], """""") 108 except RegexpError as e: 109 assert 'no_compile_to_regexp' in str(e), str(e) 110 self.run_test('_list', [1, 2, 3], 111 "[[1, 2, 3]]", 112 "[([[1, 2]], (2, <helper>))]", 113 "[([[1, 2, 3]], (3, <helper>))]", 114 "The match failed in <list<int>> at [2, 3] (offset 1, value 2).", 115 config=lambda m: m.config.no_compile_to_regexp())
116 117
118 -class BugTest(TestCase):
119
120 - def test_bug(self):
121 matcher = Any('a') 122 matcher.config.clear() 123 result = list(matcher.match_list(['b'])) 124 assert result == [], result
125 126
127 -class TraceVariablesTest(TestCase):
128
129 - def test_trace(self):
130 buffer = StringIO() 131 with TraceVariables(out=buffer): 132 word = ~Lookahead('OR') & Word() 133 phrase = String() 134 with DroppedSpace(): 135 text = (phrase | word)[1:] > list 136 query = text[:, Drop('OR')] 137 expected = ''' phrase failed stream = 'spicy meatballs OR... 138 word = ['spicy'] stream = ' meatballs OR "el ... 139 phrase failed stream = 'meatballs OR "el b... 140 word = ['meatballs'] stream = ' OR "el bulli rest... 141 phrase failed stream = 'OR "el bulli resta... 142 word failed stream = 'OR "el bulli resta... 143 phrase failed stream = ' OR "el bulli rest... 144 word failed stream = ' OR "el bulli rest... 145 text = [['spicy', 'meatballs']] stream = ' OR "el bulli rest... 146 phrase = ['el bulli restaurant'] stream = '' 147 phrase failed stream = '' 148 word failed stream = '' 149 text = [['el bulli restaurant']] stream = '' 150 query = [['spicy', 'meatballs'], ['el... stream = '' 151 ''' 152 query.config.auto_memoize(full=True) 153 query.parse('spicy meatballs OR "el bulli restaurant"') 154 trace = buffer.getvalue() 155 assert trace == expected, '"""' + trace + '"""' 156 # check caching works 157 query.parse('spicy meatballs OR "el bulli restaurant"') 158 trace = buffer.getvalue() 159 assert trace == expected, '"""' + trace + '"""'
160