3 from openid
.yadis
import accept
6 """Read the test data off of disk
10 filename
= os
.path
.join(os
.path
.dirname(__file__
), 'data', 'accept.txt')
13 for line
in file(filename
):
14 lines
.append((i
, line
))
19 """Return groups of lines separated by whitespace or comments
21 [(int, str)] -> [[(int, str)]]
25 for lineno
, line
in lines
:
26 stripped
= line
.strip()
27 if not stripped
or stripped
[0] == '#':
32 chunk
.append((lineno
, stripped
))
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)}
45 for (lineno
, line
) in chunk
:
46 header
, data
= line
.split(':', 1)
47 header
= header
.lower()
48 items
[header
] = (lineno
, data
.strip())
52 def parseAvailable(available_text
):
53 """Parse an Available: line's data
57 return [s
.strip() for s
in available_text
.split(',')]
59 def parseExpected(expected_text
):
60 """Parse an Expected: line's data
66 for chunk
in expected_text
.split(','):
68 mtype
, qstuff
= chunk
.split(';')
71 qstuff
= qstuff
.strip()
72 q
, qstr
= qstuff
.split('=')
75 expected
.append((mtype
, qval
))
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
87 def shortDescription(self
):
91 accepted
= accept
.parseAcceptHeader(self
.accept_header
)
92 actual
= accept
.matchTypes(accepted
, self
.available
)
93 self
.failUnlessEqual(self
.expected
, actual
)
98 data_sets
= map(parseLines
, chunks
)
100 for data
in data_sets
:
102 lno
, header
= data
['accept']
104 lno
, avail_data
= data
['available']
107 available
= parseAvailable(avail_data
)
112 lno
, exp_data
= data
['expected']
115 expected
= parseExpected(exp_data
)
120 descr
= 'MatchAcceptTest for lines %r' % (lnos
,)
121 case
= MatchAcceptTest(descr
, header
, available
, expected
)
123 return unittest
.TestSuite(cases
)
125 if __name__
== '__main__':
126 runner
= unittest
.TextTestRunner()
127 runner
.run(loadTests())