Ver a proveniência

Restarting work on unit tests with a test covering doc examples.

tags/v0.2
Ben Kurtovic há 11 anos
ascendente
cometimento
28b124a96c
6 ficheiros alterados com 122 adições e 289 eliminações
  1. +3
    -1
      README.rst
  2. +2
    -0
      mwparserfromhell/compat.py
  3. +117
    -0
      tests/test_docs.py
  4. +0
    -119
      tests/test_parameter.py
  5. +0
    -63
      tests/test_parser.py
  6. +0
    -106
      tests/test_template.py

+ 3
- 1
README.rst Ver ficheiro

@@ -124,7 +124,9 @@ following code (via the API_)::
import mwparserfromhell
API_URL = "http://en.wikipedia.org/w/api.php"
def parse(title):
raw = urllib.urlopen(API_URL, data).read()
data = {"action": "query", "prop": "revisions", "rvlimit": 1,
"rvprop": "content", "format": "json", "titles": title}
raw = urllib.urlopen(API_URL, urllib.urlencode(data)).read()
res = json.loads(raw)
text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
return mwparserfromhell.parse(text)


+ 2
- 0
mwparserfromhell/compat.py Ver ficheiro

@@ -18,6 +18,7 @@ if py3k:
basestring = str
maxsize = sys.maxsize
import html.entities as htmlentities
from io import StringIO
else:
bytes = str
@@ -25,5 +26,6 @@ else:
basestring = basestring
maxsize = sys.maxint
import htmlentitydefs as htmlentities
from StringIO import StringIO
del sys

+ 117
- 0
tests/test_docs.py Ver ficheiro

@@ -0,0 +1,117 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function, unicode_literals
import json
import unittest
import urllib

import mwparserfromhell
from mwparserfromhell.compat import py3k, str, StringIO

class TestDocs(unittest.TestCase):
def assertPrint(self, input, output):
"""Assertion check that *input*, when printed, produces *output*."""
buff = StringIO()
print(input, end="", file=buff)
buff.seek(0)
self.assertEqual(buff.read(), output)

def test_readme_1(self):
text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?"
wikicode = mwparserfromhell.parse(text)
self.assertPrint(wikicode,
"I has a template! {{foo|bar|baz|eggs=spam}} See it?")
templates = wikicode.filter_templates()
if py3k:
self.assertPrint(templates, "['{{foo|bar|baz|eggs=spam}}']")
else:
self.assertPrint(templates, "[u'{{foo|bar|baz|eggs=spam}}']")
template = templates[0]
self.assertPrint(template.name, "foo")
if py3k:
self.assertPrint(template.params, "['bar', 'baz', 'eggs=spam']")
else:
self.assertPrint(template.params, "[u'bar', u'baz', u'eggs=spam']")
self.assertPrint(template.get(1).value, "bar")
self.assertPrint(template.get("eggs").value, "spam")

def test_readme_2(self):
code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
if py3k:
self.assertPrint(code.filter_templates(),
"['{{foo|this {{includes a|template}}}}']")
else:
self.assertPrint(code.filter_templates(),
"[u'{{foo|this {{includes a|template}}}}']")
foo = code.filter_templates()[0]
self.assertPrint(foo.get(1).value, "this {{includes a|template}}")
self.assertPrint(foo.get(1).value.filter_templates()[0],
"{{includes a|template}}")
self.assertPrint(foo.get(1).value.filter_templates()[0].get(1).value,
"template")

def test_readme_3(self):
text = "{{foo|{{bar}}={{baz|{{spam}}}}}}"
temps = mwparserfromhell.parse(text).filter_templates(recursive=True)
if py3k:
res = "['{{foo|{{bar}}={{baz|{{spam}}}}}}', '{{bar}}', '{{baz|{{spam}}}}', '{{spam}}']"
else:
res = "[u'{{foo|{{bar}}={{baz|{{spam}}}}}}', u'{{bar}}', u'{{baz|{{spam}}}}', u'{{spam}}']"
self.assertPrint(temps, res)

def test_readme_4(self):
text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
code = mwparserfromhell.parse(text)
for template in code.filter_templates():
if template.name == "cleanup" and not template.has_param("date"):
template.add("date", "July 2012")
res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}"
self.assertPrint(code, res)
code.replace("{{uncategorized}}", "{{bar-stub}}")
res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}"
self.assertPrint(code, res)
if py3k:
res = "['{{cleanup|date=July 2012}}', '{{bar-stub}}']"
else:
res = "[u'{{cleanup|date=July 2012}}', u'{{bar-stub}}']"
self.assertPrint(code.filter_templates(), res)
text = str(code)
res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}"
self.assertPrint(text, res)
self.assertEqual(text, code)

