瀏覽代碼

Add a few more tests; use assert*(expected, actual) instead of opposite.

tags/v0.2
Ben Kurtovic 11 年之前
父節點
當前提交
d500f8972e
共有 3 個檔案被更改,包括 66 行新增15 行删除
  1. +4
    -3
      tests/_test_tokenizer.py
  2. +3
    -3
      tests/test_docs.py
  3. +59
    -9
      tests/tokenizer/templates.test

+ 4
- 3
tests/_test_tokenizer.py 查看文件

@@ -47,8 +47,9 @@ class TokenizerTestCase(object):
*label* for the method's docstring. *label* for the method's docstring.
""" """
def inner(self): def inner(self):
expected = data["output"]
actual = self.tokenizer().tokenize(data["input"]) actual = self.tokenizer().tokenize(data["input"])
self.assertEqual(actual, data["output"])
self.assertEqual(expected, actual)
if not py3k: if not py3k:
inner.__name__ = funcname.encode("utf8") inner.__name__ = funcname.encode("utf8")
inner.__doc__ = data["label"] inner.__doc__ = data["label"]
@@ -61,7 +62,7 @@ class TokenizerTestCase(object):
counter = 1 counter = 1
digits = len(str(len(tests))) digits = len(str(len(tests)))
for test in tests: for test in tests:
data = {"name": "", "label": "", "input": "", "output": []}
data = {"name": None, "label": None, "input": None, "output": None}
try: try:
for line in test.strip().splitlines(): for line in test.strip().splitlines():
if line.startswith("name:"): if line.startswith("name:"):
@@ -92,7 +93,7 @@ class TokenizerTestCase(object):
error = "A test in '{0}' was ignored because it lacked a name" error = "A test in '{0}' was ignored because it lacked a name"
print(error.format(filename)) print(error.format(filename))
continue continue
if not data["input"] or not data["output"]:
if data["input"] is None or data["output"] is None:
error = "Test '{0}' in '{1}' was ignored because it lacked an input or an output" error = "Test '{0}' in '{1}' was ignored because it lacked an input or an output"
print(error.format(data["name"], filename)) print(error.format(data["name"], filename))
continue continue


+ 3
- 3
tests/test_docs.py 查看文件

@@ -35,7 +35,7 @@ class TestDocs(unittest.TestCase):
buff = StringIO() buff = StringIO()
print(input, end="", file=buff) print(input, end="", file=buff)
buff.seek(0) buff.seek(0)
self.assertEqual(buff.read(), output)
self.assertEqual(output, buff.read())


def test_readme_1(self): def test_readme_1(self):
"""test a block of example code in the README""" """test a block of example code in the README"""
@@ -115,9 +115,9 @@ class TestDocs(unittest.TestCase):
raw = urllib.urlopen(url1, urllib.urlencode(data)).read() raw = urllib.urlopen(url1, urllib.urlencode(data)).read()
res = json.loads(raw) res = json.loads(raw)
text = res["query"]["pages"].values()[0]["revisions"][0]["*"] text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
actual = mwparserfromhell.parse(text)
expected = urllib.urlopen(url2.format(title)).read().decode("utf8") expected = urllib.urlopen(url2.format(title)).read().decode("utf8")
self.assertEqual(actual, expected)
actual = mwparserfromhell.parse(text)
self.assertEqual(expected, actual)


if __name__ == "__main__": if __name__ == "__main__":
unittest.main(verbosity=2) unittest.main(verbosity=2)

+ 59
- 9
tests/tokenizer/templates.test 查看文件

@@ -208,17 +208,62 @@ output: [TemplateOpen(), TemplateOpen(), TemplateOpen(), TemplateOpen(), Text(te


--- ---


name: incomplete_tests
name: mixed_nested_templates
label: mixed assortment of nested templates within template names, parameter names, and values
input: "{{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}"
output: [TemplateOpen(), TemplateOpen(), TemplateOpen(), TemplateOpen(), Text(text="foo"), TemplateClose(), Text(text="bar"), TemplateParamSeparator(), Text(text="baz"), TemplateParamEquals(), Text(text="biz"), TemplateClose(), Text(text="buzz"), TemplateClose(), Text(text="usr"), TemplateParamSeparator(), TemplateOpen(), Text(text="bin"), TemplateClose(), TemplateClose()]

---

name: newline_start
label: a newline at the start of a template name
input: "{{\nfoobar}}"
output: [TemplateOpen(), Text(text="\nfoobar"), TemplateClose()]

---

name: newline_end
label: a newline at the end of a template name
input: "{{foobar\n}}"
output: [TemplateOpen(), Text(text="foobar\n"), TemplateClose()]

---

name: newline_start_end
label: a newline at the start and end of a template name
input: "{{\nfoobar\n}}"
output: [TemplateOpen(), Text(text="\nfoobar\n"), TemplateClose()]

---


"{{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}"
name: newline_mid
label: a newline at the middle of a template name
input: "{{foo\nbar}}"
output: [Text(text="{{foo\nbar}}")]


"{{\nfoobar}}"
"{{foobar\n}}"
"{{\nfoobar\n}}"
"{{foo\nbar}}"
"{{\nfoo\nbar}}"
"{{foo\nbar\n}}"
"{{\nfoo\nbar\n}}"
---

name: newline_start_mid
label: a newline at the start and middle of a template name
input: "{{\nfoo\nbar}}"
output: [Text(text="{{\nfoo\nbar}}")]

---

name: newline_mid_end
label: a newline at the middle and end of a template name
input: "{{foo\nbar\n}}"
output: [Text(text="{{foo\nbar\n}}")]

---

name: newline_start_mid_end
label: a newline at the start, middle, and end of a template name
input: "{{\nfoo\nbar\n}}"
output: [Text(text="{{\nfoo\nbar\n}}")]

---
name: incomplete_tests


"{{foo|\nbar}}" "{{foo|\nbar}}"
"{{foo|bar\n}}" "{{foo|bar\n}}"
@@ -300,6 +345,11 @@ name: incomplete_tests
"{{[foobar}}" "{{[foobar}}"
"{{foobar]}}" "{{foobar]}}"


"{{foo|ba{r}}"
"{{foo|ba{r}}}"
"{{foo|ba{r}=baz}}"
"{{foo|ba[r]}}"

"{{foobar" "{{foobar"
"{{foobar}" "{{foobar}"
"{{foobar|" "{{foobar|"


Loading…
取消
儲存