Quellcode durchsuchen

Fix a bug in Wikicode.set(); implement test_set() and test_index()

tags/v0.2
Ben Kurtovic vor 11 Jahren
Ursprung
Commit
ee99e6eceb
2 geänderte Dateien mit 27 neuen und 3 gelöschten Zeilen
  1. +2
    -1
      mwparserfromhell/wikicode.py
  2. +25
    -2
      tests/test_wikicode.py

+ 2
- 1
mwparserfromhell/wikicode.py Datei anzeigen

@@ -184,9 +184,10 @@ class Wikicode(StringMixIn):
raise ValueError("Cannot coerce multiple nodes into one index") raise ValueError("Cannot coerce multiple nodes into one index")
if index >= len(self.nodes) or -1 * index > len(self.nodes): if index >= len(self.nodes) or -1 * index > len(self.nodes):
raise IndexError("List assignment index out of range") raise IndexError("List assignment index out of range")
self.nodes.pop(index)
if nodes: if nodes:
self.nodes[index] = nodes[0] self.nodes[index] = nodes[0]
else:
self.nodes.pop(index)


def index(self, obj, recursive=False): def index(self, obj, recursive=False):
"""Return the index of *obj* in the list of nodes. """Return the index of *obj* in the list of nodes.


+ 25
- 2
tests/test_wikicode.py Datei anzeigen

@@ -66,11 +66,34 @@ class TestWikicode(TreeEqualityTestCase):


def test_set(self): def test_set(self):
"""test Wikicode.set()""" """test Wikicode.set()"""
pass
code = parse("Have a {{template}} and a [[page|link]]")
code.set(1, "{{{argument}}}")
self.assertEqual("Have a {{{argument}}} and a [[page|link]]", code)
self.assertIsInstance(code.get(1), Argument)
code.set(2, None)
self.assertEqual("Have a {{{argument}}}[[page|link]]", code)
code.set(-3, "This is an ")
self.assertEqual("This is an {{{argument}}}[[page|link]]", code)
self.assertRaises(ValueError, code.set, 1, "foo {{bar}}")
self.assertRaises(IndexError, code.set, 3, "{{baz}}")
self.assertRaises(IndexError, code.set, -4, "{{baz}}")


def test_index(self): def test_index(self):
"""test Wikicode.index()""" """test Wikicode.index()"""
pass
code = parse("Have a {{template}} and a [[page|link]]")
self.assertEqual(0, code.index("Have a "))
self.assertEqual(3, code.index("[[page|link]]"))
self.assertEqual(1, code.index(code.get(1)))
self.assertRaises(ValueError, code.index, "foo")

code = parse("{{foo}}{{bar|{{baz}}}}")
self.assertEqual(1, code.index("{{bar|{{baz}}}}"))
self.assertEqual(1, code.index("{{baz}}", recursive=True))
self.assertEqual(1, code.index(code.get(1).get(1).value,
recursive=True))
self.assertRaises(ValueError, code.index, "{{baz}}", recursive=False)
self.assertRaises(ValueError, code.index,
code.get(1).get(1).value, recursive=False)


def test_insert(self): def test_insert(self):
"""test Wikicode.insert()""" """test Wikicode.insert()"""


Laden…
Abbrechen
Speichern