def test_readme_5(self):
url1 = "http://en.wikipedia.org/w/api.php"
url2 = "http://en.wikipedia.org/w/index.php?title={0}&action=raw"
title = "Test"
data = {"action": "query", "prop": "revisions", "rvlimit": 1,
"rvprop": "content", "format": "json", "titles": title}
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)

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

+ 0
- 119
tests/test_parameter.py Ver ficheiro

@@ -1,119 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import unittest

from mwparserfromhell.parameter import Parameter
from mwparserfromhell.template import Template

class TestParameter(unittest.TestCase):
def setUp(self):
self.name = "foo"
self.value1 = "bar"
self.value2 = "{{spam}}"
self.value3 = "bar{{spam}}"
self.value4 = "embedded {{eggs|spam|baz=buz}} {{goes}} here"
self.templates2 = [Template("spam")]
self.templates3 = [Template("spam")]
self.templates4 = [Template("eggs", [Parameter("1", "spam"),
Parameter("baz", "buz")]),
Template("goes")]

def test_construct(self):
Parameter(self.name, self.value1)
Parameter(self.name, self.value2, self.templates2)
Parameter(name=self.name, value=self.value3)
Parameter(name=self.name, value=self.value4, templates=self.templates4)

def test_name(self):
params = [
Parameter(self.name, self.value1),
Parameter(self.name, self.value2, self.templates2),
Parameter(name=self.name, value=self.value3),
Parameter(name=self.name, value=self.value4,
templates=self.templates4)
]
for param in params:
self.assertEqual(param.name, self.name)

def test_value(self):
tests = [
(Parameter(self.name, self.value1), self.value1),
(Parameter(self.name, self.value2, self.templates2), self.value2),
(Parameter(name=self.name, value=self.value3), self.value3),
(Parameter(name=self.name, value=self.value4,
templates=self.templates4), self.value4)
]
for param, correct in tests:
self.assertEqual(param.value, correct)

def test_templates(self):
tests = [
(Parameter(self.name, self.value3, self.templates3),
self.templates3),
(Parameter(name=self.name, value=self.value4,
templates=self.templates4), self.templates4)
]
for param, correct in tests:
self.assertEqual(param.templates, correct)

def test_magic(self):
params = [Parameter(self.name, self.value1),
Parameter(self.name, self.value2, self.templates2),
Parameter(self.name, self.value3, self.templates3),
Parameter(self.name, self.value4, self.templates4)]
for param in params:
self.assertEqual(repr(param), repr(param.value))
self.assertEqual(str(param), str(param.value))
self.assertIs(param < "eggs", param.value < "eggs")
self.assertIs(param <= "bar{{spam}}", param.value <= "bar{{spam}}")
self.assertIs(param == "bar", param.value == "bar")
self.assertIs(param != "bar", param.value != "bar")
self.assertIs(param > "eggs", param.value > "eggs")
self.assertIs(param >= "bar{{spam}}", param.value >= "bar{{spam}}")
self.assertEquals(bool(param), bool(param.value))
self.assertEquals(len(param), len(param.value))
self.assertEquals(list(param), list(param.value))
self.assertEquals(param[2], param.value[2])
self.assertEquals(list(reversed(param)),
list(reversed(param.value)))
self.assertIs("bar" in param, "bar" in param.value)
self.assertEquals(param + "test", param.value + "test")
self.assertEquals("test" + param, "test" + param.value)
# add param
# add template left
# add template right

self.assertEquals(param * 3, Parameter(param.name, param.value * 3,
param.templates * 3))
self.assertEquals(3 * param, Parameter(param.name, 3 * param.value,
3 * param.templates))

# add param inplace
# add template implace
# add str inplace
# multiply int inplace
self.assertIsInstance(param, Parameter)
self.assertIsInstance(param.value, str)

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

+ 0
- 63
tests/test_parser.py Ver ficheiro

@@ -1,63 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import unittest

from mwparserfromhell.parameter import Parameter
from mwparserfromhell.parser import Parser
from mwparserfromhell.template import Template

