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

Source Code for Module lepl._test.wrong_cache_bug

  1  from lepl.support._test.node import NodeTest 
  2  from lepl.core.rewriters import NodeStats, NodeStats2 
  3   
  4  # The contents of this file are subject to the Mozilla Public License 
  5  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  6  # in compliance with the License. You may obtain a copy of the License 
  7  # at http://www.mozilla.org/MPL/ 
  8  # 
  9  # Software distributed under the License is distributed on an "AS IS" 
 10  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 11  # the License for the specific language governing rights and 
 12  # limitations under the License. 
 13  # 
 14  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 15  # The Initial Developer of the Original Code is Andrew Cooke. 
 16  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 17  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 18  # 
 19  # Alternatively, the contents of this file may be used under the terms 
 20  # of the LGPL license (the GNU Lesser General Public License, 
 21  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 22  # of the LGPL License are applicable instead of those above. 
 23  # 
 24  # If you wish to allow use of your version of this file only under the 
 25  # terms of the LGPL License and not to allow others to use your version 
 26  # of this file under the MPL, indicate your decision by deleting the 
 27  # provisions above and replace them with the notice and other provisions 
 28  # required by the LGPL License.  If you do not delete the provisions 
 29  # above, a recipient may use your version of this file under either the 
 30  # MPL or the LGPL License. 
 31   
 32  ''' 
 33  Example returning less results than before. 
 34  ''' 
 35   
 36  from logging import basicConfig, DEBUG 
 37  from unittest import TestCase 
 38   
 39  from lepl import * 
 40   
41 -class CacheTest(TestCase):
42
43 - def test_cache(self):
44 #basicConfig(level=DEBUG) 45 46 with TraceVariables(): 47 value = Token(UnsignedReal()) 48 symbol = Token('[^0-9a-zA-Z \t\r\n]') 49 number = (Optional(symbol('-')) + value) >> float 50 group2, group3c = Delayed(), Delayed() 51 52 parens = symbol('(') & group3c & symbol(')') 53 group1 = parens | number 54 55 mul = (group2 & symbol('*') & group2) > List # changed 56 div = (group2 & symbol('/') & group2) > List # changed 57 group2 += (mul | div | group1) 58 59 add = (group3c & symbol('+') & group3c) > List # changed 60 sub = (group3c & symbol('-') & group3c) > List # changed 61 group3c += (add | sub | group2) 62 63 group3c.config.clear().lexer().auto_memoize().trace_variables() 64 p = group3c.get_parse_all() 65 #print(p.matcher.tree()) 66 results = list(p('1+2*(3-4)+5/6+7')) 67 for result in results: 68 #print(result[0]) 69 pass 70 assert len(results) == 12, results
71
72 - def test_left(self):
73 #basicConfig(level=DEBUG) 74 a = Delayed() 75 a += Optional(a) & (a | 'b' | 'c') 76 for (conservative, full, d, n) in [(None, True, 0, 104), 77 (None, True, 1, 38), 78 (False, False, 1, 38)]: 79 a.config.clear().no_full_first_match().auto_memoize( 80 conservative=conservative, full=full, d=d) 81 p = a.get_parse_all() 82 #print(p.matcher.tree()) 83 r = list(p('bcb')) 84 assert len(r) == n, (n, len(r), r)
85
86 - def test_trace_variables(self):
87 # for comparison 88 with TraceVariables(): 89 a = Delayed() 90 a += Optional(a) & (a | 'b' | 'c') 91 92 #print('\n*** clear') 93 a.config.clear().no_full_first_match() 94 p = a.get_parse_all() 95 #print(p.matcher.tree()) 96 #print(NodeStats2(p.matcher)) 97 98 #print('*** trace_variables') 99 a.config.clear().no_full_first_match().trace_variables() 100 p = a.get_parse_all() 101 #print(p.matcher.tree()) 102 #print(NodeStats2(p.matcher)) 103 104 #print('*** auto_memoize') 105 a.config.clear().no_full_first_match().auto_memoize() 106 p = a.get_parse_all() 107 #print(p.matcher.tree()) 108 #print(NodeStats2(p.matcher)) 109 r = list(p('bcb')) 110 assert len(r) == 104, (len(r), r) 111 112 #basicConfig(level=DEBUG) 113 a.config.clear().no_full_first_match().auto_memoize().trace_variables() 114 p = a.get_parse_all() 115 #print('*** trace_variables and memoize') 116 #print(p.matcher.tree()) 117 #print(NodeStats2(p.matcher)) 118 r = list(p('bcb')) 119 assert len(r) == 104, (len(r), r)
120