def test_footnotes2(self): for name, (flag, kind) in footnotes.items(): for label in ("+", "-", "4"): word1 = test_utils.word(allow_empty=False) word2 = test_utils.word(allow_empty=False) paragraph_flag = random.choice(["p", "m", "pi", "ipr"]) lines = ( r"\{p}".format(p=paragraph_flag), r"\{f} {l} {w1} \fqa {w2} \{f}*".format(f=flag, l=label, w1=word1, w2=word2), ) document = self.parse(*lines) self.assertIsNone(document.heading) elements = document.elements self.assertEqual(len(elements), 1) paragraph = elements[0] self.assertIsInstance(paragraph, Paragraph) children = paragraph.children self.assertEqual(len(children), 1) footnote = children[0] self.assertIsInstance(footnote, Footnote) self.assertEqual(footnote.kind, kind) self.assertEqual(len(footnote.children), 2)
def test_heading(self): heading = test_utils.word() document = self.parse(r"\id {}".format(test_utils.word()), r"", r"\h {}".format(heading), r"\p {}".format(test_utils.word())) self.assertEqual(document.heading, heading) elements = document.elements self.assertEqual(len(elements), 1)
def test_heading(self): word = test_utils.word() heading = test_utils.word() elements = [Paragraph([Text(word)])] document = Document(elements, heading=heading) rendered = self.render(document) self.assertIn(word, rendered) self.assertIn(heading, rendered)
def test_unmatched(self): for name, (flag, kind) in lower_open_closes.items(): if kind is None: continue lines = (r"\{}".format(random.choice(["p", "m", "pi", "ipr"])), r"\{f} {w}".format(f=flag, w=test_utils.word()), r" ") self.assert_raises_at(lines, line_no=4, col=1) word = test_utils.word() lines = (r"\{}".format(random.choice(["p", "m", "pi", "ipr"])), r"{w} \{f}*".format(w=word, f=flag), r" ") self.assert_raises_at(lines, line_no=2, col=len(word) + 2)
def test_table_of_contents(self): word1 = test_utils.word() word2 = test_utils.word() word3 = test_utils.word() document = self.parse(r"\toc1 {}".format(word1), r"\toc2 {}".format(word2), r"\toc3 {}".format(word3)) elements = document.elements self.assertEqual(len(elements), 0) toc = document.table_of_contents self.assertEqual(toc.long_description, word1) self.assertEqual(toc.short_description, word2) self.assertEqual(toc.abbreviation, word3)
def test_chapter_label_after(self): word = test_utils.word() document = self.parse(r"\c 4", r"\cl {}".format(word)) elements = document.elements chapter_no = elements[0] self.assertIsInstance(chapter_no, ChapterNumber) self.assertEqual(chapter_no.children[0].content, word)
def test_no_footnote_label(self): for name, (flag, kind) in footnotes.items(): lines = ( r"\mt1 {}".format(test_utils.word()), r"\{f} \{f}*".format(f=flag), ) self.assert_raises_at(lines, line_no=2, col=len(flag) + 3)
def test_paragraph(self): bools = (False, True) for embedded, poetic, introductory, continuation \ in itertools.product(bools, bools, bools, bools): word = test_utils.word() text = Text(word) paragraph = Paragraph([text], embedded=embedded, poetic=poetic, introductory=introductory, continuation=continuation) rendered = self.render_elements(paragraph) self.assertIn(word, rendered) if embedded: self.assertIn("embedded", rendered) # should appear as a class else: self.assertNotIn("embedded", rendered) if poetic: self.assertIn("poetic", rendered) else: self.assertNotIn("poetic", rendered) if introductory: self.assertIn("introductory", rendered) else: self.assertNotIn("introductory", rendered) if continuation: self.assertIn("continuation", rendered) else: self.assertNotIn("continuation", rendered)
def test_unrecognized_flag(self): for _ in range(10): word = test_utils.word() lines = ( r"\{}".format(random.choice(["p", "m", "pi", "ipr"])), r"\{flag}".format(flag=word), ) self.assert_raises_at(lines, line_no=2, col=1)
def test_footnotes(self): for kind in list(Footnote.Kind): word = test_utils.word() footnote = Footnote(kind, [Text(word)], AutomaticFootnoteLabel()) paragraph = Paragraph([footnote]) rendered = self.render_elements(paragraph) self.assertIn(kind.name, rendered) self.assertIn(word, rendered)
def test_published_verse(self): word = test_utils.word() document = self.parse(r"\p \v 4 \vp 5 \vp* {w}".format(w=word)) elements = document.elements paragraph = elements[0] self.assertIsInstance(paragraph, Paragraph) verse = paragraph.children[0] self.assertIsInstance(verse, FormattedText) self.assertIn("5", verse.children[0].content)
def test_paragraphs(self): for name, (flag, builder) in paragraphs.items(): if builder is None: continue num_words = random.randint(0, 10) words = " ".join(test_utils.word() for _ in range(num_words)) s = r"\{flag} {words}".format(flag=flag, words=words) document = self.parse(s) elements = document.elements self.assertEqual(len(elements), 1) paragraph = elements[0] self.assertIsInstance(paragraph, Paragraph) self.assertIn(len(paragraph.children), [0, 1])
def test_formatted_text(self): for kind in list(FormattedText.Kind): text = " ".join(test_utils.word(allow_empty=False) for _ in range(10)) formatted_text = FormattedText(kind, [Text(text)]) rendered = self.render_elements(formatted_text) self.assertIn(text, rendered) if kind in non_span_formatting: open_tag, close_tag = non_span_formatting[kind] self.assertIn(open_tag, rendered) self.assertIn(close_tag, rendered) else: self.assertIn(kind.name, rendered) # kind.name should appear as a class
def test_lower_open_closes(self): for name, (flag, builder) in lower_open_closes.items(): if builder is None: continue word = test_utils.word(allow_empty=False) s = r"\p \{flag} {word}\{flag}*".format(flag=flag, word=word) document = self.parse(s) elements = document.elements self.assertEqual(len(elements), 1) paragraph = elements[0] self.assertIsInstance(paragraph, Paragraph) self.assertEqual(len(paragraph.children), 1) formatted = paragraph.children[0] self.assertIsInstance(formatted, FormattedText) self.assertEqual(len(formatted.children), 1) if len(formatted.children) > 0 and isinstance( formatted.children[0], Text): self.assertEqual(formatted.children[0].content, word)
def test_footnotes1(self): for name, (flag, kind) in footnotes.items(): for label in ("+", "-", "4"): word = test_utils.word() paragraph_flag = random.choice(["p", "m", "pi", "ipr"]) lines = (r"\{}".format(paragraph_flag), r"\{f} {l} {w} \{f}*".format(f=flag, l=label, w=word)) document = self.parse(*lines) self.assertIsNone(document.heading) elements = document.elements self.assertEqual(len(elements), 1) paragraph = elements[0] self.assertIsInstance(paragraph, Paragraph) children = paragraph.children self.assertEqual(len(children), 1) footnote = children[0] self.assertIsInstance(footnote, Footnote) self.assertEqual(footnote.kind, kind)
def test_no_break(self): for _ in range(10): paragraph_flag = random.choice(["p", "m", "pmo", "pi", "q"]) text = " ".join(test_utils.word() for _ in range(10)) + "a" lines = (r"\{p}".format(p=paragraph_flag), r"\nb", r"{t}".format(t=text)) document = self.parse(*lines) elements = document.elements self.assertEqual(len(elements), 2) p0 = elements[0] self.assertIsInstance(p0, Paragraph) p1 = elements[1] self.assertIsInstance(p1, Paragraph) self.assertEqual(p0.embedded, p1.embedded) self.assertEqual(p0.poetic, p1.poetic) self.assertEqual(p0.introductory, p1.introductory) self.assertFalse(p0.continuation) self.assertTrue(p1.continuation) self.assertEqual(len(p1.children), 1) child = p1.children[0] self.assertIsInstance(child, Text) self.assertEqual(child.content.strip(), text.strip())