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

Source Code for Module lepl.support._test.graph

  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.graph module. 
 32  ''' 
 33   
 34  from unittest import TestCase 
 35   
 36   
 37  from lepl.support.graph import ArgAsAttributeMixin, preorder, postorder, reset, \ 
 38      ConstructorWalker, Clone, make_proxy, LEAF, leaves 
 39  from lepl.support.node import Node 
 40   
 41   
 42  # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102, C0321, W0141 
 43  # (dude this is just a test) 
 44   
 45       
46 -class SimpleNode(ArgAsAttributeMixin):
47 48 # pylint: disable-msg=E1101 49
50 - def __init__(self, label, *nodes):
51 super(SimpleNode, self).__init__() 52 self._arg(label=label) 53 self._args(nodes=nodes)
54
55 - def __str__(self):
56 return str(self.label)
57
58 - def __repr__(self):
59 args = [str(self.label)] 60 args.extend(map(repr, self.nodes)) 61 return 'SimpleNode(%s)' % ','.join(args)
62
63 - def __getitem__(self, index):
64 return self.nodes[index]
65
66 - def __len__(self):
67 return len(self.nodes)
68 69
70 -def graph():
71 return SimpleNode(1, 72 SimpleNode(11, 73 SimpleNode(111), 74 SimpleNode(112)), 75 SimpleNode(12))
76
77 -class OrderTest(TestCase):
78
79 - def test_preorder(self):
80 result = [node.label for node in preorder(graph(), SimpleNode, exclude=LEAF)] 81 assert result == [1, 11, 111, 112, 12], result
82
83 - def test_postorder(self):
84 result = [node.label for node in postorder(graph(), SimpleNode, exclude=LEAF)] 85 assert result == [111, 112, 11, 12, 1], result
86 87
88 -class ResetTest(TestCase):
89
90 - def test_reset(self):
91 nodes = preorder(graph(), SimpleNode, exclude=LEAF) 92 assert next(nodes).label == 1 93 assert next(nodes).label == 11 94 reset(nodes) 95 assert next(nodes).label == 1 96 assert next(nodes).label == 11
97 98
99 -class CloneTest(TestCase):
100
101 - def test_simple(self):
102 g1 = graph() 103 g2 = ConstructorWalker(g1, SimpleNode)(Clone()) 104 assert repr(g1) == repr(g2) 105 assert g1 is not g2
106
107 - def assert_same(self, text1, text2):
108 assert self.__clean(text1) == self.__clean(text2), self.__clean(text1)
109
110 - def __clean(self, text):
111 depth = 0 112 result = '' 113 for c in text: 114 if c == '<': 115 depth += 1 116 elif c == '>': 117 depth -= 1 118 elif depth == 0: 119 result += c 120 return result
121
122 - def test_loop(self):
123 (s, n) = make_proxy() 124 g1 = SimpleNode(1, 125 SimpleNode(11, 126 SimpleNode(111), 127 SimpleNode(112), 128 n), 129 SimpleNode(12)) 130 s(g1) 131 g2 = ConstructorWalker(g1, SimpleNode)(Clone()) 132 self.assert_same(repr(g1), repr(g2))
133
134 - def test_loops(self):
135 (s1, n1) = make_proxy() 136 (s2, n2) = make_proxy() 137 g1 = SimpleNode(1, 138 SimpleNode(11, 139 SimpleNode(111, n2), 140 SimpleNode(112), 141 n1), 142 SimpleNode(12, n1)) 143 s1(g1) 144 s2(next(iter(g1))) 145 g2 = ConstructorWalker(g1, SimpleNode)(Clone()) 146 self.assert_same(repr(g1), repr(g2))
147
148 - def test_loops_with_proxy(self):
149 (s1, n1) = make_proxy() 150 (s2, n2) = make_proxy() 151 g1 = SimpleNode(1, 152 SimpleNode(11, 153 SimpleNode(111, n2), 154 SimpleNode(112), 155 n1), 156 SimpleNode(12, n1)) 157 s1(g1) 158 s2(next(iter(g1))) 159 g2 = ConstructorWalker(g1, SimpleNode)(Clone()) 160 g3 = ConstructorWalker(g2, SimpleNode)(Clone()) 161 self.assert_same(repr(g1), repr(g3))
162 # print(repr(g3)) 163 164
165 -class GenericOrderTest(TestCase):
166
167 - def test_preorder(self):
168 g = [1, [11, [111, 112], 12]] 169 result = [node for node in preorder(g, list) if isinstance(node, int)] 170 assert result == [1, 11, 111, 112, 12], result
171
172 - def test_postorder(self):
173 ''' 174 At first I was surprised about this (compare with SimpleNode results above), 175 but these are leaf nodes, so postorder doesn't change anything (there's 176 no difference between "before visiting" and "after visiting" a leaf). 177 ''' 178 g = [1, [11, [111, 112], 12]] 179 result = [node for node in postorder(g, list) if isinstance(node, int)] 180 assert result == [1, 11, 111, 112, 12], result
181 182
183 -class LeafTest(TestCase):
184
185 - def test_order(self):
186 tree = Node(1, 2, Node(3, Node(4), Node(), 5)) 187 result = list(leaves(tree, Node)) 188 assert result == [1,2,3,4,5], result
189