getting file size for all dict files to be downloaded. coming to be 400mb or so.
[worddb.git] / libs / openid / test / test_accept.py
blob3cc36025cdfa6a06617e0f96546772142edf6e09
1 import unittest
2 import os.path
3 from openid.yadis import accept
5 def getTestData():
6 """Read the test data off of disk
8 () -> [(int, str)]
9 """
10 filename = os.path.join(os.path.dirname(__file__), 'data', 'accept.txt')
11 i = 1
12 lines = []
13 for line in file(filename):
14 lines.append((i, line))
15 i += 1
16 return lines
18 def chunk(lines):
19 """Return groups of lines separated by whitespace or comments
21 [(int, str)] -> [[(int, str)]]
22 """
23 chunks = []
24 chunk = []
25 for lineno, line in lines:
26 stripped = line.strip()
27 if not stripped or stripped[0] == '#':
28 if chunk:
29 chunks.append(chunk)
30 chunk = []
31 else:
32 chunk.append((lineno, stripped))
34 if chunk:
35 chunks.append(chunk)
37 return chunks
39 def parseLines(chunk):
40 """Take the given chunk of lines and turn it into a test data dictionary
42 [(int, str)] -> {str:(int, str)}
43 """
44 items = {}
45 for (lineno, line) in chunk:
46 header, data = line.split(':', 1)
47 header = header.lower()
48 items[header] = (lineno, data.strip())
50 return items
52 def parseAvailable(available_text):
53 """Parse an Available: line's data
55 str -> [str]
56 """
57 return [s.strip() for s in available_text.split(',')]
59 def parseExpected(expected_text):
60 """Parse an Expected: line's data
62 str -> [(str, float)]
63 """
64 expected = []
65 if expected_text:
66 for chunk in expected_text.split(','):
67 chunk = chunk.strip()
68 mtype, qstuff = chunk.split(';')
69 mtype = mtype.strip()
70 assert '/' in mtype
71 qstuff = qstuff.strip()
72 q, qstr = qstuff.split('=')
73 assert q == 'q'
74 qval = float(qstr)
75 expected.append((mtype, qval))
77 return expected
79 class MatchAcceptTest(unittest.TestCase):
80 def __init__(self, descr, accept_header, available, expected):
81 unittest.TestCase.__init__(self)
82 self.accept_header = accept_header
83 self.available = available
84 self.expected = expected
85 self.descr = descr
87 def shortDescription(self):
88 return self.descr
90 def runTest(self):
91 accepted = accept.parseAcceptHeader(self.accept_header)
92 actual = accept.matchTypes(accepted, self.available)
93 self.failUnlessEqual(self.expected, actual)
95 def pyUnitTests():
96 lines = getTestData()
97 chunks = chunk(lines)
98 data_sets = map(parseLines, chunks)
99 cases = []
100 for data in data_sets:
101 lnos = []
102 lno, header = data['accept']
103 lnos.append(lno)
104 lno, avail_data = data['available']
105 lnos.append(lno)
106 try:
107 available = parseAvailable(avail_data)
108 except:
109 print 'On line', lno
110 raise
112 lno, exp_data = data['expected']
113 lnos.append(lno)
114 try:
115 expected = parseExpected(exp_data)
116 except:
117 print 'On line', lno
118 raise
120 descr = 'MatchAcceptTest for lines %r' % (lnos,)
121 case = MatchAcceptTest(descr, header, available, expected)
122 cases.append(case)
123 return unittest.TestSuite(cases)
125 if __name__ == '__main__':
126 runner = unittest.TextTestRunner()
127 runner.run(loadTests())