Package openid :: Package test :: Module test_parsehtml
[frames] | no frames]

Source Code for Module openid.test.test_parsehtml

 1  from openid.yadis.parsehtml import YadisHTMLParser, ParseDone 
 2  from HTMLParser import HTMLParseError 
 3   
 4  import os.path, unittest, sys 
 5   
6 -class _TestCase(unittest.TestCase):
7 reserved_values = ['None', 'EOF'] 8
9 - def __init__(self, filename, testname, expected, case):
10 self.filename = filename 11 self.testname = testname 12 self.expected = expected 13 self.case = case 14 unittest.TestCase.__init__(self)
15
16 - def runTest(self):
17 p = YadisHTMLParser() 18 try: 19 p.feed(self.case) 20 except ParseDone, why: 21 found = why[0] 22 23 # make sure we protect outselves against accidental bogus 24 # test cases 25 assert found not in self.reserved_values 26 27 # convert to a string 28 if found is None: 29 found = 'None' 30 31 msg = "%r != %r for case %s" % (found, self.expected, self.case) 32 self.failUnlessEqual(found, self.expected, msg) 33 except HTMLParseError: 34 self.failUnless(self.expected == 'None', (self.case, self.expected)) 35 else: 36 self.failUnless(self.expected == 'EOF', (self.case, self.expected))
37
38 - def shortDescription(self):
39 return "%s (%s<%s>)" % ( 40 self.testname, 41 self.__class__.__module__, 42 os.path.basename(self.filename))
43
44 -def parseCases(data):
45 cases = [] 46 for chunk in data.split('\f\n'): 47 expected, case = chunk.split('\n', 1) 48 cases.append((expected, case)) 49 return cases
50
51 -def pyUnitTests():
52 """Make a pyunit TestSuite from a file defining test cases.""" 53 s = unittest.TestSuite() 54 for (filename, test_num, expected, case) in getCases(): 55 s.addTest(_TestCase(filename, str(test_num), expected, case)) 56 return s
57
58 -def test():
59 runner = unittest.TextTestRunner() 60 return runner.run(loadTests())
61 62 filenames = ['data/test1-parsehtml.txt'] 63 64 default_test_files = [] 65 base = os.path.dirname(__file__) 66 for filename in filenames: 67 full_name = os.path.join(base, filename) 68 default_test_files.append(full_name) 69
70 -def getCases(test_files=default_test_files):
71 cases = [] 72 for filename in test_files: 73 test_num = 0 74 data = file(filename).read() 75 for expected, case in parseCases(data): 76 test_num += 1 77 cases.append((filename, test_num, expected, case)) 78 return cases
79 80 81 if __name__ == '__main__': 82 sys.exit(not test().wasSuccessful()) 83