class TestRenderer(TestCase): def setUp(self): self.renderer = HTMLRenderer() self.renderer.render_inner = mock.Mock(return_value='inner') self.renderer.__enter__() self.addCleanup(self.renderer.__exit__, None, None, None) def _test_token(self, token_path, output, children=True, **kwargs): def mock_render(token_path): def inner(token): _, token_name = token_path.split('.') return self.renderer.render_map[token_name](token) return inner self.renderer.render = mock_render(token_path) path = 'mistletoe.{}'.format(token_path) MockToken = mock.Mock(path, name=token_path) if children: MockToken.return_value = mock.Mock(children=mock.MagicMock(), **kwargs) else: MockToken.return_value = mock.Mock(**kwargs) self.assertEqual(self.renderer.render(MockToken()), output)
class TestRenderer(TestCase): def setUp(self): self.renderer = HTMLRenderer() self.renderer.render_inner = mock.Mock(return_value='inner') self.renderer.__enter__() self.addCleanup(self.renderer.__exit__, None, None, None) def _test_token(self, token_name, output, children=True, **kwargs): render_func = self.renderer.render_map[token_name] children = mock.MagicMock() if children else None mock_token = mock.Mock(children=children, **kwargs) self.assertEqual(render_func(mock_token), output)
class TestHTMLToken(TestCase): def setUp(self): self.renderer = HTMLRenderer() self.renderer.__enter__() self.addCleanup(self.renderer.__exit__, None, None, None) def _test_html_token(self, token, token_cls, content): self.assertIsInstance(token, token_cls) self.assertEqual(token.content, content) @mock.patch('mistletoe.span_token.RawText') def test_span(self, MockRawText): raw = 'some <span>more</span> text' tokens = tokenize_inner(raw) next(tokens) MockRawText.assert_called_with('some ') content = '<span>more</span>' self._test_html_token(next(tokens), html_token.HTMLSpan, content) next(tokens) MockRawText.assert_called_with(' text') def test_block(self): lines = ['<p>a paragraph\n', 'within an html block\n', '</p>\n'] token = next(tokenize(lines)) content = '<p>a paragraph\nwithin an html block\n</p>\n' self._test_html_token(token, html_token.HTMLBlock, content) def test_span_attrs(self): raw = '<span class="foo">more</span>' token = next(tokenize_inner(raw)) content = '<span class="foo">more</span>' self._test_html_token(token, html_token.HTMLSpan, content) def test_block_attrs(self): lines = [ '<p class="bar">a paragraph\n', 'within an html block\n', '</p>\n' ] token = next(tokenize(lines)) content = '<p class="bar">a paragraph\nwithin an html block\n</p>\n' self._test_html_token(token, html_token.HTMLBlock, content) def test_empty_span(self): raw = '<span></span>' token = next(tokenize_inner(raw)) content = '<span></span>' self._test_html_token(token, html_token.HTMLSpan, content) def test_self_closing_span(self): raw = '<span />' token = next(tokenize_inner(raw)) content = '<span />' self._test_html_token(token, html_token.HTMLSpan, content)
class TestHTMLRendererFootnotes(TestCase): def setUp(self): self.renderer = HTMLRenderer() self.renderer.__enter__() self.addCleanup(self.renderer.__exit__, None, None, None) def test_footnote_image(self): from mistletoe import Document token = Document(['![alt] [foo]\n', '\n', '[foo]: bar "title"\n']) output = '<p><img src="bar" title="title" alt="alt">\n</p>\n' self.assertEqual(self.renderer.render(token), output) def test_footnote_link(self): from mistletoe import Document token = Document(['[name] [foo]\n', '\n', '[foo]: target\n']) output = '<p><a href="target">name</a>\n</p>\n' self.assertEqual(self.renderer.render(token), output)
class TestHTMLToken(TestCase): def setUp(self): self.renderer = HTMLRenderer() self.renderer.__enter__() self.addCleanup(self.renderer.__exit__, None, None, None) def _test_html_token(self, token, token_cls, content): self.assertIsInstance(token, token_cls) self.assertEqual(token.content, content) def test_span(self): raw = 'some <span>more</span> text' tokens = tokenize_inner(raw) next(tokens) content = '<span>more</span>' self._test_html_token(next(tokens), html_token.HTMLSpan, content) next(tokens) def test_block(self): lines = ['<p>a paragraph\n', 'within an html block\n', '</p>\n'] token = next(tokenize(lines)) content = '<p>a paragraph\nwithin an html block\n</p>\n' self._test_html_token(token, html_token.HTMLBlock, content) def test_span_attrs(self): raw = '<span class="foo">more</span>' token = next(tokenize_inner(raw)) content = '<span class="foo">more</span>' self._test_html_token(token, html_token.HTMLSpan, content) def test_block_attrs(self): lines = [ '<p class="bar">a paragraph\n', 'within an html block\n', '</p>\n' ] token = next(tokenize(lines)) content = '<p class="bar">a paragraph\nwithin an html block\n</p>\n' self._test_html_token(token, html_token.HTMLBlock, content) def test_comment(self): from mistletoe.block_token import Heading lines = ['<!-- hello -->\n', '\n', '# heading 1\n'] token1, token2 = tokenize(lines) content = '<!-- hello -->\n' self._test_html_token(token1, html_token.HTMLBlock, content) self.assertIsInstance(token2, Heading) def test_empty_span(self): raw = '<span></span>' token = next(tokenize_inner(raw)) content = '<span></span>' self._test_html_token(token, html_token.HTMLSpan, content) def test_self_closing_span(self): raw = '<span />' token = next(tokenize_inner(raw)) content = '<span />' self._test_html_token(token, html_token.HTMLSpan, content) def test_autolink(self): from mistletoe.span_token import AutoLink self.assertIsInstance(next(tokenize_inner('<autolink>')), AutoLink)