浏览代码

Implement 'before' parameter for Template.add() (closes #21)

tags/v0.2
Ben Kurtovic 11 年前
父节点
当前提交
2ca3b2805e
共有 2 个文件被更改,包括 29 次插入4 次删除
  1. +8
    -3
      mwparserfromhell/nodes/template.py
  2. +21
    -1
      tests/test_template.py

+ 8
- 3
mwparserfromhell/nodes/template.py 查看文件

@@ -226,9 +226,9 @@ class Template(Node):
self.remove(name, keep_field=True)
existing = self.get(name)
if showkey is not None:
if not showkey:
self._surface_escape(value, "=")
existing.showkey = showkey
if not existing.showkey:
self._surface_escape(value, "=")
nodes = existing.value.nodes
if preserve_spacing:
existing.value = parse_anything([nodes[0], value, nodes[1]])
@@ -262,7 +262,12 @@ class Template(Node):
value = parse_anything([before_v, value, after_v])

param = Parameter(name, value, showkey)
self.params.append(param)
if before:
if not isinstance(before, Parameter):
before = self.get(before)
self.params.insert(self.params.index(before), param)
else:
self.params.append(param)
return param

def remove(self, name, keep_field=False):


+ 21
- 1
tests/test_template.py 查看文件

@@ -133,7 +133,27 @@ class TestTemplate(TreeEqualityTestCase):
self.assertIs(node3p2, node3.get("1"))
self.assertIs(node4p1, node4.get("b "))

# add
def test_add(self):
"""test Template.add()"""
# add new param with showkey to end
# add new param without showkey to end
# add new param to end with an escapable |
# add new param with showkey to end with an escapable =
# add new param without showkey to end with an escapable =
# add new param with showkey to end preserving spacing (x3)
# add new param without showkey to end not preserving spacing
# add new param guessing showkey where key is to be shown
# add new param guessing showkey where key is to be shown with an escapable =
# add new param guessing showkey where key is not to be shown
# add new param guessing showkey where key is not to be shown with an escapable =
# add existing parameter without modifying showkey
# add existing parameter without modifying showkey with an escapable =
# add existing parameter with modifying showkey
# add existing parameter with modifying showkey with an escapable =
# add existing parameter preserving spacing (x3)
# add existing parameter not preserving spacing
# add existing parameter when there are multiple params involved
# add existing parameter when there are multiple params involved; params with dependencies

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


正在加载...
取消
保存