예제 #1
0
 def test_name(self):
     """test getter/setter for the name attribute"""
     name = wraptext("id")
     node = Attribute(name, wraptext("bar"))
     self.assertIs(name, node.name)
     node.name = "{{id}}"
     self.assertWikicodeEqual(wrap([Template(wraptext("id"))]), node.name)
예제 #2
0
 def test_name(self):
     """test getter/setter for the name attribute"""
     name = wraptext("id")
     node = Attribute(name, wraptext("bar"))
     self.assertIs(name, node.name)
     node.name = "{{id}}"
     self.assertWikicodeEqual(wrap([Template(wraptext("id"))]), node.name)
예제 #3
0
    def test_children(self):
        """test Tag.__children__()"""
        # <ref>foobar</ref>
        node1 = Tag(wraptext("ref"), wraptext("foobar"))
        # '''bold text'''
        node2 = Tag(wraptext("b"), wraptext("bold text"), wiki_markup="'''")
        # <img id="foo" class="bar" />
        node3 = Tag(wraptext("img"),
                    attrs=[
                        Attribute(wraptext("id"), wraptext("foo")),
                        Attribute(wraptext("class"), wraptext("bar"))
                    ],
                    self_closing=True,
                    padding=" ")

        gen1 = node1.__children__()
        gen2 = node2.__children__()
        gen3 = node3.__children__()
        self.assertEqual(node1.tag, next(gen1))
        self.assertEqual(node3.tag, next(gen3))
        self.assertEqual(node3.attributes[0].name, next(gen3))
        self.assertEqual(node3.attributes[0].value, next(gen3))
        self.assertEqual(node3.attributes[1].name, next(gen3))
        self.assertEqual(node3.attributes[1].value, next(gen3))
        self.assertEqual(node1.contents, next(gen1))
        self.assertEqual(node2.contents, next(gen2))
        self.assertEqual(node1.closing_tag, next(gen1))
        self.assertRaises(StopIteration, next, gen1)
        self.assertRaises(StopIteration, next, gen2)
        self.assertRaises(StopIteration, next, gen3)
예제 #4
0
 def test_value(self):
     """test getter/setter for the value attribute"""
     value = wraptext("foo")
     node = Attribute(wraptext("id"), value)
     self.assertIs(value, node.value)
     node.value = "{{bar}}"
     self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
     node.value = None
     self.assertIs(None, node.value)
예제 #5
0
 def test_value(self):
     """test getter/setter for the value attribute"""
     value = wraptext("foo")
     node = Attribute(wraptext("id"), value)
     self.assertIs(value, node.value)
     node.value = "{{bar}}"
     self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
     node.value = None
     self.assertIs(None, node.value)
예제 #6
0
 def test_quoted(self):
     """test getter/setter for the quoted attribute"""
     node1 = Attribute(wraptext("id"), wraptext("foo"), False)
     node2 = Attribute(wraptext("id"), wraptext("bar"))
     self.assertFalse(node1.quoted)
     self.assertTrue(node2.quoted)
     node1.quoted = True
     node2.quoted = ""
     self.assertTrue(node1.quoted)
     self.assertFalse(node2.quoted)
예제 #7
0
 def test_unicode(self):
     """test Attribute.__unicode__()"""
     node = Attribute(wraptext("foo"))
     self.assertEqual(" foo", str(node))
     node2 = Attribute(wraptext("foo"), wraptext("bar"))
     self.assertEqual(' foo="bar"', str(node2))
     node3 = Attribute(wraptext("a"), wraptext("b"), True, "", " ", "   ")
     self.assertEqual('a =   "b"', str(node3))
     node3 = Attribute(wraptext("a"), wraptext("b"), False, "", " ", "   ")
     self.assertEqual("a =   b", str(node3))
     node4 = Attribute(wraptext("a"), wrap([]), False, " ", "", " ")
     self.assertEqual(" a= ", str(node4))
예제 #8
0
 def test_quotes(self):
     """test getter/setter for the quotes attribute"""
     node1 = Attribute(wraptext("id"), wraptext("foo"), None)
     node2 = Attribute(wraptext("id"), wraptext("bar"))
     node3 = Attribute(wraptext("id"), wraptext("foo bar baz"))
     self.assertIs(None, node1.quotes)
     self.assertEqual('"', node2.quotes)
     node1.quotes = "'"
     node2.quotes = None
     self.assertEqual("'", node1.quotes)
     self.assertIs(None, node2.quotes)
     self.assertRaises(ValueError, setattr, node1, "quotes", "foobar")
     self.assertRaises(ValueError, setattr, node3, "quotes", None)
     self.assertRaises(ValueError, Attribute, wraptext("id"),
                       wraptext("foo bar baz"), None)
