1 from openid.consumer.html_parse import parseLinkAttrs
2 import os.path
3 import codecs
4 import unittest
5
7 parts = line.split()
8 optional = parts[0] == 'Link*:'
9 assert optional or parts[0] == 'Link:'
10
11 attrs = {}
12 for attr in parts[1:]:
13 k, v = attr.split('=', 1)
14 if k[-1] == '*':
15 attr_optional = 1
16 k = k[:-1]
17 else:
18 attr_optional = 0
19
20 attrs[k] = (attr_optional, v)
21
22 return (optional, attrs)
23
25 header, markup = s.split('\n\n', 1)
26 lines = header.split('\n')
27 name = lines.pop(0)
28 assert name.startswith('Name: ')
29 desc = name[6:]
30 return desc, markup, map(parseLink, lines)
31
33 tests = []
34
35 cases = s.split('\n\n\n')
36 header = cases.pop(0)
37 tests_line, _ = header.split('\n', 1)
38 k, v = tests_line.split(': ')
39 assert k == 'Num Tests'
40 num_tests = int(v)
41
42 for case in cases[:-1]:
43 desc, markup, links = parseCase(case)
44 tests.append((desc, markup, links, case))
45
46 return num_tests, tests
47
49 - def __init__(self, desc, case, expected, raw):
50 unittest.TestCase.__init__(self)
51 self.desc = desc
52 self.case = case
53 self.expected = expected
54 self.raw = raw
55
58
60 actual = parseLinkAttrs(self.case)
61 i = 0
62 for optional, exp_link in self.expected:
63 if optional:
64 if i >= len(actual):
65 continue
66
67 act_link = actual[i]
68 for k, (o, v) in exp_link.items():
69 if o:
70 act_v = act_link.get(k)
71 if act_v is None:
72 continue
73 else:
74 act_v = act_link[k]
75
76 if optional and v != act_v:
77 break
78
79 self.assertEqual(v, act_v)
80 else:
81 i += 1
82
83 assert i == len(actual)
84
86 here = os.path.dirname(os.path.abspath(__file__))
87 test_data_file_name = os.path.join(here, 'linkparse.txt')
88 test_data_file = codecs.open(test_data_file_name, 'r', 'utf-8')
89 test_data = test_data_file.read()
90 test_data_file.close()
91
92 num_tests, test_cases = parseTests(test_data)
93
94 tests = [_LinkTest(*case) for case in test_cases]
95
96 def test_parseSucceeded():
97 assert len(test_cases) == num_tests, (len(test_cases), num_tests)
98
99 check_desc = 'Check that we parsed the correct number of test cases'
100 check = unittest.FunctionTestCase(
101 test_parseSucceeded, description=check_desc)
102 tests.insert(0, check)
103
104 return unittest.TestSuite(tests)
105
106 if __name__ == '__main__':
107 suite = pyUnitTests()
108 runner = unittest.TextTestRunner()
109 runner.run(suite)
110