Added pulbic transport test.
[philipp/winterrodeln/wrpylib.git] / tests / test_mwmarkup.py
1 #!/usr/bin/python3.4
2 # coding=utf-8
3 import unittest
4 import mwparserfromhell
5 import wrpylib.mwmarkup
6
7
8 class TestMwMarkup(unittest.TestCase):
9     def test_find_template(self):
10         wikitext = '''== Allgemeines ==
11         {{Rodelbahnbox
12         | Position             = 47.309820 N 9.986508 E
13         | Position oben        =
14         | Höhe oben            = 1244
15         | Position unten       =
16         | Höhe unten           = 806
17         | Länge                = 5045
18         | Schwierigkeit        =
19         | Lawinen              = gelegentlich
20         | Betreiber            =
21         | Öffentliche Anreise  = Ja
22         | Gehzeit              = 105
23         | Aufstieg getrennt    = Nein
24         | Aufstiegshilfe       = Nein
25         | Beleuchtungsanlage   = Nein
26         | Beleuchtungstage     =
27         | Rodelverleih         = Ja
28         | Gütesiegel           =
29         | Webauskunft          =
30         | Telefonauskunft      = +43-664-1808482 (Bergkristallhütte)
31         | Bild                 = Rodelbahn Bergkristallhütte 2009-03-03.jpg
32         | In Übersichtskarte   = Ja
33         | Forumid              = 72
34         }}
35         Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
36         start, end = wrpylib.mwmarkup.find_template(wikitext, 'Rodelbahnbox')
37         self.assertEqual(start, wikitext.find('{{'))
38         self.assertEqual(end, wikitext.find('}}')+2)
39
40
41     def test_TemplateValidator(self):
42         v = wrpylib.mwmarkup.TemplateValidator()
43         value = '{{Rodelbahnbox | Unbenannt | Position = 47.309820 N 9.986508 E | Aufstieg möglich = Ja }}'
44         title, anonym_params, named_params = v.to_python(value)
45         self.assertEqual(title, 'Rodelbahnbox')
46         self.assertEqual(anonym_params, ['Unbenannt'])
47         self.assertEqual(list(named_params.keys()), ['Position', 'Aufstieg möglich'])
48         self.assertEqual(list(named_params.values()), ['47.309820 N 9.986508 E', 'Ja'])
49         value2 = v.from_python((title, anonym_params, named_params))
50         self.assertEqual(value2, '{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}')
51         v = wrpylib.mwmarkup.TemplateValidator(as_table=True)
52         value3 = v.from_python((title, anonym_params, named_params))
53         self.assertEqual(value3,
54             '{{Rodelbahnbox\n' +
55             '| Unbenannt\n' +
56             '| Position         = 47.309820 N 9.986508 E\n' +
57             '| Aufstieg möglich = Ja\n' +
58             '}}')
59         v = wrpylib.mwmarkup.TemplateValidator(strip=False)
60         title, anonym_params, named_params = v.to_python(value)
61         self.assertEqual(title, 'Rodelbahnbox ')
62         self.assertEqual(anonym_params, [' Unbenannt '])
63         self.assertEqual(list(named_params.keys()), [' Position ', ' Aufstieg möglich '])
64         self.assertEqual(list(named_params.values()), [' 47.309820 N 9.986508 E ', ' Ja '])
65
66
67     def test_split_template(self):
68         wikitext = '''== Allgemeines ==
69         {{Rodelbahnbox
70         | Position             = 47.309820 N 9.986508 E
71         | Position oben        =
72         | Höhe oben            = 1244
73         | Position unten       =
74         | Höhe unten           = 806
75         | Länge                = 5045
76         | Schwierigkeit        =
77         | Lawinen              = gelegentlich
78         | Betreiber            =
79         | Öffentliche Anreise  = Ja
80         | Gehzeit              = 105
81         | Aufstieg getrennt    = Nein
82         | Aufstiegshilfe       = Nein
83         | Beleuchtungsanlage   = Nein
84         | Beleuchtungstage     =
85         | Rodelverleih         = Ja
86         | Gütesiegel           =
87         | Webauskunft          =
88         | Telefonauskunft      = +43-664-1808482 (Bergkristallhütte)
89         | Bild                 = Rodelbahn Bergkristallhütte 2009-03-03.jpg
90         | In Übersichtskarte   = Ja
91         | Forumid              = 72
92         }}
93         Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
94         start, end = wrpylib.mwmarkup.find_template(wikitext, 'Rodelbahnbox')
95         template_title, parameters = wrpylib.mwmarkup.split_template(wikitext[start:end])
96         assert template_title == 'Rodelbahnbox'
97         assert len(parameters) == 22
98         assert parameters['Position'] == '47.309820 N 9.986508 E'
99         assert parameters['Telefonauskunft'] == '+43-664-1808482 (Bergkristallhütte)'
100         assert parameters['Schwierigkeit'] == ''
101
102
103     def test_create_template(self):
104         wikitext = '''{{Nicetemplate|Red|Bold|Position=Top|Alignment=Right}}'''
105         template_title, parameters = wrpylib.mwmarkup.split_template(wikitext)
106         keys = ['1', '2', 'Position', 'Alignment']
107         values = [parameters[k] for k in keys]
108         wikitext_generated = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:])
109         wikitext_table = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:], True)
110         assert wikitext_generated == wikitext
111
112
113     def test_find_tag(self):
114         wikitext = 'This is <tag>my first tag</tag> and <tag>my second tag</tag>.'
115         start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tags')
116         assert (start, content, endtag, end) == (None, None, None, None)
117         start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tag')
118         assert (start, content, endtag, end) == (8, 13, 25, 31)
119         start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tag', end)
120         assert (start, content, endtag, end) == (36, 41, 54, 60)
121         wikitext = 'This is <tag myattrib="4"/>.'
122         start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tag')
123         assert (start, content, endtag, end) == (8, None, None, 27)
124
125
126     def test_parse_googlemap(self):
127         wikitext = '''
128         <googlemap version="0.9" lat="47.113291" lon="11.272337" zoom="15">
129         (Parkplatz)47.114958,11.266026
130         Erster Parkplatz
131
132         (Gasthaus) 47.114715, 11.266262, Alt Bärnbad (Gasthaus)
133         6#FF014E9A
134         47.114715,11.266262
135         47.114135,11.268381
136         47.113421,11.269322
137         47.11277,11.269979
138         47.112408,11.271119
139         </googlemap>
140         '''
141         attributes, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
142         assert attributes['lon'] == 11.272337
143         assert attributes['lat'] == 47.113291
144         assert attributes['zoom'] == 15
145         assert coords == [
146             (11.266026, 47.114958, 'Parkplatz', 'Erster Parkplatz'),
147             (11.266262, 47.114715, 'Gasthaus', 'Alt Bärnbad (Gasthaus)')]
148         assert paths == [
149             ('6#FF014E9A', [
150                 (11.266262, 47.114715, None, None),
151                 (11.268381, 47.114135, None, None),
152                 (11.269322, 47.113421, None, None),
153                 (11.269979, 47.11277, None, None),
154                 (11.271119, 47.112408, None, None)])]
155         try:
156             result = wrpylib.mwmarkup.parse_googlemap(wikitext.replace('<googlemap', '|googlemap'))
157             assert False
158         except wrpylib.mwmarkup.ParseError:
159             pass
160
161
162 class TestMwParserFromHell(unittest.TestCase):
163     def test_find_template(self):
164         wikitext = '''== Allgemeines ==
165         {{Rodelbahnbox
166         | Position             = 47.309820 N 9.986508 E
167         | Position oben        =
168         | Höhe oben            = 1244
169         | Position unten       =
170         | Höhe unten           = 806
171         | Länge                = 5045
172         | Schwierigkeit        =
173         | Lawinen              = gelegentlich
174         | Betreiber            =
175         | Öffentliche Anreise  = Ja
176         | Gehzeit              = 105
177         | Aufstieg getrennt    = Nein
178         | Aufstiegshilfe       = Nein
179         | Beleuchtungsanlage   = Nein
180         | Beleuchtungstage     =
181         | Rodelverleih         = Ja
182         | Gütesiegel           =
183         | Webauskunft          =
184         | Telefonauskunft      = +43-664-1808482 (Bergkristallhütte)
185         | Bild                 = Rodelbahn Bergkristallhütte 2009-03-03.jpg
186         | In Übersichtskarte   = Ja
187         | Forumid              = 72
188         }}
189         Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
190         wikicode = mwparserfromhell.parse(wikitext)
191         rb = list(wikicode.filter_templates())[0]
192         self.assertEqual(rb.name.strip(), 'Rodelbahnbox')
193         self.assertEqual(rb.get('Aufstiegshilfe').value.strip(), 'Nein')
194         self.assertEqual(rb[:2], '{{')
195         self.assertEqual(rb[-2:], '}}')
196
197     def test_template_to_table(self):
198         wikitext = '{{Rodelbahnbox | Unbenannt | Position = 47.309820 N 9.986508 E | Aufstieg möglich = Ja }}'
199         wikicode = mwparserfromhell.parse(wikitext)
200         template = list(wikicode.filter_templates())[0]
201         self.assertEqual(template.name.strip(), 'Rodelbahnbox')
202         self.assertEqual(template.params[0].strip(), 'Unbenannt')
203         self.assertEqual(template.params[1].name.strip(), 'Position')
204         self.assertEqual(template.params[1].value.strip(), '47.309820 N 9.986508 E')
205         self.assertEqual(template.params[2].name.strip(), 'Aufstieg möglich')
206         self.assertEqual(template.params[2].value.strip(), 'Ja')
207
208         template = mwparserfromhell.nodes.template.Template('Rodelbahnbox')
209         template.add(1, 'Unbenannt')
210         template.add('Position', '47.309820 N 9.986508 E')
211         template.add('Aufstieg möglich', 'Ja')
212         self.assertEqual(template, '{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}')
213
214         wrpylib.mwmarkup.template_to_table(template)
215         self.assertEqual(template,
216             '{{Rodelbahnbox\n' +
217             '| Unbenannt\n' +
218             '| Position         = 47.309820 N 9.986508 E\n' +
219             '| Aufstieg möglich = Ja\n' +
220             '}}')
221
222         wrpylib.mwmarkup.template_to_table(template, 18)
223         self.assertEqual(template,
224             '{{Rodelbahnbox\n' +
225             '| Unbenannt\n' +
226             '| Position          = 47.309820 N 9.986508 E\n' +
227             '| Aufstieg möglich  = Ja\n' +
228             '}}')
229
230     def test_split_template(self):
231         wikitext = '''== Allgemeines ==
232         {{Rodelbahnbox
233         | Position             = 47.309820 N 9.986508 E
234         | Position oben        =
235         | Höhe oben            = 1244
236         | Position unten       =
237         | Höhe unten           = 806
238         | Länge                = 5045
239         | Schwierigkeit        =
240         | Lawinen              = gelegentlich
241         | Betreiber            =
242         | Öffentliche Anreise  = Ja
243         | Gehzeit              = 105
244         | Aufstieg getrennt    = Nein
245         | Aufstiegshilfe       = Nein
246         | Beleuchtungsanlage   = Nein
247         | Beleuchtungstage     =
248         | Rodelverleih         = Ja
249         | Gütesiegel           =
250         | Webauskunft          =
251         | Telefonauskunft      = +43-664-1808482 (Bergkristallhütte)
252         | Bild                 = Rodelbahn Bergkristallhütte 2009-03-03.jpg
253         | In Übersichtskarte   = Ja
254         | Forumid              = 72
255         }}
256         Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
257         wikicode = mwparserfromhell.parse(wikitext)
258         template = wikicode.filter_templates(matches='Rodelbahnbox')[0]
259         self.assertEqual(template.name.strip(), 'Rodelbahnbox')
260         self.assertEqual(len(template.params), 22)
261         self.assertEqual(template.get('Position').value.strip(), '47.309820 N 9.986508 E')
262         self.assertEqual(template.get('Telefonauskunft').value.strip(), '+43-664-1808482 (Bergkristallhütte)')
263         self.assertEqual(template.get('Schwierigkeit').value.strip(), '')
264
265     def test_create_template(self):
266         template = mwparserfromhell.nodes.template.Template('Rodelbahnbox')
267         template.add(1, 'Unbenannt')
268         template.add('Position', '47.309820 N 9.986508 E')
269         template.add('Aufstieg möglich', 'Ja')
270         self.assertEqual(template, '{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}')
271
272         wrpylib.mwmarkup.template_to_table(template)
273         self.assertEqual(template,
274             '{{Rodelbahnbox\n' +
275             '| Unbenannt\n' +
276             '| Position         = 47.309820 N 9.986508 E\n' +
277             '| Aufstieg möglich = Ja\n' +
278             '}}')
279
280     def test_find_tag(self):
281         wikitext = 'This is <tag>my first tag</tag> and <tag>my second tag</tag>.'
282         wikicode = mwparserfromhell.parse(wikitext)
283         tag_iter = wikicode.ifilter_tags()
284         tag = next(tag_iter)
285         self.assertEqual(tag.tag.strip(), 'tag')
286         self.assertEqual(tag.contents.strip(), 'my first tag')
287         tag = next(tag_iter)
288         self.assertEqual(tag.tag.strip(), 'tag')
289         self.assertEqual(tag.contents.strip(), 'my second tag')
290
291         wikitext = 'This is <tag myattrib="4"/>.'
292         wikicode = mwparserfromhell.parse(wikitext)
293         tag = next(wikicode.ifilter_tags())
294         self.assertEqual('tag', tag.tag)
295
296     def test_parse_googlemap(self):
297         wikitext = '''
298         <googlemap version="0.9" lat="47.113291" lon="11.272337" zoom="15">
299         (Parkplatz)47.114958,11.266026
300         Erster Parkplatz
301
302         (Gasthaus) 47.114715, 11.266262, Alt Bärnbad (Gasthaus)
303         6#FF014E9A
304         47.114715,11.266262
305         47.114135,11.268381
306         47.113421,11.269322
307         47.11277,11.269979
308         47.112408,11.271119
309         </googlemap>
310         '''
311         attributes, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
312         self.assertEqual(attributes['lon'], 11.272337)
313         self.assertEqual(attributes['lat'], 47.113291)
314         self.assertEqual(attributes['zoom'], 15)
315         self.assertEqual(coords, [
316             (11.266026, 47.114958, 'Parkplatz', 'Erster Parkplatz'),
317             (11.266262, 47.114715, 'Gasthaus', 'Alt Bärnbad (Gasthaus)')])
318         self.assertEqual(paths, [
319             ('6#FF014E9A', [
320                 (11.266262, 47.114715, None, None),
321                 (11.268381, 47.114135, None, None),
322                 (11.269322, 47.113421, None, None),
323                 (11.269979, 47.11277, None, None),
324                 (11.271119, 47.112408, None, None)])])
325         with self.assertRaises(wrpylib.mwmarkup.ParseError):
326             wrpylib.mwmarkup.parse_googlemap(wikitext.replace('<googlemap', '|googlemap'))