예제 #9
0
 def test_padding(self):
     """test getter/setter for the padding attributes"""
     for pad in ["pad_first", "pad_before_eq", "pad_after_eq"]:
         node = Attribute(wraptext("id"), wraptext("foo"), **{pad: "\n"})
         self.assertEqual("\n", getattr(node, pad))
         setattr(node, pad, " ")
         self.assertEqual(" ", getattr(node, pad))
         setattr(node, pad, None)
         self.assertEqual("", getattr(node, pad))
         self.assertRaises(ValueError, setattr, node, pad, True)
예제 #10
0
 def test_value(self):
     """test getter/setter for the value attribute"""
     value = wraptext("foo")
     node = Attribute(wraptext("id"), value)
     self.assertIs(value, node.value)
     node.value = "{{bar}}"
     self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
     node.value = None
     self.assertIs(None, node.value)
     node2 = Attribute(wraptext("id"), wraptext("foo"), None)
     node2.value = "foo bar baz"
     self.assertWikicodeEqual(wraptext("foo bar baz"), node2.value)
     self.assertEqual('"', node2.quotes)
     node2.value = 'foo "bar" baz'
     self.assertWikicodeEqual(wraptext('foo "bar" baz'), node2.value)
     self.assertEqual("'", node2.quotes)
     node2.value = "foo 'bar' baz"
     self.assertWikicodeEqual(wraptext("foo 'bar' baz"), node2.value)
     self.assertEqual('"', node2.quotes)
     node2.value = "fo\"o 'bar' b\"az"
     self.assertWikicodeEqual(wraptext("fo\"o 'bar' b\"az"), node2.value)
     self.assertEqual('"', node2.quotes)
예제 #11
0
 def test_quoted(self):
     """test getter/setter for the quoted attribute"""
     node1 = Attribute(wraptext("id"), wraptext("foo"), False)
     node2 = Attribute(wraptext("id"), wraptext("bar"))
     self.assertFalse(node1.quoted)
     self.assertTrue(node2.quoted)
     node1.quoted = True
     node2.quoted = ""
     self.assertTrue(node1.quoted)
     self.assertFalse(node2.quoted)
예제 #12
0
 def test_quotes(self):
     """test getter/setter for the quotes attribute"""
     node1 = Attribute(wraptext("id"), wraptext("foo"), None)
     node2 = Attribute(wraptext("id"), wraptext("bar"))
     node3 = Attribute(wraptext("id"), wraptext("foo bar baz"))
     self.assertIs(None, node1.quotes)
     self.assertEqual('"', node2.quotes)
     node1.quotes = "'"
     node2.quotes = None
     self.assertEqual("'", node1.quotes)
     self.assertIs(None, node2.quotes)
     self.assertRaises(ValueError, setattr, node1, "quotes", "foobar")
     self.assertRaises(ValueError, setattr, node3, "quotes", None)
     self.assertRaises(ValueError, Attribute, wraptext("id"),
                       wraptext("foo bar baz"), None)
예제 #13
0
 def test_unicode(self):
     """test Attribute.__unicode__()"""
     node = Attribute(wraptext("foo"))
     self.assertEqual(" foo", str(node))
     node2 = Attribute(wraptext("foo"), wraptext("bar"))
     self.assertEqual(' foo="bar"', str(node2))
     node3 = Attribute(wraptext("a"), wraptext("b"), '"', "", " ", "   ")
     self.assertEqual('a =   "b"', str(node3))
     node4 = Attribute(wraptext("a"), wraptext("b"), "'", "", " ", "   ")
     self.assertEqual("a =   'b'", str(node4))
     node5 = Attribute(wraptext("a"), wraptext("b"), None, "", " ", "   ")
     self.assertEqual("a =   b", str(node5))
     node6 = Attribute(wraptext("a"), wrap([]), None, " ", "", " ")
     self.assertEqual(" a= ", str(node6))
예제 #14
0
 def test_value(self):
     """test getter/setter for the value attribute"""
     value = wraptext("foo")
     node = Attribute(wraptext("id"), value)
     self.assertIs(value, node.value)
     node.value = "{{bar}}"
     self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
     node.value = None
     self.assertIs(None, node.value)
     node2 = Attribute(wraptext("id"), wraptext("foo"), None)
     node2.value = "foo bar baz"
     self.assertWikicodeEqual(wraptext("foo bar baz"), node2.value)
     self.assertEqual('"', node2.quotes)
     node2.value = 'foo "bar" baz'
     self.assertWikicodeEqual(wraptext('foo "bar" baz'), node2.value)
     self.assertEqual("'", node2.quotes)
     node2.value = "foo 'bar' baz"
     self.assertWikicodeEqual(wraptext("foo 'bar' baz"), node2.value)
     self.assertEqual('"', node2.quotes)
     node2.value = "fo\"o 'bar' b\"az"
     self.assertWikicodeEqual(wraptext("fo\"o 'bar' b\"az"), node2.value)
     self.assertEqual('"', node2.quotes)
