#!/usr/bin/python3.4
-# -*- coding: iso-8859-15 -*-
-import wrpylib.mwmarkup
import unittest
+import mwparserfromhell
+import wrpylib.mwmarkup
-class TestMwMarkup(unittest.TestCase):
+class TestMwParserFromHell(unittest.TestCase):
+ def setUp(self):
+ self.maxDiff = None
+
def test_find_template(self):
wikitext = '''== Allgemeines ==
{{Rodelbahnbox
| Position = 47.309820 N 9.986508 E
| Position oben =
- | Höhe oben = 1244
+ | Höhe oben = 1244
| Position unten =
- | Höhe unten = 806
- | Länge = 5045
+ | Höhe unten = 806
+ | Länge = 5045
| Schwierigkeit =
| Lawinen = gelegentlich
| Betreiber =
- | Öffentliche Anreise = Ja
+ | Öffentliche Anreise = Ja
| Gehzeit = 105
| Aufstieg getrennt = Nein
| Aufstiegshilfe = Nein
| Beleuchtungsanlage = Nein
| Beleuchtungstage =
| Rodelverleih = Ja
- | Gütesiegel =
+ | Gütesiegel =
| Webauskunft =
- | Telefonauskunft = +43-664-1808482 (Bergkristallhütte)
- | Bild = Rodelbahn Bergkristallhütte 2009-03-03.jpg
- | In Übersichtskarte = Ja
+ | Telefonauskunft = +43-664-1808482 (Bergkristallhütte)
+ | Bild = Rodelbahn Bergkristallhütte 2009-03-03.jpg
+ | In Übersichtskarte = Ja
| Forumid = 72
}}
- Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
- start, end = wrpylib.mwmarkup.find_template(wikitext, 'Rodelbahnbox')
- assert start == wikitext.find('{{')
- assert end == wikitext.find('}}')+2
+ Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
+ wikicode = mwparserfromhell.parse(wikitext)
+ rb = list(wikicode.filter_templates())[0]
+ self.assertEqual(rb.name.strip(), 'Rodelbahnbox')
+ self.assertEqual(rb.get('Aufstiegshilfe').value.strip(), 'Nein')
+ self.assertEqual(rb[:2], '{{')
+ self.assertEqual(rb[-2:], '}}')
+
+ def test_template_to_table(self):
+ wikitext = '{{Rodelbahnbox | Unbenannt | Position = 47.309820 N 9.986508 E | Aufstieg möglich = Ja }}'
+ wikicode = mwparserfromhell.parse(wikitext)
+ template = list(wikicode.filter_templates())[0]
+ self.assertEqual(template.name.strip(), 'Rodelbahnbox')
+ self.assertEqual(template.params[0].strip(), 'Unbenannt')
+ self.assertEqual(template.params[1].name.strip(), 'Position')
+ self.assertEqual(template.params[1].value.strip(), '47.309820 N 9.986508 E')
+ self.assertEqual(template.params[2].name.strip(), 'Aufstieg möglich')
+ self.assertEqual(template.params[2].value.strip(), 'Ja')
+ template = mwparserfromhell.nodes.template.Template('Rodelbahnbox')
+ template.add(1, 'Unbenannt')
+ template.add('Position', '47.309820 N 9.986508 E')
+ template.add('Aufstieg möglich', 'Ja')
+ self.assertEqual(template, '{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}')
- def test_TemplateValidator(self):
- v = wrpylib.mwmarkup.TemplateValidator()
- value = '{{Rodelbahnbox | Unbenannt | Position = 47.309820 N 9.986508 E | Aufstieg möglich = Ja }}'
- title, anonym_params, named_params = v.to_python(value)
- assert title == 'Rodelbahnbox'
- assert anonym_params == ['Unbenannt']
- assert list(named_params.keys()) == ['Position', 'Aufstieg möglich']
- assert list(named_params.values()) == ['47.309820 N 9.986508 E', 'Ja']
- value2 = v.from_python((title, anonym_params, named_params))
- assert value2 == '{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}'
- v = wrpylib.mwmarkup.TemplateValidator(as_table=True)
- value3 = v.from_python((title, anonym_params, named_params))
- assert value3 == \
- '{{Rodelbahnbox\n' + \
- '| Unbenannt\n' + \
- '| Position = 47.309820 N 9.986508 E\n' + \
- '| Aufstieg möglich = Ja\n' + \
- '}}'
- v = wrpylib.mwmarkup.TemplateValidator(strip=False)
- title, anonym_params, named_params = v.to_python(value)
- assert title == 'Rodelbahnbox '
- assert anonym_params == [' Unbenannt ']
- assert list(named_params.keys()) == [' Position ', ' Aufstieg möglich ']
- assert list(named_params.values()) == [' 47.309820 N 9.986508 E ', ' Ja ']
+ wrpylib.mwmarkup.format_template_table(template)
+ self.assertEqual(template,
+ '{{Rodelbahnbox\n' +
+ '| Unbenannt\n' +
+ '| Position = 47.309820 N 9.986508 E\n' +
+ '| Aufstieg möglich = Ja\n' +
+ '}}')
+ wrpylib.mwmarkup.format_template_table(template, 18)
+ self.assertEqual(template,
+ '{{Rodelbahnbox\n' +
+ '| Unbenannt\n' +
+ '| Position = 47.309820 N 9.986508 E\n' +
+ '| Aufstieg möglich = Ja\n' +
+ '}}')
def test_split_template(self):
wikitext = '''== Allgemeines ==
{{Rodelbahnbox
| Position = 47.309820 N 9.986508 E
| Position oben =
- | Höhe oben = 1244
+ | Höhe oben = 1244
| Position unten =
- | Höhe unten = 806
- | Länge = 5045
+ | Höhe unten = 806
+ | Länge = 5045
| Schwierigkeit =
| Lawinen = gelegentlich
| Betreiber =
- | Öffentliche Anreise = Ja
+ | Öffentliche Anreise = Ja
| Gehzeit = 105
| Aufstieg getrennt = Nein
| Aufstiegshilfe = Nein
| Beleuchtungsanlage = Nein
| Beleuchtungstage =
| Rodelverleih = Ja
- | Gütesiegel =
+ | Gütesiegel =
| Webauskunft =
- | Telefonauskunft = +43-664-1808482 (Bergkristallhütte)
- | Bild = Rodelbahn Bergkristallhütte 2009-03-03.jpg
- | In Übersichtskarte = Ja
+ | Telefonauskunft = +43-664-1808482 (Bergkristallhütte)
+ | Bild = Rodelbahn Bergkristallhütte 2009-03-03.jpg
+ | In Übersichtskarte = Ja
| Forumid = 72
}}
- Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
- start, end = wrpylib.mwmarkup.find_template(wikitext, 'Rodelbahnbox')
- template_title, parameters = wrpylib.mwmarkup.split_template(wikitext[start:end])
- assert template_title == 'Rodelbahnbox'
- assert len(parameters) == 22
- assert parameters['Position'] == '47.309820 N 9.986508 E'
- assert parameters['Telefonauskunft'] == '+43-664-1808482 (Bergkristallhütte)'
- assert parameters['Schwierigkeit'] == ''
-
+ Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
+ wikicode = mwparserfromhell.parse(wikitext)
+ template = wikicode.filter_templates(matches='Rodelbahnbox')[0]
+ self.assertEqual(template.name.strip(), 'Rodelbahnbox')
+ self.assertEqual(len(template.params), 22)
+ self.assertEqual(template.get('Position').value.strip(), '47.309820 N 9.986508 E')
+ self.assertEqual(template.get('Telefonauskunft').value.strip(), '+43-664-1808482 (Bergkristallhütte)')
+ self.assertEqual(template.get('Schwierigkeit').value.strip(), '')
def test_create_template(self):
- wikitext = '''{{Nicetemplate|Red|Bold|Position=Top|Alignment=Right}}'''
- template_title, parameters = wrpylib.mwmarkup.split_template(wikitext)
- keys = ['1', '2', 'Position', 'Alignment']
- values = [parameters[k] for k in keys]
- wikitext_generated = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:])
- wikitext_table = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:], True)
- assert wikitext_generated == wikitext
+ template = mwparserfromhell.nodes.template.Template('Rodelbahnbox')
+ template.add(1, 'Unbenannt')
+ template.add('Position', '47.309820 N 9.986508 E')
+ template.add('Aufstieg möglich', 'Ja')
+ self.assertEqual(template, '{{Rodelbahnbox|Unbenannt|Position=47.309820 N 9.986508 E|Aufstieg möglich=Ja}}')
+ wrpylib.mwmarkup.format_template_table(template)
+ self.assertEqual(template,
+ '{{Rodelbahnbox\n' +
+ '| Unbenannt\n' +
+ '| Position = 47.309820 N 9.986508 E\n' +
+ '| Aufstieg möglich = Ja\n' +
+ '}}')
def test_find_tag(self):
wikitext = 'This is <tag>my first tag</tag> and <tag>my second tag</tag>.'
- start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tags')
- assert (start, content, endtag, end) == (None, None, None, None)
- start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tag')
- assert (start, content, endtag, end) == (8, 13, 25, 31)
- start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tag', end)
- assert (start, content, endtag, end) == (36, 41, 54, 60)
- wikitext = 'This is <tag myattrib="4"/>.'
- start, content, endtag, end = wrpylib.mwmarkup.find_tag(wikitext, 'tag')
- assert (start, content, endtag, end) == (8, None, None, 27)
+ wikicode = mwparserfromhell.parse(wikitext)
+ tag_iter = wikicode.ifilter_tags()
+ tag = next(tag_iter)
+ self.assertEqual(tag.tag.strip(), 'tag')
+ self.assertEqual(tag.contents.strip(), 'my first tag')
+ tag = next(tag_iter)
+ self.assertEqual(tag.tag.strip(), 'tag')
+ self.assertEqual(tag.contents.strip(), 'my second tag')
+ wikitext = 'This is <tag myattrib="4"/>.'
+ wikicode = mwparserfromhell.parse(wikitext)
+ tag = next(wikicode.ifilter_tags())
+ self.assertEqual('tag', tag.tag)
- def test_parse_googlemap(self):
- wikitext = '''
- <googlemap version="0.9" lat="47.113291" lon="11.272337" zoom="15">
- (Parkplatz)47.114958,11.266026
- Erster Parkplatz
-
- (Gasthaus) 47.114715, 11.266262, Alt Bärnbad (Gasthaus)
- 6#FF014E9A
- 47.114715,11.266262
- 47.114135,11.268381
- 47.113421,11.269322
- 47.11277,11.269979
- 47.112408,11.271119
- </googlemap>
- '''
- attributes, coords, paths = wrpylib.mwmarkup.parse_googlemap(wikitext)
- assert attributes['lon'] == 11.272337
- assert attributes['lat'] == 47.113291
- assert attributes['zoom'] == 15
- assert coords == [
- (11.266026, 47.114958, 'Parkplatz', 'Erster Parkplatz'),
- (11.266262, 47.114715, 'Gasthaus', 'Alt Bärnbad (Gasthaus)')]
- assert paths == [
- ('6#FF014E9A', [
- (11.266262, 47.114715, None, None),
- (11.268381, 47.114135, None, None),
- (11.269322, 47.113421, None, None),
- (11.269979, 47.11277, None, None),
- (11.271119, 47.112408, None, None)])]
- try:
- result = wrpylib.mwmarkup.parse_googlemap(wikitext.replace('<googlemap', '|googlemap'))
- assert False
- except wrpylib.mwmarkup.ParseError:
- pass
+class TestFormatTemplate(unittest.TestCase):
+ def test_format_template_oneline(self):
+ value = mwparserfromhell.parse('{{ my_template || | var = 7 }}').filter_templates()[0]
+ wrpylib.mwmarkup.format_template_oneline(value)
+ self.assertEqual('{{my_template| | |var=7}}', value)