Selaa lähdekoodia

Template.has() should be passed ignore_empty=False by default.

**Breaking change**; closes #51.
tags/v0.3.3
Ben Kurtovic 11 vuotta sitten
vanhempi
commit
cf90557229
4 muutettua tiedostoa jossa 20 lisäystä ja 10 poistoa
  1. +3
    -0
      CHANGELOG
  2. +4
    -0
      docs/changelog.rst
  3. +2
    -2
      mwparserfromhell/nodes/template.py
  4. +11
    -8
      tests/test_template.py

+ 3
- 0
CHANGELOG Näytä tiedosto

@@ -1,5 +1,8 @@
v0.4 (unreleased):

- Template.has() is now passed 'ignore_empty=False' by default instead of True.
This fixes a bug when adding parameters to templates with empty fields, and
is a breaking change if you rely on the default behavior.
- 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


+ 4
- 0
docs/changelog.rst Näytä tiedosto

@@ -7,6 +7,10 @@ v0.4
Unreleased
(`changes <https://github.com/earwig/mwparserfromhell/compare/v0.3.2...develop>`__):

- :py:meth:`.Template.has` is now passed *ignore_empty=False* by default
instead of *True*. This fixes a bug when adding parameters to templates with
empty fields, **and is a breaking change if you rely on the default
behavior.**
- 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.


+ 2
- 2
mwparserfromhell/nodes/template.py Näytä tiedosto

@@ -174,7 +174,7 @@ class Template(Node):
def name(self, value):
self._name = parse_anything(value)

def has(self, name, ignore_empty=True):
def has(self, name, ignore_empty=False):
"""Return ``True`` if any parameter in the template is named *name*.

With *ignore_empty*, ``False`` will be returned even if the template
@@ -190,7 +190,7 @@ class Template(Node):
return True
return False

has_param = lambda self, name, ignore_empty=True: \
has_param = lambda self, name, ignore_empty=False: \
self.has(name, ignore_empty)
has_param.__doc__ = "Alias for :py:meth:`has`."



+ 11
- 8
tests/test_template.py Näytä tiedosto

@@ -123,15 +123,15 @@ class TestTemplate(TreeEqualityTestCase):
node3 = Template(wraptext("foo"),
[pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")])
node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")])
self.assertFalse(node1.has("foobar"))
self.assertTrue(node2.has(1))
self.assertTrue(node2.has("abc"))
self.assertFalse(node2.has("def"))
self.assertTrue(node3.has("1"))
self.assertTrue(node3.has(" b "))
self.assertFalse(node4.has("b"))
self.assertTrue(node3.has("b", False))
self.assertFalse(node1.has("foobar", False))
self.assertTrue(node2.has(1, False))
self.assertTrue(node2.has("abc", False))
self.assertFalse(node2.has("def", False))
self.assertTrue(node3.has("1", False))
self.assertTrue(node3.has(" b ", False))
self.assertTrue(node4.has("b", False))
self.assertTrue(node3.has("b", True))
self.assertFalse(node4.has("b", True))

def test_get(self):
"""test Template.get()"""
@@ -223,6 +223,7 @@ class TestTemplate(TreeEqualityTestCase):
pgenh("1", "c"), pgenh("2", "d")])
node40 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"),
pgens("f", "g")])
node41 = Template(wraptext("a"), [pgenh("1", "")])

node1.add("e", "f", showkey=True)
node2.add(2, "g", showkey=False)
@@ -266,6 +267,7 @@ class TestTemplate(TreeEqualityTestCase):
node38.add("1", "e")
node39.add("1", "e")
node40.add("d", "h", before="b")
node41.add(1, "b")

self.assertEqual("{{a|b=c|d|e=f}}", node1)
self.assertEqual("{{a|b=c|d|g}}", node2)
@@ -312,6 +314,7 @@ class TestTemplate(TreeEqualityTestCase):
self.assertEqual("{{a|1=e|x=y|2=d}}", node38)
self.assertEqual("{{a|x=y|e|d}}", node39)
self.assertEqual("{{a|b=c|d=h|f=g}}", node40)
self.assertEqual("{{a|b}}", node41)

def test_remove(self):
"""test Template.remove()"""


Ladataan…
Peruuta
Tallenna