예제 #15
0
#
# 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.nodes import Tag, Template, Text
from mwparserfromhell.nodes.extras import Attribute
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext

agen = lambda name, value: Attribute(wraptext(name), wraptext(value))
agennv = lambda name: Attribute(wraptext(name))
agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), None)
agenp = lambda name, v, a, b, c: Attribute(wraptext(name), v, '"', a, b, c)
agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, '"', a, b, c)


class TestTag(TreeEqualityTestCase):
    """Test cases for the Tag node."""
    def test_str(self):
        """test Tag.__str__()"""
        node1 = Tag(wraptext("ref"))
        node2 = Tag(wraptext("span"), wraptext("foo"),
                    [agen("style", "color: red;")])
        node3 = Tag(
            wraptext("ref"),
예제 #16
0
    def test_tag(self):
        """tests for building Tag nodes"""
        tests = [
            # <ref></ref>
            ([
                tokens.TagOpenOpen(),
                tokens.Text(text="ref"),
                tokens.TagCloseOpen(padding=""),
                tokens.TagOpenClose(),
                tokens.Text(text="ref"),
                tokens.TagCloseClose()
            ],
             wrap(
                 [Tag(wraptext("ref"), wrap([]),
                      closing_tag=wraptext("ref"))])),

            # <ref name></ref>
            ([
                tokens.TagOpenOpen(),
                tokens.Text(text="ref"),
                tokens.TagAttrStart(pad_first=" ",
                                    pad_before_eq="",
                                    pad_after_eq=""),
                tokens.Text(text="name"),
                tokens.TagCloseOpen(padding=""),
                tokens.TagOpenClose(),
                tokens.Text(text="ref"),
                tokens.TagCloseClose()
            ],
             wrap([
                 Tag(wraptext("ref"),
                     wrap([]),
                     attrs=[Attribute(wraptext("name"))])
             ])),

            # <ref name="abc" />
            ([
                tokens.TagOpenOpen(),
                tokens.Text(text="ref"),
                tokens.TagAttrStart(pad_first=" ",
                                    pad_before_eq="",
                                    pad_after_eq=""),
                tokens.Text(text="name"),
                tokens.TagAttrEquals(),
                tokens.TagAttrQuote(char='"'),
                tokens.Text(text="abc"),
                tokens.TagCloseSelfclose(padding=" ")
            ],
             wrap([
                 Tag(wraptext("ref"),
                     attrs=[Attribute(wraptext("name"), wraptext("abc"))],
                     self_closing=True,
                     padding=" ")
             ])),

            # <br/>
            ([
                tokens.TagOpenOpen(),
                tokens.Text(text="br"),
                tokens.TagCloseSelfclose(padding="")
            ], wrap([Tag(wraptext("br"), self_closing=True)])),

            # <li>
            ([
                tokens.TagOpenOpen(),
                tokens.Text(text="li"),
                tokens.TagCloseSelfclose(padding="", implicit=True)
            ], wrap([Tag(wraptext("li"), self_closing=True, implicit=True)])),

            # </br>
            ([
                tokens.TagOpenOpen(invalid=True),
                tokens.Text(text="br"),
                tokens.TagCloseSelfclose(padding="", implicit=True)
            ],
             wrap([
                 Tag(wraptext("br"),
                     self_closing=True,
                     invalid=True,
                     implicit=True)
             ])),

            # </br/>
            ([
                tokens.TagOpenOpen(invalid=True),
                tokens.Text(text="br"),
                tokens.TagCloseSelfclose(padding="")
            ], wrap([Tag(wraptext("br"), self_closing=True, invalid=True)])),

            # <ref name={{abc}}   foo="bar {{baz}}" abc={{de}}f ghi=j{{k}}{{l}}
            #      mno =  '{{p}} [[q]] {{r}}'>[[Source]]</ref>
            ([
                tokens.TagOpenOpen(),
                tokens.Text(text="ref"),
                tokens.TagAttrStart(pad_first=" ",
                                    pad_before_eq="",
                                    pad_after_eq=""),
                tokens.Text(text="name"),
                tokens.TagAttrEquals(),
                tokens.TemplateOpen(),
                tokens.Text(text="abc"),
                tokens.TemplateClose(),
                tokens.TagAttrStart(pad_first="   ",
                                    pad_before_eq="",
                                    pad_after_eq=""),
                tokens.Text(text="foo"),
                tokens.TagAttrEquals(),
                tokens.TagAttrQuote(char='"'),
                tokens.Text(text="bar "),
                tokens.TemplateOpen(),
                tokens.Text(text="baz"),
                tokens.TemplateClose(),
                tokens.TagAttrStart(pad_first=" ",
                                    pad_before_eq="",
                                    pad_after_eq=""),
                tokens.Text(text="abc"),
                tokens.TagAttrEquals(),
                tokens.TemplateOpen(),
                tokens.Text(text="de"),
                tokens.TemplateClose(),
                tokens.Text(text="f"),
                tokens.TagAttrStart(pad_first=" ",
                                    pad_before_eq="",
                                    pad_after_eq=""),
                tokens.Text(text="ghi"),
                tokens.TagAttrEquals(),
                tokens.Text(text="j"),
                tokens.TemplateOpen(),
                tokens.Text(text="k"),
                tokens.TemplateClose(),
                tokens.TemplateOpen(),
                tokens.Text(text="l"),
                tokens.TemplateClose(),
                tokens.TagAttrStart(pad_first=" \n ",
                                    pad_before_eq=" ",
                                    pad_after_eq="  "),
                tokens.Text(text="mno"),
                tokens.TagAttrEquals(),
                tokens.TagAttrQuote(char="'"),
                tokens.TemplateOpen(),
                tokens.Text(text="p"),
                tokens.TemplateClose(),
                tokens.Text(text=" "),
                tokens.WikilinkOpen(),
                tokens.Text(text="q"),
                tokens.WikilinkClose(),
                tokens.Text(text=" "),
                tokens.TemplateOpen(),
                tokens.Text(text="r"),
                tokens.TemplateClose(),
                tokens.TagCloseOpen(padding=""),
                tokens.WikilinkOpen(),
                tokens.Text(text="Source"),
                tokens.WikilinkClose(),
                tokens.TagOpenClose(),
                tokens.Text(text="ref"),
                tokens.TagCloseClose()
            ],
             wrap([
                 Tag(wraptext("ref"), wrap([Wikilink(wraptext("Source"))]), [
                     Attribute(wraptext("name"),
                               wrap([Template(wraptext("abc"))]), None),
                     Attribute(wraptext("foo"),
                               wrap([Text("bar "),
                                     Template(wraptext("baz"))]),
                               pad_first="   "),
                     Attribute(wraptext("abc"),
                               wrap([Template(wraptext("de")),
                                     Text("f")]), None),
                     Attribute(
                         wraptext("ghi"),
                         wrap([
                             Text("j"),
                             Template(wraptext("k")),
                             Template(wraptext("l"))
                         ]), None),
                     Attribute(
                         wraptext("mno"),
                         wrap([
                             Template(wraptext("p")),
                             Text(" "),
                             Wikilink(wraptext("q")),
                             Text(" "),
                             Template(wraptext("r"))
                         ]), "'", " \n ", " ", "  ")
                 ])
             ])),

            # "''italic text''"
            ([
                tokens.TagOpenOpen(wiki_markup="''"),
                tokens.Text(text="i"),
                tokens.TagCloseOpen(),
                tokens.Text(text="italic text"),
                tokens.TagOpenClose(),
                tokens.Text(text="i"),
                tokens.TagCloseClose()
            ],
             wrap([
                 Tag(wraptext("i"), wraptext("italic text"), wiki_markup="''")
             ])),

            # * bullet
            ([
                tokens.TagOpenOpen(wiki_markup="*"),
                tokens.Text(text="li"),
                tokens.TagCloseSelfclose(),
                tokens.Text(text=" bullet")
            ],
             wrap([
                 Tag(wraptext("li"), wiki_markup="*", self_closing=True),
                 Text(" bullet")
             ])),
        ]
        for test, valid in tests:
            self.assertWikicodeEqual(valid, self.builder.build(test))
예제 #17
0
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import unicode_literals

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from mwparserfromhell.compat import str
from mwparserfromhell.nodes import Tag, Template, Text
from mwparserfromhell.nodes.extras import Attribute
from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext

agen = lambda name, value: Attribute(wraptext(name), wraptext(value))
agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False)
agenp = lambda name, v, a, b, c: Attribute(wraptext(name), v, True, a, b, c)
agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, True, a, b, c)


class TestTag(TreeEqualityTestCase):
    """Test cases for the Tag node."""
    def test_unicode(self):
        """test Tag.__unicode__()"""
        node1 = Tag(wraptext("ref"))
        node2 = Tag(wraptext("span"), wraptext("foo"),
                    [agen("style", "color: red;")])
        node3 = Tag(
            wraptext("ref"),
            attrs=[agennq("name", "foo"),