forked from jcgregorio/uri-templates
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_draft_examples.py
executable file
·53 lines (44 loc) · 1.42 KB
/
test_draft_examples.py
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
import re
import sys
lines = file(sys.argv[1], "r").readlines()
del sys.argv[1]
vars_regex = re.compile(r"^\s*\| \S")
vars_txt = [[w.strip() for w in l.strip().split('|')][1:3] for l in lines if vars_regex.search(l)]
vars = {}
ucode_strings = re.compile(r'"([^"]*)"')
for (key, value) in vars_txt:
if value and "[" == value[0]:
value = ucode_strings.sub(r'u"\1"', value)
vars[key] = eval(value)
else:
vars[key] = eval('u"""%s"""' % value)
begin = [i for (i, line) in enumerate(lines) if line.startswith(" ----") > 0][0]
got_template = False
template = ""
expansion = ""
tests = []
for index in range(begin+1, len(lines)):
if lines[index].startswith(" ----"):
break
if lines[index].startswith(" "):
line = lines[index].strip()
if (line):
if got_template:
expansion = line
got_template = False
tests.append((template, expansion))
else:
template = line
got_template = True
if __name__ == "__main__":
import unittest
import sys
sys.path.append("..")
from template_parser import URITemplate
class Test(unittest.TestCase):
def test(self):
for (template, expected) in tests:
t = URITemplate(template)
expanded = t.sub(vars)
self.assertEqual(expanded, expected)
unittest.main()