TESTS = [
("", []),
("abcdef ghijhk", []),
("abc{this is not a template}def", []),
("neither is {{this one}nor} {this one {despite}} containing braces", []),
("this is an acceptable {{template}}", [Template("template")]),
("{{multiple}}{{templates}}", [Template("multiple"),
Template("templates")]),
("multiple {{-}} templates {{+}}!", [Template("-"), Template("+")]),
("{{{no templates here}}}", []),
("{ {{templates here}}}", [Template("templates here")]),
("{{{{I do not exist}}}}", []),
("{{foo|bar|baz|eggs=spam}}",
[Template("foo", [Parameter("1", "bar"), Parameter("2", "baz"),
Parameter("eggs", "spam")])]),
("{{abc def|ghi|jk=lmno|pqr|st=uv|wx|yz}}",
[Template("abc def", [Parameter("1", "ghi"), Parameter("jk", "lmno"),
Parameter("2", "pqr"), Parameter("st", "uv"),
Parameter("3", "wx"), Parameter("4", "yz")])]),
("{{this has a|{{template}}|inside of it}}",
[Template("this has a", [Parameter("1", "{{template}}",
[Template("template")]),
Parameter("2", "inside of it")])]),
("{{{{I exist}} }}", [Template("I exist", [] )]),
("{{}}")
]

class TestParser(unittest.TestCase):
def test_parse(self):
parser = Parser()
for unparsed, parsed in TESTS:
self.assertEqual(parser.parse(unparsed), parsed)

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

+ 0
- 106
tests/test_template.py Ver ficheiro

@@ -1,106 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from itertools import permutations
import unittest

from mwparserfromhell.parameter import Parameter
from mwparserfromhell.template import Template

class TestTemplate(unittest.TestCase):
def setUp(self):
self.name = "foo"
self.bar = Parameter("1", "bar")
self.baz = Parameter("2", "baz")
self.eggs = Parameter("eggs", "spam")
self.params = [self.bar, self.baz, self.eggs]

def test_construct(self):
Template(self.name)
Template(self.name, self.params)
Template(name=self.name)
Template(name=self.name, params=self.params)

def test_name(self):
templates = [
Template(self.name),
Template(self.name, self.params),
Template(name=self.name),
Template(name=self.name, params=self.params)
]
for template in templates:
self.assertEqual(template.name, self.name)

def test_params(self):
for template in (Template(self.name), Template(name=self.name)):
self.assertEqual(template.params, [])
for template in (Template(self.name, self.params),
Template(name=self.name, params=self.params)):
self.assertEqual(template.params, self.params)

def test_getitem(self):
template = Template(name=self.name, params=self.params)
self.assertIs(template[0], self.bar)
self.assertIs(template[1], self.baz)
self.assertIs(template[2], self.eggs)
self.assertIs(template["1"], self.bar)
self.assertIs(template["2"], self.baz)
self.assertIs(template["eggs"], self.eggs)

def test_render(self):
tests = [
(Template(self.name), "{{foo}}"),
(Template(self.name, self.params), "{{foo|bar|baz|eggs=spam}}")
]
for template, rendered in tests:
self.assertEqual(template.render(), rendered)

def test_repr(self):
correct1= 'Template(name=foo, params={})'
correct2 = 'Template(name=foo, params={"1": "bar", "2": "baz", "eggs": "spam"})'
tests = [(Template(self.name), correct1),
(Template(self.name, self.params), correct2)]
for template, correct in tests:
self.assertEqual(repr(template), correct)
self.assertEqual(str(template), correct)

def test_cmp(self):
tmp1 = Template(self.name)
tmp2 = Template(name=self.name)
tmp3 = Template(self.name, [])
tmp4 = Template(name=self.name, params=[])
tmp5 = Template(self.name, self.params)
tmp6 = Template(name=self.name, params=self.params)

for tmpA, tmpB in permutations((tmp1, tmp2, tmp3, tmp4), 2):
self.assertEqual(tmpA, tmpB)

for tmpA, tmpB in permutations((tmp5, tmp6), 2):
self.assertEqual(tmpA, tmpB)

for tmpA in (tmp5, tmp6):
for tmpB in (tmp1, tmp2, tmp3, tmp4):
self.assertNotEqual(tmpA, tmpB)
self.assertNotEqual(tmpB, tmpA)

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

Carregando…
Cancelar
Guardar