import pytest from wikimini import document ########################### # Block level element tests ########################### class TestBlockLink: def test_append(self): link = document.BlockLink("https://localhost", "foo ") link.append(document.Plain("bar")) assert link.title == "foo bar" link.append(document.Style(document.Plain("qux"), False, True, False)) assert link.title == "foo barqux" def test_plain(self): link = document.BlockLink("https://localhost", "foo ") assert link.plain() == "foo " def test_to_nodes(self): link = document.BlockLink("https://localhost", "foo ") assert link.to_nodes() == [ document.Plain("foo ") ] class TestBlockQuote: def test_append(self): quote = document.BlockQuote(document.Paragraph([])) quote.append(document.Plain("Foo")) assert quote.plain() == "Foo" assert len(quote.content.nodes) == 1 quote.append(document.Style(document.Plain("bar"), True, True, False)) assert quote.plain() == "Foobar" assert len(quote.content.nodes) == 2 def test_plain(self): quote = document.BlockQuote(document.Paragraph([ document.Plain("Foo"), document.InlineLink("http://localhost", document.Plain("bar")), ])) assert quote.plain() == "Foobar" def test_to_nodes(self): nodes = [ document.Plain("Foo"), document.InlineLink("http://localhost", document.Plain("bar")), ] assert (document.BlockQuote(document.Paragraph(nodes)).to_nodes() == nodes) class TestHeading: def test_append(self): heading = document.Heading(1, "foo") heading.append(document.Plain("bar")) assert heading.text == "foobar" assert heading.level == 1 heading.append( document.Style(document.Plain("bar"), True, True, False) ) assert heading.text == "foobarbar" assert heading.level == 1 def test_plain(self): heading = document.Heading(1, "raboof") assert heading.plain() == "raboof" def test_to_nodes(self): heading = document.Heading(1, "raboof") assert heading.to_nodes() == [document.Plain("raboof")] class TestItemList: def test_append(self): item_list = document.ItemList([], False) item_list.append(document.Plain("foo")) item_list.append(document.Plain("bar")) assert len(item_list.items) == 1 def test_cleanup(self): item_list = document.ItemList([ document.Paragraph([document.Plain(" foo")]), document.Paragraph([document.Plain(" ")]), document.Paragraph([document.Plain(" bar")]), ], False) item_list.cleanup() assert len(item_list.items) == 2 assert item_list.items[0].plain() == "foo" assert item_list.items[1].plain() == "bar" def test_new_item(self): item_list = document.ItemList([], False) item_list.append(document.Plain("foo")) item_list.new_item() item_list.append(document.Plain("bar")) assert len(item_list.items) == 2 def test_plain(self): item_list = document.ItemList([ document.Paragraph([document.Plain("foo")]), document.Paragraph([document.Plain("bar")]), ], False) assert item_list.plain() == "foo\nbar" def test_to_nodes(self): item_list = document.ItemList([ document.Paragraph([document.Plain("foo")]), document.Paragraph([document.Plain("bar")]), ], False) nodes = item_list.to_nodes() assert nodes == [document.Plain("foo"), document.Plain("bar")] class TestParagraph: def test_append(self): par = document.Paragraph([]) par.append(document.Plain("foo")) par.append(document.InlineLink( "https://localhost", document.Plain("bar") )) assert len(par.nodes) == 2 def test_cleanup(self): par = document.Paragraph([ document.Plain(" "), document.Plain(" foo"), document.InlineLink("https://localhost", document.Plain("bar ")), ]) par.cleanup() assert len(par.nodes) == 2 assert par.plain() == "foobar" @pytest.mark.parametrize("nodes, result", [ ([], False), ([document.Plain("foo")], False), ([document.InlineLink("foo", document.Plain("bar"))], True), ([ document.InlineLink("foo", document.Plain("bar")), document.Plain("raboof") ], False), ]) def test_is_link_paragraph(self, nodes, result): par = document.Paragraph(nodes) assert par.is_link_paragraph() == result @pytest.mark.parametrize("nodes, result", [ ([], ""), ([document.Plain("foo")], "foo"), ([document.Plain("foo"), document.Plain("bar")], "foobar"), ([document.InlineLink("foo", document.Plain("bar"))], "bar"), ]) def test_plain(self, nodes, result): par = document.Paragraph(nodes) assert par.plain() == result class TestVerbatim: def test_append(self): verb = document.Verbatim("") verb.append(document.Plain("foo")) verb.append(document.InlineLink("foo", document.Plain("bar"))) assert verb.text == "foobar" def test_cleanup(self): verb = document.Verbatim(" \nfoo\n bar\n ") verb.cleanup() assert verb.text == " \nfoo\n bar\n " ############################# # Tests for extract_plaintext ############################# class TestExtractPlaintext: @pytest.mark.parametrize("node, plain", [ (document.Plain("foo"), "foo"), (document.InlineLink("foo", document.Plain("bar")), "bar"), (document.Style(document.Plain("foo"), True, False, False), "foo"), ]) def test_with_node(self, node, plain): assert document.extract_plaintext(node) == plain def test_with_nodelist(self): nodes = [ document.Plain("foo"), document.Plain("bar "), document.Style(document.Plain("qux"), False, True, True), ] expected = "foobar qux" assert document.extract_plaintext(nodes) == expected # Most of those just defer to Block.plain() which is tested above @pytest.mark.parametrize("block, plain", [ (document.Paragraph([document.Plain("foo"), document.Plain("bar")]), "foobar"), (document.Verbatim("foo bar"), "foo bar"), ]) def test_with_block(self, block, plain): assert document.extract_plaintext(block) == plain def test_with_blocklist(self): blocks = [ document.Paragraph([document.Plain("foo")]), document.Verbatim("bar"), document.ItemList([ document.Paragraph([document.Plain("qux")]), document.Paragraph([document.Plain("baz")]), ], False), ] expected = "foo\n\nbar\n\nqux\nbaz" assert document.extract_plaintext(blocks) == expected def test_with_document(self): blocks = [ document.Paragraph([document.Plain("foo")]), document.Verbatim("bar"), document.ItemList([ document.Paragraph([document.Plain("qux")]), document.Paragraph([document.Plain("baz")]), ], False), ] doc = document.Document(blocks) expected = "foo\n\nbar\n\nqux\nbaz" assert document.extract_plaintext(doc) == expected