From 5e6c994c2cbc816bd1d8107726b10b1c049d7aad Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 21 Sep 2013 14:48:05 -0400 Subject: [PATCH] Wikicode.matches() now accepts tuples (closes #48) --- CHANGELOG | 2 ++ docs/changelog.rst | 3 +++ mwparserfromhell/wikicode.py | 18 ++++++++++++------ tests/test_wikicode.py | 8 ++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 230236b..43309f6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,8 @@ v0.4 (unreleased): - The 'matches' argument of Wikicode's filter methods now accepts a function (taking one argument, a Node, and returning a bool) in addition to a regex. +- Wikicode.matches() now accepts a tuple of strings/Wikicode objects instead of + just a single string or Wikicode. v0.3.2 (released September 1, 2013): diff --git a/docs/changelog.rst b/docs/changelog.rst index ed8372d..7ac5c9a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -10,6 +10,9 @@ Unreleased - The *matches* argument of :py:class:`Wikicode's <.Wikicode>` :py:meth:`.filter` methods now accepts a function (taking one argument, a :py:class:`.Node`, and returning a bool) in addition to a regex. +- :py:meth:`.Wikicode.matches` now accepts a tuple of + strings/:py:class:`.Wikicode` objects instead of just a single string or + :py:class:`.Wikicode`. v0.3.2 ------ diff --git a/mwparserfromhell/wikicode.py b/mwparserfromhell/wikicode.py index be751ed..b7d2ab2 100644 --- a/mwparserfromhell/wikicode.py +++ b/mwparserfromhell/wikicode.py @@ -362,16 +362,22 @@ class Wikicode(StringMixIn): """Do a loose equivalency test suitable for comparing page names. *other* can be any string-like object, including - :py:class:`~.Wikicode`. This operation is symmetric; both sides are - adjusted. Specifically, whitespace and markup is stripped and the first - letter's case is normalized. Typical usage is + :py:class:`~.Wikicode`, or a tuple of these. This operation is + symmetric; both sides are adjusted. Specifically, whitespace and markup + is stripped and the first letter's case is normalized. Typical usage is ``if template.name.matches("stub"): ...``. """ + cmp = lambda a, b: (a[0].upper() + a[1:] == b[0].upper() + b[1:] + if a and b else a == b) this = self.strip_code().strip() + if isinstance(other, tuple): + for obj in other: + that = parse_anything(obj).strip_code().strip() + if cmp(this, that): + return True + return False that = parse_anything(other).strip_code().strip() - if not this or not that: - return this == that - return this[0].upper() + this[1:] == that[0].upper() + that[1:] + return cmp(this, that) def ifilter(self, recursive=True, matches=None, flags=FLAGS, forcetype=None): diff --git a/tests/test_wikicode.py b/tests/test_wikicode.py index 31fa82f..632c6b4 100644 --- a/tests/test_wikicode.py +++ b/tests/test_wikicode.py @@ -242,6 +242,7 @@ class TestWikicode(TreeEqualityTestCase): """test Wikicode.matches()""" code1 = parse("Cleanup") code2 = parse("\nstub") + code3 = parse("") self.assertTrue(code1.matches("Cleanup")) self.assertTrue(code1.matches("cleanup")) self.assertTrue(code1.matches(" cleanup\n")) @@ -250,6 +251,13 @@ class TestWikicode(TreeEqualityTestCase): self.assertTrue(code2.matches("stub")) self.assertTrue(code2.matches("Stub")) self.assertFalse(code2.matches("StuB")) + self.assertTrue(code1.matches(("cleanup", "stub"))) + self.assertTrue(code2.matches(("cleanup", "stub"))) + self.assertFalse(code2.matches(("StuB", "sTUb", "foobar"))) + self.assertTrue(code2.matches(("StuB", "sTUb", "foo", "bar", "Stub"))) + self.assertTrue(code3.matches("")) + self.assertTrue(code3.matches("")) + self.assertTrue(code3.matches(("a", "b", ""))) def test_filter_family(self): """test the Wikicode.i?filter() family of functions"""