@@ -46,11 +46,37 @@ def atheistParseError(self):
4646 """This error is not an error"""
4747 self .outputTokens .append (u"AtheistParseError" )
4848
49- def loadTests (f ):
50- for i , line in enumerate (f ):
51- if line and not line [0 ] == "#" :
52- testList = eval (line )
53- yield i + 1 , tuple (testList )
49+ def concatanateCharacterTokens (tokens ):
50+ outputTokens = []
51+ for token in tokens :
52+ if not "ParseError" in token and token [0 ] == "Character" :
53+ if (outputTokens and not "ParseError" in outputTokens [- 1 ] and
54+ outputTokens [- 1 ][0 ] == "Character" ):
55+ outputTokens [- 1 ][1 ] += token [1 ]
56+ else :
57+ outputTokens .append (token )
58+ else :
59+ outputTokens .append (token )
60+ return outputTokens
61+
62+ def tokensMatch (expectedTokens , recievedTokens ):
63+ """Test whether the test has passed or failed
64+
65+ For brevity in the tests, the test has passed if the sequence of expected
66+ tokens appears anywhere in the sequqnce of returned tokens.
67+
68+ We also concatanate all consecutive character tokens into a single token"""
69+
70+ expectedTokens = concatanateCharacterTokens (expectedTokens )
71+ recievedTokens = concatanateCharacterTokens (recievedTokens )
72+
73+ for i , token in enumerate (recievedTokens ):
74+ if expectedTokens [0 ] == token :
75+ if (len (expectedTokens ) <= len (recievedTokens [i :]) and
76+ recievedTokens [i :i + len (expectedTokens )]):
77+ return True
78+ return False
79+
5480
5581def test_tokenizer ():
5682 for filename in glob .glob ('tokenizer/*.test' ):
@@ -62,21 +88,21 @@ def runTokenizerTest(description, input, output):
6288 #XXX - move this out into the setup function
6389 parser = TokenizerTestParser ()
6490 tokens = parser .parse (StringIO .StringIO (input ))
65- try :
66- assert tokens == output
67- except AssertionError :
68- print "Failed test %s" % (description ,)
69- print "Got" , tokens , "expected" , output
70- return False
71- return True
91+ assert tokensMatch (tokens , output )
7292
7393def main ():
7494 failed = 0
7595 tests = 0
7696 for func , desc , input , output in test_tokenizer ():
7797 tests += 1
78- passed = func (desc , input , output )
79- if not passed : failed += 1
98+ try :
99+ func (desc , input , output )
100+ except AssertionError :
101+ print "Failed test %s" % (desc ,)
102+ parser = TokenizerTestParser ()
103+ tokens = parser .parse (StringIO .StringIO (input ))
104+ print "Got" , tokens , "expected" , output
105+ failed += 1
80106 print "Ran %i tests, failed %i" % (tests , failed )
81107
82108if __name__ == "__main__" :
0 commit comments