From d500f8972e8a3ae0bfee706d40b76b3bfa1fc00d Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 16 Feb 2013 13:01:41 -0500 Subject: [PATCH] Add a few more tests; use assert*(expected, actual) instead of opposite. --- tests/_test_tokenizer.py | 7 +++-- tests/test_docs.py | 6 ++-- tests/tokenizer/templates.test | 68 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/tests/_test_tokenizer.py b/tests/_test_tokenizer.py index bef7569..114b835 100644 --- a/tests/_test_tokenizer.py +++ b/tests/_test_tokenizer.py @@ -47,8 +47,9 @@ class TokenizerTestCase(object): *label* for the method's docstring. """ def inner(self): + expected = data["output"] actual = self.tokenizer().tokenize(data["input"]) - self.assertEqual(actual, data["output"]) + self.assertEqual(expected, actual) if not py3k: inner.__name__ = funcname.encode("utf8") inner.__doc__ = data["label"] @@ -61,7 +62,7 @@ class TokenizerTestCase(object): counter = 1 digits = len(str(len(tests))) for test in tests: - data = {"name": "", "label": "", "input": "", "output": []} + data = {"name": None, "label": None, "input": None, "output": None} try: for line in test.strip().splitlines(): if line.startswith("name:"): @@ -92,7 +93,7 @@ class TokenizerTestCase(object): error = "A test in '{0}' was ignored because it lacked a name" print(error.format(filename)) 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" print(error.format(data["name"], filename)) continue diff --git a/tests/test_docs.py b/tests/test_docs.py index d99652f..8673cb9 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -35,7 +35,7 @@ class TestDocs(unittest.TestCase): buff = StringIO() print(input, end="", file=buff) buff.seek(0) - self.assertEqual(buff.read(), output) + self.assertEqual(output, buff.read()) def test_readme_1(self): """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() res = json.loads(raw) text = res["query"]["pages"].values()[0]["revisions"][0]["*"] - actual = mwparserfromhell.parse(text) expected = urllib.urlopen(url2.format(title)).read().decode("utf8") - self.assertEqual(actual, expected) + actual = mwparserfromhell.parse(text) + self.assertEqual(expected, actual) if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/tests/tokenizer/templates.test b/tests/tokenizer/templates.test index 7399022..348e1f5 100644 --- a/tests/tokenizer/templates.test +++ b/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|bar\n}}" @@ -300,6 +345,11 @@ name: incomplete_tests "{{[foobar}}" "{{foobar]}}" +"{{foo|ba{r}}" +"{{foo|ba{r}}}" +"{{foo|ba{r}=baz}}" +"{{foo|ba[r]}}" + "{{foobar" "{{foobar}" "{{foobar|"