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

Source Code for Module lepl.matchers._test.operators

  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.matchers.operators module. 
 32  ''' 
 33   
 34  from threading import Thread 
 35  from unittest import TestCase 
 36   
 37  from lepl import Delayed, Any, Eos, Drop, Separator, Literal, Space, \ 
 38      SmartSeparator1, Optional, Real 
 39  from lepl._test.base import BaseTest 
 40   
 41   
 42  # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102 
 43  # (dude this is just a test) 
 44   
 45       
46 -class ThreadTest(TestCase):
47 ''' 48 Test for thread safety. 49 ''' 50
51 - def test_safety(self):
52 matcher3 = Delayed() 53 matcher4 = Delayed() 54 matcher1 = Any()[::'b',...] & Eos() 55 with Separator(Drop(Any('a')[:])): 56 matcher2 = Any()[::'b',...] & Eos() 57 # pylint: disable-msg=W0613 58 def target(matcher3=matcher3, matcher4=matcher4): 59 matcher3 += Any()[::'b',...] & Eos() 60 with Separator(Drop(Any('b')[:])): 61 matcher4 += Any()[::'b',...] & Eos()
62 t = Thread(target=target) 63 t.start() 64 t.join() 65 matcher5 = Any()[::'b',...] & Eos() 66 matcher6 = Any()[::'b',...] & Eos() 67 text = 'cababab' 68 assert text == matcher1.parse_string(text)[0], matcher1.parse_string(text) 69 assert 'cbbb' == matcher2.parse_string(text)[0], matcher2.parse_string(text) 70 assert text == matcher3.parse_string(text)[0], matcher3.parse_string(text) 71 assert 'caaa' == matcher4.parse_string(text)[0], matcher4.parse_string(text) 72 assert 'cbbb' == matcher5.parse_string(text)[0], matcher5.parse_string(text) 73 assert text == matcher6.parse_string(text)[0], matcher6.parse_string(text)
74 75
76 -class SpaceTest(TestCase):
77
78 - def word(self):
79 return Literal("a") & Literal("bc")[1:,...]
80
81 - def test_spaces(self):
82 with Separator(~Space()): 83 s1 = self.word()[1:] 84 s1.config.no_full_first_match() 85 s1 = s1.get_parse_string() 86 assert not s1("abc") 87 assert s1("a bc") 88 with Separator(None): 89 s2 = self.word()[1:] 90 s2.config.no_full_first_match() 91 s2 = s2.get_parse_string() 92 assert s2("abc") 93 assert not s2("a bc")
94 95
96 -class SmartSpace1Test(TestCase):
97
98 - def test_smart_spaces(self):
99 with SmartSeparator1(Space()): 100 parser = 'a' & Optional('b') & 'c' & Eos() 101 parser.config.no_full_first_match() 102 assert parser.parse('a b c') 103 assert parser.parse('a c') 104 assert not parser.parse('a b c ') 105 assert not parser.parse('a c ') 106 assert not parser.parse('a bc') 107 assert not parser.parse('ab c') 108 assert not parser.parse('abc') 109 assert not parser.parse('ac') 110 assert not parser.parse('a c')
111 112
113 -class IndexTest(TestCase):
114
115 - def test_bad_indices(self):
116 try: 117 Any()[1:2,3:4] 118 assert False, 'expected error' 119 except TypeError as e: 120 assert 'slice' in str(e), e
121 122
123 -class RepeatParseTest(BaseTest):
124
125 - def test_limit(self):
126 self.assert_direct('1.2', Real()[1:1], [['1.2'], ['1.'], ['1']]) 127 self.assert_direct('1.2', Real()[1:1:0], []) 128 self.assert_direct('1.2', Real()[1:1:1], [['1.2']]) 129 self.assert_direct('1.2', Real()[1:1:-1], [['1.2'], ['1.'], ['1']]) 130 self.assert_direct('1.2', Real()[1:1:'d'], [['1.2'], ['1.'], ['1']]) 131 self.assert_direct('1.2', Real()[1:1:'d1'], [['1.2']]) 132 self.assert_direct('1.2', Real()[1:1:'1d'], [['1.2']])
133