def test_dump(self): # Test T dumping a Tempy object to a file result = """# -*- coding: utf-8 -*- from tempy import T from tempy.tags import * Div(klass="cssClass", bool_attr="True")(A(href=\"\"\"www.foo.bar\"\"\")(\"\"\"non-tempy content\"\"\"), T.CustomTag(numb_attr=9), Br(), Doctype("html"), Comment("test comment"), T.Void.TestVoid())""" filename = 'test.py' tempy_tree = [ Div(klass='cssClass', bool_attr=bool)(A(href='www.foo.bar')('non-tempy content'), T.CustomTag(numb_attr=9), Br(), Doctype('html'), Comment('test comment'), T.Void.TestVoid()), ] T.dump(tempy_tree, filename) with open(filename, 'r') as f: self.assertEqual(Counter(f.read()), Counter(result)) os.remove(filename) T.dump(tempy_tree, 'test') os.remove(filename) with self.assertRaises(ValueError): T.dump(tempy_tree, 'test.jpg') with self.assertRaises(ValueError): T.dump(tempy_tree, None)
def test_inside_places(self): class Obj: foo = 'foo' bar = 'bar' class TestA(InsideDiv): def repr(self): self(self.bar) class A(InsideSpan): def repr(self): self(self.foo + 'test') class Test2(InsideP): def repr(self): self(self.bar + 'test') inst = Obj() self.assertEqual(Span()(A()(inst)).render(), '<span><a>footest</a></span>') inst = Obj() self.assertEqual(Div()(Div()(inst)).render(), '<div><div>bar</div></div>') inst = Obj() self.assertEqual(P()(T.CustomTag()(inst)).render(), '<p><customtag>bartest</customtag></p>')
def test_getattr(self): # Test T tempt tag factory __getattr__ custom_tag = T.CustomTag() self.assertIsInstance(custom_tag, Tag) self.assertIsInstance(custom_tag, DOMElement) self.assertEqual(custom_tag._CustomTag__tag, 'customtag') self.assertEqual(custom_tag.__class__.__name__, 'CustomTag')
def test_parser_nested(self): # Tests T parese with multiple nested elements html_string = '<div><a></a></div>' tempy_elements = T.from_string(html_string) self.assertIsInstance(tempy_elements[0], Div) self.assertIsInstance(tempy_elements[0][0], A) self.assertEqual(tempy_elements[0].render(), html_string)
def test_autolink(self): t = "https://hrabal.github.io/TemPy" r = T.from_markdown(t) self.assertEqual( r[0].render(), '<p><a href="https://hrabal.github.io/TemPy">https://hrabal.github.io/TemPy</a></p>' )
def test_parser_contents(self): # Parsed handling of tag contents content = "I'm a content" html_string = '<div>' + content + '</div>' temped = T.from_string(html_string)[0] self.assertEqual(temped.childs[0], content) self.assertEqual(temped.text(), content) self.assertEqual(temped.html(), html.escape(content))
def test_line_break(self): t = """Text in a paragraph that ends with two spaces other text""" r = T.from_markdown(t) self.assertEqual( r[0].render(), '<p>Text in a paragraph that ends with two spaces<br/>other text</p>' )
def test_code_block(self): t = """``` some code here ``` """ r = T.from_markdown(t) self.assertEqual(''.join(t.render() for t in r), '<pre><code>some code here</code></pre>')
def test_img(self): t = '![alt text](foo.png "Title text")' r = T.from_markdown(t) self.assertEqual( Counter(r[0].render()), Counter( '<p><img src="foo.png" alt="alt text" title="Title text"/></p>' ))
def test_parser_attrs(self): # Tests parser dealing with tag attributes html_string = '<div class="cssClass"></div>' temped = T.from_string(html_string)[0] self.assertTrue(temped.has_class("cssClass")) html_string = '<div class="cssClass" id="cssId"></div>' temped = T.from_string(html_string)[0] self.assertTrue(temped.is_id("cssId")) html_string = '<div custom_attr="foo"></div>' temped = T.from_string(html_string)[0] self.assertEqual(temped.attrs["custom_attr"], 'foo') html_string = '<div custom_bool_attr></div>' temped = T.from_string(html_string)[0] self.assertIsInstance(temped.attrs["custom_bool_attr"], bool)
def test_code_block_lang(self): t = """```python some code here ``` """ r = T.from_markdown(t) self.assertEqual( ''.join(t.render() for t in r), '<pre><code class="lang-python">some code here</code></pre>')
def test_list(self): t = """Important list: * foo * bar * baz""" r = T.from_markdown(t) self.assertEqual( ''.join(t.render() for t in r), '<p>Important list:</p><ul><li>foo</li><li>bar</li><li>baz</li></ul>' )
def test_list_numbered(self): t = """Important Numbered list: 1. foo 2. bar 3. baz""" r = T.from_markdown(t) self.assertEqual( ''.join(t.render() for t in r), '<p>Important Numbered list:</p><ol><li>foo</li><li>bar</li><li>baz</li></ol>' )
def test_table_non_styled_non_pretty(self): t = """Test markdown table non pretty Markdown | Less | Pretty --- | --- | --- *Still* | `renders` | **nicely** 1 | 2 | 3""" r = T.from_markdown(t) self.assertEqual( ''.join(t.render() for t in r), '<p>Test markdown table non pretty</p><table><thead><tr><th>Markdown</th><th>Less</th><th>Pretty</th></tr></thead><tbody><tr><td><em>Still</em></td><td><code>renders</code></td><td><strong>nicely</strong></td></tr><tr><td>1</td><td>2</td><td>3</td></tr></tbody></table>' )
def test_table_non_styled(self): t = """Test markdown table | Tables | Are | Cool | | ------------- | ------------- | ----- | | col 3 is | foo | $1600 | | col 2 is | bar. | $12 | | zebra stripes | baz. | $1 |""" r = T.from_markdown(t) self.assertEqual( ''.join(t.render() for t in r), '<p>Test markdown table</p><table><thead><tr><th>Tables</th><th>Are</th><th>Cool</th></tr></thead><tbody><tr><td>col 3 is</td><td>foo</td><td>$1600</td></tr><tr><td>col 2 is</td><td>bar.</td><td>$12</td></tr><tr><td>zebra stripes</td><td>baz.</td><td>$1</td></tr></tbody></table>' )
def test_table_styled(self): t = """Test markdown table | Tables | Are | Cool | | ------------- |:-------------:| -----:| | col 3 is | right-aligned | $1600 | | col 2 is | centered | $12 | | zebra stripes | are neat | $1 |""" r = T.from_markdown(t) self.assertEqual( ''.join(t.render() for t in r), '<p>Test markdown table</p><table><thead><tr><th>Tables</th><th style="text-align: center;">Are</th><th style="text-align: right;">Cool</th></tr></thead><tbody><tr><td>col 3 is</td><td style="text-align: center;">right-aligned</td><td style="text-align: right;">$1600</td></tr><tr><td>col 2 is</td><td style="text-align: center;">centered</td><td style="text-align: right;">$12</td></tr><tr><td>zebra stripes</td><td style="text-align: center;">are neat</td><td style="text-align: right;">$1</td></tr></tbody></table>' )
def test_parser_base(self): # Test the T parser div_string = '<div></div>' tempy_elements = T.from_string(div_string) self.assertIsInstance(tempy_elements[0], Div) a_string = '<a></a>' tempy_elements = T.from_string(a_string) self.assertIsInstance(tempy_elements[0], A) br_string = '<br>' tempy_elements = T.from_string(br_string) self.assertIsInstance(tempy_elements[0], Br) custom_string = '<custom>' tempy_elements = T.from_string(custom_string) self.assertIsInstance(tempy_elements[0], DOMElement) self.assertIsInstance(tempy_elements[0], Tag) custom_string = '<custom_void/>' tempy_elements = T.from_string(custom_string) self.assertIsInstance(tempy_elements[0], VoidTag) self.assertIsInstance(tempy_elements[0], DOMElement) self.assertIsInstance(tempy_elements[0], Tag)
def test_near_places(self): class Obj: foo = 'foo' bar = 'bar' class TestA(NearDiv): def repr(self): self(self.bar) class TestB(NearSpan): def repr(self): self(self.foo + 'test') inst = Obj() self.assertEqual(Pre()(Span(), A()(inst)).render(), '<pre><span></span><a>footest</a></pre>') self.assertEqual(Pre()(T.Custom()(inst), Div()).render(), '<pre><custom>bar</custom><div></div></pre>')
def test_markdown_escape(self): t = """&""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<p>&</p>')
def test_markdown_return_P_wrap(self): t = 'Lorem ipsum' r = T.from_markdown(t) self.assertIsInstance(r[0], P)
def test_hr(self): t = "---" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<hr/>')
def test_strikethrough(self): t = """~~strikethrough~~""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<p><del>strikethrough</del></p>')
def test_link(self): t = """A [link](https://hrabal.github.io/TemPy/).""" r = T.from_markdown(t) self.assertEqual( r[0].render(), '<p>A <a href="https://hrabal.github.io/TemPy/">link</a>.</p>')
def test_bold(self): t = """**bold**""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<p><strong>bold</strong></p>')
def test_code_inline(self): t = """`monospace`""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<p><code>monospace</code></p>')
def test_italic(self): t = """*italic*""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<p><em>italic</em></p>')
def test_email_link(self): t = "<*****@*****.**>" r = T.from_markdown(t) self.assertEqual( r[0].render(), '<p><a href="mailto:[email protected]">[email protected]</a></p>')
def test_h3_heading(self): t = """### H3 heading""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<h3>H3 heading</h3>')
def test_sub_heading(self): t = """Sub-heading -----------""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<h2>Sub-heading</h2>')
def test_heading(self): t = """Heading =======""" r = T.from_markdown(t) self.assertEqual(r[0].render(), '<h1>Heading</h1>')