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

Source Code for Module openid.test.test_accept

  1  import unittest 
  2  import os.path 
  3  from openid.yadis import accept 
  4   
5 -def getTestData():
6 """Read the test data off of disk 7 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
17
18 -def chunk(lines):
19 """Return groups of lines separated by whitespace or comments 20 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)) 33 34 if chunk: 35 chunks.append(chunk) 36 37 return chunks
38
39 -def parseLines(chunk):
40 """Take the given chunk of lines and turn it into a test data dictionary 41 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()) 49 50 return items
51
52 -def parseAvailable(available_text):
53 """Parse an Available: line's data 54 55 str -> [str] 56 """ 57 return [s.strip() for s in available_text.split(',')]
58
59 -def parseExpected(expected_text):
60 """Parse an Expected: line's data 61 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)) 76 77 return expected
78
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
86
87 - def shortDescription(self):
88 return self.descr
89
90 - def runTest(self):
91 accepted = accept.parseAcceptHeader(self.accept_header) 92 actual = accept.matchTypes(accepted, self.available) 93 self.failUnlessEqual(self.expected, actual)
94
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 111 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 119 120 descr = 'MatchAcceptTest for lines %r' % (lnos,) 121 case = MatchAcceptTest(descr, header, available, expected) 122 cases.append(case) 123 return unittest.TestSuite(cases)
124 125 if __name__ == '__main__': 126 runner = unittest.TextTestRunner() 127 runner.run(loadTests()) 128