def test_imul_zero(self): a = A() div = Div() div(P(), a, P()) self.assertEqual(len(div), 3) a *= 0 self.assertEqual(len(div), 2) self.assertIsInstance(div[1], P)
def test_imul(self): a = A() div = Div() div(P(), a, P()) self.assertEqual(len(div), 3) a *= 2 self.assertTrue(a in div) self.assertEqual(len(div), 4) self.assertIsInstance(div[2], A) b = Div() b *= 2 self.assertEqual(b, [Div(), Div()])
def test_page(self): self.maxDiff = None expected = '<!DOCTYPE HTML><html><head><meta charset="utf-8"/><link href="my.css" type="text/css" ' \ 'rel="stylesheet"/><title>test_title</title></head><body><div class="linkBox"><a href="www.' \ 'foo.com">www.foo.com</a></div><p>This is foo</p><p>This is Bar</p><p>Have you met my friend Baz?' \ '</p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</body></html>' my_text_list = [ 'This is foo', 'This is Bar', 'Have you met my friend Baz?' ] another_list = [ 'Lorem ipsum ', 'dolor sit amet, ', 'consectetur adipiscing elit' ] page = Html( )( # add tags inside the one you created calling the parent Head()( # add multiple tags in one call Meta( charset='utf-8' ), # add tag attributes using kwargs in tag initialization Link(href="my.css", typ="text/css", rel="stylesheet"), Title('test_title')), body=Body() ( # give them a name so you can navigate the DOM with those names Div(klass='linkBox')(A(href='www.foo.com')), (P()(text) for text in my_text_list), # tag insertion accepts generators another_list # add text from a list, str.join is used in rendering )) self.assertEqual(Counter(page.render()), Counter(expected))
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_wrap_inner(self): d1, p, a = Div(), P(), A() d1(a) d1.wrap_inner(p) self.assertFalse(a in d1) self.assertTrue(a in p) self.assertTrue(p in d1)
def test_content_from_granpa(self): cont = Content(name='test1') p = P()(Div()(cont)) p.inject(self.test_contents) self.assertEqual(list(cont.content), [ 1, ])
def test_isub(self): a = A() div = Div() div(a) div -= a self.assertFalse(a in div) with self.assertRaises(ValueError): div -= P()
def test_move_childs(self): childs = [A(), P(), 'test', 0] d1 = Div()(childs) d2 = Div() d1.move_childs(d2) self.assertFalse(d1.childs) self.assertTrue(d2.childs) self.assertEqual(childs[0].parent, d2)
def test__find_content(self): a = Div() a.inject(test='test') b = Div() a(b) b._find_content('test') c = P() c._find_content('test')
def test_sub(self): a = A() div = Div() div(a) result = div - a self.assertFalse(a in result) self.assertTrue(a in div) self.assertIsNot(div, result) with self.assertRaises(ValueError): _ = div - P()
def test_wrap(self): container = Div() new = A().wrap(container) self.assertTrue(new in container) with self.assertRaises(TagError): A().wrap(Div()(P())) container = Div() to_wrap = Div() outermost = Div() outermost(to_wrap) to_wrap.wrap(container) self.assertTrue(to_wrap in container) self.assertTrue(container in outermost)
def init(self): self.body.main.container.attr(klass="album py-5 bg-light") self.body.main.container( Div(klass="container")(Div(klass='row')(persons=[ Div(klass='col-md-4')(Div(klass='card mb-4 shadow-sm')(A( href=f'person/{person.person_id}')(Div( klass='card-body' )(P(klass='card-text') (f'{person.name.title()} {person.second_name.title()}'), contacts=[ Div(klass='contactIcon')(I(klass=CONTACTS_ICONS.get( contact.contact_type, 'noIcon'))) for contact in person.contacts ])))) for person in self.content_data['people'] ])))
def test_copy(self): from copy import copy # Empty tag copy generates equal div d1 = Div(test='test')() d2 = copy(d1) self.assertEqual(d1, d2) # A copy of a copy is a copy of the original d3 = copy(d2) self.assertEqual(d1, d3) # Non empty tag copy generates different tag but with same structure d1(P()) d3 = copy(d1) self.assertNotEqual(d1, d3) self.assertEqual(len(d1.childs), len(d3.childs)) self.assertEqual(d1.childs[0].__class__, d3.childs[0].__class__)
def test_pop(self): new = Div().append_to(self.page) self.page.pop(0) self.assertTrue(new not in self.page) new2 = Div().append_to(self.page) self.page.pop() self.assertTrue(new2 not in self.page) new3 = Div() self.page(child_foo=new3) self.page.pop('child_foo') self.assertTrue(new3 not in self.page) new4, new5 = Div(), Div() self.page(child_foo_1=new4) self.page(child_foo_2=new4) self.page.pop(['child_foo_1', 'child_foo_2']) self.assertTrue(new4 not in self.page and new5 not in self.page) with self.assertRaises(DOMModByKeyError): test = Div()(test_key=A()) test.pop('nonexistentkey') with self.assertRaises(DOMModByIndexError): test = Div()(A(), P()) test.pop(2)
def test_contents(self): test = [A(), Div(), P()] div = Div()(test) self.assertEqual(div.contents(), test)
def repr(self): self(P()(self.foo), P()(self.bar))
def repr(self): self(Div()(P()(self.foo)), P()(self.bar))
def test_iter_reversed(self): d = Div() childs = [A(), P(), P(), Div(), 'test', 1] d(childs) for t_child, child in zip(reversed(childs), reversed(d)): self.assertEqual(t_child, child)
def test_next_childs(self): d = Div() childs = [A(), P(), P(), Div(), 'test', 1] d(childs) self.assertEqual(childs[0], next(d))
def test_iter_chidls(self): d = Div() childs = [A(), P(), P(), Div(), 'test', 1] d(childs) for i, child in enumerate(d): self.assertEqual(childs[i], child)
def test_childs_index(self): div = Div() a = A() div(P(), P(), a) self.assertEqual(div[2], a)
from tempy.tags import Html, Head, Body, Meta, Link, Div, P, A my_text_list = ['This is foo', 'This is Bar', 'Have you met my friend Baz?'] another_list = ['Lorem ipsum ', 'dolor sit amet, ', 'consectetur adipiscing elit'] # make tags instantiating TemPy objects page = Html()( # add tags inside the one you created calling the parent Head()( # add multiple tags in one call Meta(charset='utf-8'), # add tag attributes using kwargs in tag initialization Link(href="my.css", typ="text/css", rel="stylesheet") ), body=Body()( # give them a name so you can navigate the DOM with those names Div(klass='linkBox')( A(href='www.foo.com') ), (P()(text) for text in my_text_list), # tag insertion accepts generators another_list # add text from a list, str.join is used in rendering ) ) # add tags and content later page[1][0](A(href='www.bar.com')) # calling the tag page(test=Div()) # WARNING! Correct ordering with named Tag insertion is ensured with Python >= 3.5 (because kwargs are ordered) page[1][0].append(A(href='www.baz.com')) # using the API link = Link().append_to(page.body) # access the body as if it's a page attribute link.attr(href='www.python.org')('This is a link to Python') # Add attributes and content to already placed tags page.render() # does not return on Python shell testfile = open("tempyTest.html", "w") print(page.render(), file=testfile) testfile.close()
def test_parent(self): d = Div() p = P().append_to(d) self.assertEqual(p.parent, d)
from tempy.tags import Html, Head, Body, Meta, Link, Div, P, A my_text_list = ['This is foo', 'This is Bar', 'Have you met my friend Baz?'] another_list = [ 'Lorem ipsum ', 'dolor sit amet, ', 'consectetur adipiscing elit' ] # make tags instantiating TemPy objects page = Html()( # add tags inside the one you created calling the parent Head()( # add multiple tags in one call Meta(charset='utf-8' ), # add tag attributes using kwargs in tag initialization Link(href="my.css", typ="text/css", rel="stylesheet")), body=Body() ( # give them a name so you can navigate the DOM with those names Div(klass='linkBox')(A(href='www.foo.com')), (P()(text) for text in my_text_list), # tag insertion accepts generators another_list # add text from a list, str.join is used in rendering )) # add tags and content later page[1][0](A(href='www.bar.com')) # calling the tag page[1][0].append(A(href='www.baz.com')) # using the API link = A().append_to( page.body[0]) # access the body as if it's a page attribute page.body( testDiv=Div() ) # WARNING! Correct ordering with named Tag insertion is ensured with Python >= 3.5 (because kwargs are ordered) link.attr(href='www.python.org')( 'This is a link to Python.' ) # Add attributes and content to already placed tags
def test_children(self): test = [A(), Div(), P(), 'test'] div = Div()(test) self.assertEqual(list(div.children()), test[:-1])
def test_next_magic(self): div = Div()(A(), P(), Div()) test = next(div) self.assertTrue(isinstance(test, A))
def test_reverse(self): div = Div()(A(), Div(), P()) test = next(reversed(div)) self.assertTrue(isinstance(test, P))