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