1
+ import sys
2
+ import os
3
+ import glob
4
+ import StringIO
5
+ import unittest
6
+ import new
7
+
8
+ # XXX Allow us to import the sibling module
9
+ os .chdir (os .path .split (os .path .abspath (__file__ ))[0 ])
10
+ sys .path .insert (0 , os .path .abspath (os .path .join (os .pardir , "src" )))
11
+
12
+ import inputstream
13
+ import codecs
14
+
15
+ def parseTestcase (testString ):
16
+ testString = testString .split ("\n " )
17
+ try :
18
+ if testString [0 ] != "#data" :
19
+ sys .stderr .write (testString )
20
+ assert testString [0 ] == "#data"
21
+ except :
22
+ raise
23
+ input = []
24
+ encoding = []
25
+ currentList = input
26
+ for line in testString :
27
+ if line and not (line .startswith ("#encoding" ) or
28
+ line .startswith ("#data" )):
29
+ currentList .append (line )
30
+ elif line .startswith ("#encoding" ):
31
+ currentList = encoding
32
+ return "\n " .join (input ), encoding [0 ]
33
+
34
+ class TestCase (unittest .TestCase ):
35
+ def runEncodingTest (self , input , encoding ):
36
+ #XXX - move this out into the setup function
37
+ #concatenate all consecutive character tokens into a single token
38
+ stream = inputstream .HTMLInputStream (input )
39
+
40
+ errorMsg = "\n " .join (["\n \n Input" , input ,"\n Expected:" , encoding ,
41
+ "\n Recieved:" , stream .charEncoding ])
42
+ self .assertEquals (encoding .lower (), stream .charEncoding .lower (),
43
+ errorMsg )
44
+
45
+ def test_encoding ():
46
+ for filename in glob .glob ('encoding/*.dat' ):
47
+ f = open (filename )
48
+ tests = f .read ().split ("#data\n " )
49
+ for test in tests :
50
+ if test == "" :
51
+ continue
52
+ test = "#data\n " + test
53
+ input , encoding = parseTestcase (test )
54
+ yield TestCase .runEncodingTest , input , encoding
55
+
56
+ def buildTestSuite ():
57
+ tests = 0
58
+ for func , input , encoding in test_encoding ():
59
+ tests += 1
60
+ testName = 'test%d' % tests
61
+ testFunc = lambda self , method = func , input = input , encoding = encoding , \
62
+ : method (self , input , encoding )
63
+ testFunc .__doc__ = 'Encoding %s' % (testName )
64
+ instanceMethod = new .instancemethod (testFunc , None , TestCase )
65
+ setattr (TestCase , testName , instanceMethod )
66
+ return unittest .TestLoader ().loadTestsFromTestCase (TestCase )
67
+
68
+ def main ():
69
+ buildTestSuite ()
70
+ unittest .main ()
71
+
72
+ if __name__ == "__main__" :
73
+ main ()
0 commit comments