/
test_parser.py
134 lines (102 loc) · 2.67 KB
/
test_parser.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
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
133
134
import os
import pytest
from mappyfile.parser import Parser
from mappyfile.pprint import PrettyPrinter
import mappyfile
from mappyfile.transformer import MapfileToDict
def output(s):
p = Parser()
m = MapfileToDict()
ast = p.parse(s)
print(ast)
d = m.transform(ast)
pp = PrettyPrinter(indent=0, newlinechar=" ")
return pp.pprint(d)
def test_all_maps():
sample_dir = os.path.join(os.path.dirname(__file__), "sample_maps")
p = Parser()
for fn in os.listdir(sample_dir):
ast = p.parse_file(os.path.join(sample_dir, fn))
ast.to_png_with_pydot(r'C:\Temp\Trees\%s.png' % os.path.basename(fn))
def test_includes():
p = Parser()
ast = p.parse_file('./tests/samples/include1.map')
m = MapfileToDict()
d = (m.transform(ast)) # works
print(mappyfile.dumps(d))
def test_layer():
s = "LAYER NAME 'Test' END"
assert(output(s) == s)
def test_class():
s = "CLASS NAME 'Test' END"
assert(output(s) == s)
def test_style():
s = "STYLE COLOR 0 0 255 WIDTH 5 LINECAP BUTT END"
assert(output(s) == s)
def test_style_pattern():
s = """
STYLE
COLOR 0 0 255
WIDTH 5
LINECAP BUTT
PATTERN 5.0 5.0 END
END
"""
exp = "STYLE COLOR 0 0 255 WIDTH 5 LINECAP BUTT PATTERN 5.0 5.0 END END"
assert(output(s) == exp)
def test_style_pattern2():
s = """
STYLE
COLOR 0 0 255
WIDTH 5
LINECAP BUTT
PATTERN
5.0 5.0
END
END
"""
exp = "STYLE COLOR 0 0 255 WIDTH 5 LINECAP BUTT PATTERN 5.0 5.0 END END"
assert(output(s) == exp)
def test_style_pattern3():
"""
This type of string fails
"""
s = "STYLE COLOR 0 0 255 WIDTH 5 LINECAP BUTT PATTERN 5 5 END END"
exp = "STYLE COLOR 0 0 255 WIDTH 5 LINECAP BUTT PATTERN 5 5 END END"
assert(output(s) == exp)
def test_style_pattern4():
"""
Fails
A single value on each line
Need new part to grammar?
| PATTERN NL+ ((int|float) NL*)* END
However this causes:
ParseError: Ambiguous parsing results (1024)
"""
s = """
STYLE
COLOR 0 0 255
WIDTH 5
LINECAP BUTT
PATTERN
5.0
5.0
END
END
"""
exp = "STYLE COLOR 0 0 255 WIDTH 5 LINECAP BUTT PATTERN 5.0 5.0 END END"
assert(output(s) == exp)
def xtest_metadata():
"""
Cannot parse metadata directly
"""
s = """METADATA 'wms_title' 'Test simple wms' END"""
assert(output(s) == s)
def run_tests():
pytest.main(["tests/test_parser.py::test_style_pattern"])
#pytest.main(["tests/test_parser.py"])
if __name__ == '__main__':
#run_tests()
#test_all_maps()
#test_includes()
test_style_pattern3()