From b018a271e714b42ed7e1049b34c25fc50c160af6 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 5 Sep 2021 22:21:48 +0200 Subject: add more tests for wikimini.document --- tests/test_document.py | 146 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/tests/test_document.py b/tests/test_document.py index 35f706f..0ddce94 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -1,5 +1,11 @@ +import pytest + from wikimini import document +########################### +# Block level element tests +########################### + class TestBlockLink: @@ -47,4 +53,142 @@ class TestBlockQuote: document.InlineLink("http://localhost", document.Plain("bar")), ] assert (document.BlockQuote(document.Paragraph(nodes)).to_nodes() - == 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 -- cgit v1.2.3