|
|
@@ -21,6 +21,7 @@ |
|
|
|
# SOFTWARE. |
|
|
|
|
|
|
|
from __future__ import unicode_literals |
|
|
|
from functools import partial |
|
|
|
import re |
|
|
|
from types import GeneratorType |
|
|
|
import unittest |
|
|
@@ -124,13 +125,12 @@ class TestWikicode(TreeEqualityTestCase): |
|
|
|
|
|
|
|
def _test_search(self, meth, expected): |
|
|
|
"""Base test for insert_before(), insert_after(), and replace().""" |
|
|
|
code = parse("{{a}}{{b}}{{c}}{{d}}") |
|
|
|
func = getattr(code, meth) |
|
|
|
code = parse("{{a}}{{b}}{{c}}{{d}}{{e}}") |
|
|
|
func = partial(meth, code) |
|
|
|
func("{{b}}", "x", recursive=True) |
|
|
|
func("{{d}}", "[[y]]", recursive=False) |
|
|
|
self.assertEqual(expected[0], code) |
|
|
|
func(code.get(2), "z") |
|
|
|
self.assertEqual(expected[1], code) |
|
|
|
self.assertEqual(expected[0], code) |
|
|
|
self.assertRaises(ValueError, func, "{{r}}", "n", recursive=True) |
|
|
|
self.assertRaises(ValueError, func, "{{r}}", "n", recursive=False) |
|
|
|
fake = parse("{{a}}").get(0) |
|
|
@@ -138,24 +138,24 @@ class TestWikicode(TreeEqualityTestCase): |
|
|
|
self.assertRaises(ValueError, func, fake, "n", recursive=False) |
|
|
|
|
|
|
|
code2 = parse("{{a}}{{a}}{{a}}{{b}}{{b}}{{b}}") |
|
|
|
func = getattr(code2, meth) |
|
|
|
func = partial(meth, code2) |
|
|
|
func(code2.get(1), "c", recursive=False) |
|
|
|
func("{{a}}", "d", recursive=False) |
|
|
|
func(code2.get(-1), "e", recursive=True) |
|
|
|
func("{{b}}", "f", recursive=True) |
|
|
|
self.assertEqual(expected[2], code2) |
|
|
|
self.assertEqual(expected[1], code2) |
|
|
|
|
|
|
|
code3 = parse("{{a|{{b}}|{{c|d={{f}}}}}}") |
|
|
|
func = getattr(code3, meth) |
|
|
|
func = partial(meth, code3) |
|
|
|
obj = code3.get(0).params[0].value.get(0) |
|
|
|
self.assertRaises(ValueError, func, obj, "x", recursive=False) |
|
|
|
func(obj, "x", recursive=True) |
|
|
|
self.assertRaises(ValueError, func, "{{f}}", "y", recursive=False) |
|
|
|
func("{{f}}", "y", recursive=True) |
|
|
|
self.assertEqual(expected[3], code3) |
|
|
|
self.assertEqual(expected[2], code3) |
|
|
|
|
|
|
|
code4 = parse("{{a}}{{b}}{{c}}{{d}}{{e}}{{f}}{{g}}") |
|
|
|
func = getattr(code4, meth) |
|
|
|
code4 = parse("{{a}}{{b}}{{c}}{{d}}{{e}}{{f}}{{g}}{{h}}{{i}}{{j}}") |
|
|
|
func = partial(meth, code4) |
|
|
|
fake = parse("{{b}}{{c}}") |
|
|
|
self.assertRaises(ValueError, func, fake, "q", recursive=False) |
|
|
|
self.assertRaises(ValueError, func, fake, "q", recursive=True) |
|
|
@@ -163,55 +163,59 @@ class TestWikicode(TreeEqualityTestCase): |
|
|
|
func("{{d}}{{e}}", "x", recursive=True) |
|
|
|
func(wrap(code4.nodes[-2:]), "y", recursive=False) |
|
|
|
func(wrap(code4.nodes[-2:]), "z", recursive=True) |
|
|
|
self.assertEqual(expected[4], code4) |
|
|
|
self.assertEqual(expected[3], code4) |
|
|
|
self.assertRaises(ValueError, func, "{{c}}{{d}}", "q", recursive=False) |
|
|
|
self.assertRaises(ValueError, func, "{{c}}{{d}}", "q", recursive=True) |
|
|
|
|
|
|
|
code5 = parse("{{a|{{b}}{{c}}|{{f|{{g}}={{h}}{{i}}}}}}") |
|
|
|
func = getattr(code5, meth) |
|
|
|
func = partial(meth, code5) |
|
|
|
self.assertRaises(ValueError, func, "{{b}}{{c}}", "x", recursive=False) |
|
|
|
func("{{b}}{{c}}", "x", recursive=True) |
|
|
|
obj = code5.get(0).params[1].value.get(0).params[0].value |
|
|
|
self.assertRaises(ValueError, func, obj, "y", recursive=False) |
|
|
|
func(obj, "y", recursive=True) |
|
|
|
self.assertEqual(expected[5], code5) |
|
|
|
self.assertEqual(expected[4], code5) |
|
|
|
|
|
|
|
code6 = parse("here is {{some text and a {{template}}}}") |
|
|
|
func = getattr(code6, meth) |
|
|
|
func = partial(meth, code6) |
|
|
|
self.assertRaises(ValueError, func, "text and", "ab", recursive=False) |
|
|
|
func("text and", "ab", recursive=True) |
|
|
|
self.assertRaises(ValueError, func, "is {{some", "cd", recursive=False) |
|
|
|
func("is {{some", "cd", recursive=True) |
|
|
|
self.assertEqual(expected[6], code6) |
|
|
|
self.assertEqual(expected[5], code6) |
|
|
|
|
|
|
|
def test_insert_before(self): |
|
|
|
"""test Wikicode.insert_before()""" |
|
|
|
meth = lambda code, *args, **kw: code.insert_before(*args, **kw) |
|
|
|
expected = [ |
|
|
|
"{{a}}x{{b}}{{c}}[[y]]{{d}}", "{{a}}xz{{b}}{{c}}[[y]]{{d}}", |
|
|
|
"{{a}}xz{{b}}{{c}}[[y]]{{d}}{{e}}", |
|
|
|
"d{{a}}cd{{a}}d{{a}}f{{b}}f{{b}}ef{{b}}", |
|
|
|
"{{a|x{{b}}|{{c|d=y{{f}}}}}}", |
|
|
|
"{{a}}w{{b}}{{c}}x{{d}}{{e}}yz{{f}}{{g}}", |
|
|
|
"{{a}}w{{b}}{{c}}x{{d}}{{e}}{{f}}{{g}}{{h}}yz{{i}}{{j}}", |
|
|
|
"{{a|x{{b}}{{c}}|{{f|{{g}}=y{{h}}{{i}}}}}}", |
|
|
|
"here cdis {{some abtext and a {{template}}}}"] |
|
|
|
self._test_search("insert_before", expected) |
|
|
|
self._test_search(meth, expected) |
|
|
|
|
|
|
|
def test_insert_after(self): |
|
|
|
"""test Wikicode.insert_after()""" |
|
|
|
meth = lambda code, *args, **kw: code.insert_after(*args, **kw) |
|
|
|
expected = [ |
|
|
|
"{{a}}{{b}}x{{c}}{{d}}[[y]]", "{{a}}{{b}}xz{{c}}{{d}}[[y]]", |
|
|
|
"{{a}}{{b}}xz{{c}}{{d}}[[y]]{{e}}", |
|
|
|
"{{a}}d{{a}}dc{{a}}d{{b}}f{{b}}f{{b}}fe", |
|
|
|
"{{a|{{b}}x|{{c|d={{f}}y}}}}", |
|
|
|
"{{a}}{{b}}{{c}}w{{d}}{{e}}x{{f}}{{g}}yz", |
|
|
|
"{{a}}{{b}}{{c}}w{{d}}{{e}}x{{f}}{{g}}{{h}}{{i}}{{j}}yz", |
|
|
|
"{{a|{{b}}{{c}}x|{{f|{{g}}={{h}}{{i}}y}}}}", |
|
|
|
"here is {{somecd text andab a {{template}}}}"] |
|
|
|
self._test_search("insert_after", expected) |
|
|
|
self._test_search(meth, expected) |
|
|
|
|
|
|
|
def test_replace(self): |
|
|
|
"""test Wikicode.replace()""" |
|
|
|
meth = lambda code, *args, **kw: code.replace(*args, **kw) |
|
|
|
expected = [ |
|
|
|
"{{a}}x{{c}}[[y]]", "{{a}}xz[[y]]", "dcdffe", "{{a|x|{{c|d=y}}}}", |
|
|
|
"{{a}}wz", "{{a|x|{{f|{{g}}=y}}}}", "here cd ab a {{template}}}}"] |
|
|
|
self._test_search("replace", expected) |
|
|
|
"{{a}}xz[[y]]{{e}}", "dcdffe", "{{a|x|{{c|d=y}}}}", |
|
|
|
"{{a}}wx{{f}}{{g}}z", "{{a|x|{{f|{{g}}=y}}}}", |
|
|
|
"here cd ab a {{template}}}}"] |
|
|
|
self._test_search(meth, expected) |
|
|
|
|
|
|
|
def test_append(self): |
|
|
|
"""test Wikicode.append()""" |
|
|
@@ -227,61 +231,12 @@ class TestWikicode(TreeEqualityTestCase): |
|
|
|
|
|
|
|
def test_remove(self): |
|
|
|
"""test Wikicode.remove()""" |
|
|
|
code = parse("{{a}}{{b}}{{c}}{{d}}") |
|
|
|
code.remove("{{b}}", recursive=True) |
|
|
|
code.remove(code.get(1), recursive=True) |
|
|
|
self.assertEqual("{{a}}{{d}}", code) |
|
|
|
self.assertRaises(ValueError, code.remove, "{{r}}", recursive=True) |
|
|
|
self.assertRaises(ValueError, code.remove, "{{r}}", recursive=False) |
|
|
|
fake = parse("{{a}}").get(0) |
|
|
|
self.assertRaises(ValueError, code.remove, fake, recursive=True) |
|
|
|
self.assertRaises(ValueError, code.remove, fake, recursive=False) |
|
|
|
|
|
|
|
code2 = parse("{{a}}{{a}}{{a}}{{b}}{{b}}{{b}}") |
|
|
|
code2.remove(code2.get(1), recursive=False) |
|
|
|
self.assertEqual("{{a}}{{a}}{{b}}{{b}}{{b}}", code2) |
|
|
|
code2.remove("{{a}}", recursive=False) |
|
|
|
self.assertEqual("{{b}}{{b}}{{b}}", code2) |
|
|
|
code2.remove(code2.get(-1), recursive=True) |
|
|
|
self.assertEqual("{{b}}{{b}}", code2) |
|
|
|
code2.remove("{{b}}", recursive=True) |
|
|
|
self.assertEqual("", code2) |
|
|
|
|
|
|
|
code3 = parse("{{a|{{b}}|{{c|d={{f}}}}}}") |
|
|
|
obj = code3.get(0).params[0].value.get(0) |
|
|
|
self.assertRaises(ValueError, code3.remove, obj, recursive=False) |
|
|
|
code3.remove(obj, recursive=True) |
|
|
|
self.assertRaises(ValueError, code3.remove, "{{f}}", recursive=False) |
|
|
|
code3.remove("{{f}}", recursive=True) |
|
|
|
self.assertEqual("{{a||{{c|d=}}}}", code3) |
|
|
|
|
|
|
|
code4 = parse("{{a}}{{b}}{{c}}{{d}}{{e}}{{f}}{{g}}{{h}}{{i}}{{j}}") |
|
|
|
fake = parse("{{b}}{{c}}") |
|
|
|
self.assertRaises(ValueError, code4.remove, fake, recursive=False) |
|
|
|
self.assertRaises(ValueError, code4.remove, fake, recursive=True) |
|
|
|
code4.remove("{{b}}{{c}}", recursive=False) |
|
|
|
code4.remove("{{d}}{{e}}", recursive=True) |
|
|
|
code4.remove(wrap(code4.nodes[-2:]), recursive=False) |
|
|
|
code4.remove(wrap(code4.nodes[-2:]), recursive=True) |
|
|
|
self.assertEqual("{{a}}{{f}}", code4) |
|
|
|
self.assertRaises(ValueError, code4.remove, "{{a}}{{b}}", False) |
|
|
|
self.assertRaises(ValueError, code4.remove, "{{a}}{{b}}", True) |
|
|
|
|
|
|
|
code5 = parse("{{a|{{b}}{{c}}|{{f|{{g}}={{h}}{{i}}}}}}") |
|
|
|
self.assertRaises(ValueError, code5.remove, "{{b}}{{c}}", False) |
|
|
|
code5.remove("{{b}}{{c}}", recursive=True) |
|
|
|
obj = code5.get(0).params[1].value.get(0).params[0].value |
|
|
|
self.assertRaises(ValueError, code5.remove, obj, recursive=False) |
|
|
|
code5.remove(obj, recursive=True) |
|
|
|
self.assertEqual("{{a||{{f|{{g}}=}}}}", code5) |
|
|
|
|
|
|
|
code6 = parse("here is {{some text and a {{template}}}}") |
|
|
|
func = code6.remove |
|
|
|
self.assertRaises(ValueError, func, "text and", recursive=False) |
|
|
|
func("text and", recursive=True) |
|
|
|
self.assertRaises(ValueError, func, "is {{some", recursive=False) |
|
|
|
func("is {{some", recursive=True) |
|
|
|
self.assertEqual("here a {{template}}}}", code6) |
|
|
|
meth = lambda code, obj, value, **kw: code.remove(obj, **kw) |
|
|
|
expected = [ |
|
|
|
"{{a}}{{c}}", "", "{{a||{{c|d=}}}}", "{{a}}{{f}}", |
|
|
|
"{{a||{{f|{{g}}=}}}}", "here a {{template}}}}" |
|
|
|
] |
|
|
|
self._test_search(meth, expected) |
|
|
|
|
|
|
|
def test_matches(self): |
|
|
|
"""test Wikicode.matches()""" |
|
|
|