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

Source Code for Module lepl.support._test.node

  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.support.node module. 
 32  ''' 
 33   
 34  #from logging import basicConfig, DEBUG, INFO 
 35  from unittest import TestCase 
 36   
 37  from lepl import Delayed, Digit, Any, Node, make_error, node_throw, Or, Space, \ 
 38      AnyBut, Eos 
 39  from lepl.support.graph import order, PREORDER, POSTORDER, LEAF 
 40  from lepl._test.base import assert_str 
 41   
 42   
 43  # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102, C0321, R0201, R0903 
 44  # (dude this is just a test) 
 45   
 46       
47 -class NodeTest(TestCase):
48
49 - def test_node(self):
50 #basicConfig(level=DEBUG) 51 52 class Term(Node): pass 53 class Factor(Node): pass 54 class Expression(Node): pass 55 56 expression = Delayed() 57 number = Digit()[1:,...] > 'number' 58 term = (number | '(' / expression / ')') > Term 59 muldiv = Any('*/') > 'operator' 60 factor = (term / (muldiv / term)[0::]) > Factor 61 addsub = Any('+-') > 'operator' 62 expression += (factor / (addsub / factor)[0::]) > Expression 63 64 p = expression.get_parse_string() 65 ast = p('1 + 2 * (3 + 4 - 5)') 66 assert_str(ast[0], """Expression 67 +- Factor 68 | +- Term 69 | | `- number '1' 70 | `- ' ' 71 +- operator '+' 72 +- ' ' 73 `- Factor 74 +- Term 75 | `- number '2' 76 +- ' ' 77 +- operator '*' 78 +- ' ' 79 `- Term 80 +- '(' 81 +- Expression 82 | +- Factor 83 | | +- Term 84 | | | `- number '3' 85 | | `- ' ' 86 | +- operator '+' 87 | +- ' ' 88 | +- Factor 89 | | +- Term 90 | | | `- number '4' 91 | | `- ' ' 92 | +- operator '-' 93 | +- ' ' 94 | `- Factor 95 | `- Term 96 | `- number '5' 97 `- ')'""")
98
99 -class ListTest(TestCase):
100
101 - def test_list(self):
102 #basicConfig(level=DEBUG) 103 104 expression = Delayed() 105 number = Digit()[1:,...] > 'number' 106 term = (number | '(' / expression / ')') > list 107 muldiv = Any('*/') > 'operator' 108 factor = (term / (muldiv / term)[0:]) > list 109 addsub = Any('+-') > 'operator' 110 expression += (factor / (addsub / factor)[0:]) > list 111 112 ast = expression.parse_string('1 + 2 * (3 + 4 - 5)') 113 assert ast == [[[[('number', '1')], ' '], ('operator', '+'), ' ', [[('number', '2')], ' ', ('operator', '*'), ' ', ['(', [[[('number', '3')], ' '], ('operator', '+'), ' ', [[('number', '4')], ' '], ('operator', '-'), ' ', [[('number', '5')]]], ')']]]], ast
114 115
116 -class ErrorTest(TestCase):
117
118 - def test_error(self):
119 #basicConfig(level=INFO) 120 121 class Term(Node): pass 122 class Factor(Node): pass 123 class Expression(Node): pass 124 125 expression = Delayed() 126 number = Digit()[1:,...] > 'number' 127 term = Or( 128 AnyBut(Space() | Digit() | '(')[1:,...] ^ 'unexpected text: {results[0]}', 129 number > Term, 130 number ** make_error("no ( before {out_rest}") / ')' >> node_throw, 131 '(' / expression / ')' > Term, 132 ('(' / expression / Eos()) ** make_error("no ) for {in_rest}") >> node_throw) 133 muldiv = Any('*/') > 'operator' 134 factor = (term / (muldiv / term)[0:,r'\s*']) > Factor 135 addsub = Any('+-') > 'operator' 136 expression += (factor / (addsub / factor)[0:,r'\s*']) > Expression 137 line = expression / Eos() 138 139 parser = line.get_parse_string() 140 141 try: 142 parser('1 + 2 * 3 + 4 - 5)')[0] 143 assert False, 'expected error' 144 except SyntaxError as e: 145 assert e.msg == "no ( before ')'", e.msg 146 147 try: 148 parser('1 + 2 * (3 + 4 - 5') 149 assert False, 'expected error' 150 except SyntaxError as e: 151 assert e.msg == "no ) for '(3 + 4 - 5'", e.msg 152 153 try: 154 parser('1 + 2 * foo') 155 assert False, 'expected error' 156 except SyntaxError as e: 157 assert e.msg == "unexpected text: foo", e.msg
158 159
160 -class EqualityTest(TestCase):
161
162 - def test_object_eq(self):
163 a = Node('a') 164 b = Node('a') 165 assert a != b 166 assert b != a 167 assert a is not b 168 assert b is not a 169 assert a == a 170 assert b == b 171 assert a is a 172 assert b is b
173
174 - def test_recursive_eq(self):
175 a = Node('a', Node('b')) 176 b = Node('a', Node('b')) 177 c = Node('a', Node('c')) 178 assert a._recursively_eq(b) 179 assert not a._recursively_eq(c)
180 181
182 -class ChildrenTest(TestCase):
183
184 - def test_children(self):
185 a = Node('a') 186 for c in a: 187 assert c == 'a', c
188 189
190 -class OrderTest(TestCase):
191
192 - def tree(self):
193 return Node('a', 194 Node('b', 195 Node('c', 196 Node('d'), 197 Node('e')), 198 Node('f')), 199 Node('g'), 200 Node('h', 201 Node('i', 202 Node('j'), 203 Node('k')), 204 Node('l')))
205
206 - def order(self, tree, flags):
207 return list(map(lambda x: x[0], order(tree, flags, Node, LEAF)))
208
209 - def test_orders(self):
210 tree = self.tree() 211 ordered = self.order(tree, PREORDER) 212 assert ordered == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'], ordered 213 ordered = self.order(tree, POSTORDER) 214 assert ordered == ['d', 'e', 'c', 'f', 'b', 'g', 'j', 'k', 'i', 'l', 'h', 'a'], ordered
215
216 - def test_str(self):
217 text = str(self.tree()) 218 assert text == """Node 219 +- 'a' 220 +- Node 221 | +- 'b' 222 | +- Node 223 | | +- 'c' 224 | | +- Node 225 | | | `- 'd' 226 | | `- Node 227 | | `- 'e' 228 | `- Node 229 | `- 'f' 230 +- Node 231 | `- 'g' 232 `- Node 233 +- 'h' 234 +- Node 235 | +- 'i' 236 | +- Node 237 | | `- 'j' 238 | `- Node 239 | `- 'k' 240 `- Node 241 `- 'l'""", text
242 243
244 -class NestedNamedTest(TestCase):
245
246 - def tree(self):
247 return Node(('a', Node('A')), ('b', Node('B')))
248
249 - def test_str(self):
250 text = str(self.tree()) 251 assert text == """Node 252 +- a 253 | `- 'A' 254 `- b 255 `- 'B'""", text
256 257
258 -class NodeEqualityTest(TestCase):
259
260 - def test_equals(self):
261 a = Node('abc') 262 b = Node('abc') 263 assert a == a 264 assert not (a != a) 265 assert not (a == b) 266 assert a._recursively_eq(b) 267 assert Node(a) != a 268 assert Node(a)._recursively_eq(Node(a)) 269 assert not Node(a)._recursively_eq(a)
270