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

Source Code for Module lepl.support._test.list

  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.list module. 
 32  ''' 
 33   
 34  #from logging import basicConfig, DEBUG, INFO 
 35  from unittest import TestCase 
 36   
 37  from lepl import * 
 38  from lepl._test.base import assert_str 
 39  from lepl.support.list import clone_sexpr, count_sexpr, join, \ 
 40      sexpr_flatten, sexpr_to_str 
 41   
 42   
43 -class FoldTest(TestCase):
44
45 - def test_clone(self):
46 def test(list_): 47 copy = clone_sexpr(list_) 48 assert copy == list_, sexpr_to_str(copy)
49 test([]) 50 test([1,2,3]) 51 test([[1],2,3]) 52 test([[[1]],2,3]) 53 test([[[1]],2,[3]]) 54 test([[[1]],'2',[3]]) 55 test(((1),List([2,3,[4]])))
56
57 - def test_count(self):
58 def test(list_, value): 59 measured = count_sexpr(list_) 60 assert measured == value, measured
61 test([], 0) 62 test([1,2,3], 3) 63 test([[1],2,3], 3) 64 test([[[1,2],3],'four',5], 5) 65
66 - def test_flatten(self):
67 def test(list_, joined, flattened): 68 if joined is not None: 69 result = join(list_) 70 assert result == joined, result 71 result = sexpr_flatten(list_) 72 assert result == flattened, result
73 test([[1],[2, [3]]], [1,2,[3]], [1,2,3]) 74 test([], [], []) 75 test([1,2,3], None, [1,2,3]) 76 test([[1],2,3], None, [1,2,3]) 77 test([[[1,'two'],3],'four',5], None, [1,'two',3,'four',5]) 78
79 - def test_sexpr_to_string(self):
80 def test(list_, value): 81 result = sexpr_to_str(list_) 82 assert result == value, result
83 test([1,2,3], '[1,2,3]') 84 test((1,2,3), '(1,2,3)') 85 test(List([1,2,3]), 'List([1,2,3])') 86 class Foo(List): pass 87 test(Foo([1,2,(3,List([4]))]), 'Foo([1,2,(3,List([4]))])') 88 89
90 -class AstTest(TestCase):
91
92 - def test_ast(self):
93 94 class Term(List): pass 95 class Factor(List): pass 96 class Expression(List): pass 97 98 expr = Delayed() 99 number = Digit()[1:,...] >> int 100 101 with Separator(Drop(Regexp(r'\s*'))): 102 term = number | '(' & expr & ')' > Term 103 muldiv = Any('*/') 104 factor = term & (muldiv & term)[:] > Factor 105 addsub = Any('+-') 106 expr += factor & (addsub & factor)[:] > Expression 107 line = expr & Eos() 108 109 ast = line.parse_string('1 + 2 * (3 + 4 - 5)')[0] 110 text = str(ast) 111 assert_str(text, """Expression 112 +- Factor 113 | `- Term 114 | `- 1 115 +- '+' 116 `- Factor 117 +- Term 118 | `- 2 119 +- '*' 120 `- Term 121 +- '(' 122 +- Expression 123 | +- Factor 124 | | `- Term 125 | | `- 3 126 | +- '+' 127 | +- Factor 128 | | `- Term 129 | | `- 4 130 | +- '-' 131 | `- Factor 132 | `- Term 133 | `- 5 134 `- ')'""")
135 136
137 -class EmptyListBugTest(TestCase):
138
139 - def test_empty(self):
140 s = str(List()) 141 assert s == 'List', s
142