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

Source Code for Module lepl.core._test.clone

 1  from lepl.matchers.variables import TraceVariables 
 2  from lepl.lexer.matchers import Token 
 3  from lepl.support.list import List 
 4   
 5  # The contents of this file are subject to the Mozilla Public License 
 6  # (MPL) Version 1.1 (the "License"); you may not use this file except 
 7  # in compliance with the License. You may obtain a copy of the License 
 8  # at http://www.mozilla.org/MPL/ 
 9  # 
10  # Software distributed under the License is distributed on an "AS IS" 
11  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
12  # the License for the specific language governing rights and 
13  # limitations under the License. 
14  # 
15  # The Original Code is LEPL (http://www.acooke.org/lepl) 
16  # The Initial Developer of the Original Code is Andrew Cooke. 
17  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
18  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
19  # 
20  # Alternatively, the contents of this file may be used under the terms 
21  # of the LGPL license (the GNU Lesser General Public License, 
22  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
23  # of the LGPL License are applicable instead of those above. 
24  # 
25  # If you wish to allow use of your version of this file only under the 
26  # terms of the LGPL License and not to allow others to use your version 
27  # of this file under the MPL, indicate your decision by deleting the 
28  # provisions above and replace them with the notice and other provisions 
29  # required by the LGPL License.  If you do not delete the provisions 
30  # above, a recipient may use your version of this file under either the 
31  # MPL or the LGPL License. 
32   
33  ''' 
34  Tests for cloning matchers. 
35  ''' 
36   
37  from unittest import TestCase 
38  from difflib import Differ 
39   
40  from lepl.matchers.core import Literal, Delayed 
41  from lepl.matchers.derived import Drop, UnsignedReal, Optional 
42  from lepl.core.rewriters import clone_matcher 
43   
44   
45 -class CloneTest(TestCase):
46
47 - def assert_clone_ok(self, matcher):
48 copy = clone_matcher(matcher) 49 mtree = matcher.tree() 50 ctree = copy.tree() 51 if mtree != ctree: 52 print(mtree) 53 print(ctree) 54 diff = Differ() 55 print('\n'.join(diff.compare(mtree.split('\n'), ctree.split('\n')))) 56 assert mtree == ctree 57 assert matcher is not copy
58
59 - def test_single(self):
60 self.assert_clone_ok(Literal('foo'))
61
62 - def test_no_loop(self):
63 self.assert_clone_ok(Literal('a') | 'b' & Drop('c'))
64
65 - def test_delayed(self):
66 d = Delayed() 67 d += Literal('foo') 68 self.assert_clone_ok(d) 69 self.assert_clone_ok(Literal('bar') & d)
70
71 - def test_loop(self):
72 d = Delayed() 73 a = d | 'b' 74 d += a 75 self.assert_clone_ok(d)
76
77 - def test_loops(self):
78 with TraceVariables(): 79 value = Token(UnsignedReal()) 80 symbol = Token('[^0-9a-zA-Z \t\r\n]') 81 number = (Optional(symbol('-')) + value) >> float 82 group2, group3c = Delayed(), Delayed() 83 84 parens = symbol('(') & group3c & symbol(')') 85 group1 = parens | number 86 87 mul = (group2 & symbol('*') & group2) > List # changed 88 div = (group2 & symbol('/') & group2) > List # changed 89 group2 += (mul | div | group1) 90 91 add = (group3c & symbol('+') & group3c) > List # changed 92 sub = (group3c & symbol('-') & group3c) > List # changed 93 group3c += (add | sub | group2) 94 self.assert_clone_ok(group3c)
95