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

Source Code for Module lepl.regexp._test.interval

  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.regexp.interval module. 
 32  ''' 
 33   
 34  from logging import basicConfig, DEBUG 
 35  from unittest import TestCase, TestLoader, TextTestRunner 
 36   
 37  from lepl.lexer.matchers import Token 
 38  from lepl.matchers.core import Any 
 39  from lepl.matchers.derived import Eos, Word 
 40  from lepl.regexp.interval import IntervalMap, TaggedFragments, Character 
 41  from lepl.regexp.unicode import UnicodeAlphabet 
 42   
 43   
 44  UNICODE = UnicodeAlphabet.instance() 
 45   
 46   
 47  # pylint: disable-msg=C0103, C0111, C0301, C0324 
 48  # (dude this is just a test) 
 49   
 50   
51 -class IntervalMapTest(TestCase):
52
53 - def test_single(self):
54 m = IntervalMap() 55 m[(1,2)] = 12 56 assert m[0] == None, m[0] 57 assert m[1] == 12, m[1] 58 assert m[1.5] == 12, m[1.5] 59 assert m[2] == 12, m[2] 60 assert m[3] == None, m[3]
61
62 - def test_multiple(self):
63 m = IntervalMap() 64 m[(1,2)] = 12 65 m[(4,5)] = 45 66 for (i, v) in [(0, None), (1, 12), (2, 12), 67 (3, None), (4, 45), (5, 45), (6, None)]: 68 assert m[i] == v, (i, m[i])
69
70 - def test_delete(self):
71 m = IntervalMap() 72 m[(1,2)] = 12 73 m[(4,5)] = 45 74 for (i, v) in [(0, None), (1, 12), (2, 12), 75 (3, None), (4, 45), (5, 45), (6, None)]: 76 assert m[i] == v, (i, m[i]) 77 del m[(1,2)] 78 for (i, v) in [(0, None), (1, None), (2, None), 79 (3, None), (4, 45), (5, 45), (6, None)]: 80 assert m[i] == v, (i, m[i])
81 82
83 -class TaggedFragmentsTest(TestCase):
84
85 - def test_single(self):
86 m = TaggedFragments(UNICODE) 87 m.append(Character([('b', 'c')], UNICODE), 'bc') 88 l = list(m) 89 assert l == [(('b', 'c'), ['bc'])], l
90
91 - def test_overlap(self):
92 m = TaggedFragments(UNICODE) 93 m.append(Character([('b', 'd')], UNICODE), 'bd') 94 m.append(Character([('c', 'e')], UNICODE), 'ce') 95 l = list(m) 96 assert l == [(('b', 'b'), ['bd']), 97 (('c', 'd'), ['bd', 'ce']), 98 (('e', 'e'), ['ce'])], l
99 100
101 -class UnicodeBugTest(TestCase):
102
103 - def test_bug(self):
104 #basicConfig(level=DEBUG) 105 t = Token(Word())(Any()[2] & Eos()) 106 t.match("ab cd")
107 108 109 if __name__ == '__main__': 110 suite = TestLoader().loadTestsFromTestCase(UnicodeBugTest) 111 TextTestRunner(verbosity=2).run(suite) 112