Tagpy XML/HTML tag generator (or factory if you prefer it that way): https://github.com/markomanninen/tagpy
iPython notebook extension for running tests is forged from: https://gist.github.com/jiffyclub/4013594 Modifications for runtests concerns only output so that instead of plain text output I'm using more pleasant html table format.
In [1]:
# Uncomment to run test script from local directory
#%install_ext runtests.py
# Load extension from remote host
%install_ext https://raw.githubusercontent.com/markomanninen/tagpy/master/runtests.py
%load_ext runtests
In [2]:
from tagpy import helper as h, table
In [3]:
tag = None
def set_up():
global tag
tag = h.tag
def test_tag_is_tag():
set_up()
assert tag == tag
def test_tag_is_called_tag():
set_up()
assert tag != tag()
def test_string_uncalled_tag():
set_up()
assert str(tag) == "<class 'tagpy.main.tag'>"
def test_get_tag_name():
set_up()
assert tag().getName() == 'tag'
def test_single_tag_without_content_and_attributes():
set_up()
assert str(tag()) == '<tag/>'
def test_single_tag_with_empty_content():
set_up()
assert str(tag('')) == '<tag></tag>'
def test_tag_with_text_content():
set_up()
assert str(tag('text')) == '<tag>text</tag>'
def test_tag_with_tag_content():
set_up()
assert str(tag(tag())) == '<tag><tag/></tag>'
def test_tag_with_two_args_tags():
set_up()
assert str(tag(tag, tag)) == '<tag><tag/><tag/></tag>'
def test_tag_with_list_args():
set_up()
assert str(tag(*[tag, tag])) == '<tag><tag/><tag/></tag>'
def test_tag_with_uncalled_tag_content():
set_up()
assert str(tag(tag)) == '<tag><tag/></tag>'
def test_tag_with_lshift_tag_content():
set_up()
assert str(tag(id=1) << tag(id=2) << tag(id=3)) == '<tag id="1"><tag id="2"/><tag id="3"/></tag>'
def test_tag_with_rshift_tag_content():
set_up()
assert str(tag(id=1) >> tag(id=2) >> tag(id=3)) == '<tag id="1"><tag id="3"/><tag id="2"/></tag>'
def test_tag_content_method():
set_up()
assert str(tag(id=1).content(tag(id=2)).content(tag(id=3))) == '<tag id="1"><tag id="2"/><tag id="3"/></tag>'
def test_tag_rcontent_method():
set_up()
assert str(tag(id=1).rcontent(tag(id=2)).rcontent(tag(id=3))) == '<tag id="1"><tag id="3"/><tag id="2"/></tag>'
def test_tag_with_str_num_tag_content():
set_up()
assert str(tag('a', 1, tag, 2, 'b')) == '<tag>a1<tag/>2b</tag>'
def test_tag_argument_attribute_string():
set_up()
assert str(tag(key='value')) == '<tag key="value"/>'
def test_tag_argument_attribute_int():
set_up()
assert str(tag(value=1)) == '<tag value="1"/>'
def test_tag_dictionary_attribute_string():
set_up()
assert str(tag(**{'key': 'value'})) == '<tag key="value"/>'
def test_tag_dictionary_attribute_int():
set_up()
assert str(tag(**{'value': 1})) == '<tag value="1"/>'
def test_tag_set_attribute():
set_up()
assert str(tag().setAttribute('key', 'value')) == '<tag key="value"/>'
def test_tag_get_attribute():
set_up()
assert tag().setAttribute('key', 'value').getAttribute('key') == 'value'
def test_tag_reserved_word_attribute():
set_up()
assert str(tag(CLASS='tag')) == '<tag class="tag"/>'
def test_tag_reserve_word_name():
set_up()
assert str(h.DEL()) == '<del/>'
def test_tag_reserve_word_set_name():
set_up()
assert str(h.DEL().setName('TAG')) == '<TAG/>'
def test_append_tag2_after_tag1():
assert str(h.tag(id=1).append(h.tag(id=2))) == '<tag id="1"/><tag id="2"/>'
def test_prepend_tag2_before_tag1():
assert str(h.tag(id=1).prepend(h.tag(id=2))) == '<tag id="2"/><tag id="1"/>'
def test_append_tag2_after_tag1_with_plus():
assert str(h.tag(id=1) + h.tag(id=2)) == '<tag id="1"/><tag id="2"/>'
def test_append_tag2_after_tag1_with_plus2():
tags = h.tag(id=1)
tags += h.tag(id=2)
assert str(tags) == '<tag id="1"/><tag id="2"/>'
def test_append_three_tags_together():
assert str(h.tag(id=1) + h.tag(id=2) + h.tag(id=3)) == '<tag id="1"/><tag id="2"/><tag id="3"/>'
def test_append_number_tag_and_string_together():
assert str(1 + h.tag() + "2" + h.tag() + 3) == '1<tag/>2<tag/>3'
tbl = None
def set_up_table():
global tbl
tbl = table(Class='data')
def test_table():
set_up_table()
assert str(tbl) == '<table class="data"/>'
def test_table_caption():
set_up_table()
tbl.addCaption('Caption')
assert str(tbl) == '<table class="data"><caption>Caption</caption></table>'
def test_table_colgroup():
set_up_table()
tbl.addColGroup(h.col(), h.col())
assert str(tbl) == '<table class="data"><colgroup><col/><col/></colgroup></table>'
def test_table_head():
set_up_table()
tbl.addHeadRow(h.tr(h.th('head')))
assert str(tbl) == '<table class="data"><thead><tr><th>head</th></tr></thead></table>'
def test_table_foot():
set_up_table()
tbl.addFootRow(h.tr(h.td('foot')))
assert str(tbl) == '<table class="data"><tfoot><tr><td>foot</td></tr></tfoot></table>'
def test_table_body():
set_up_table()
tbl.addBodyRow(h.tr(h.td('body 1.1')))
tbl.addBodyRow(h.tr(h.td('body 1.2')))
assert str(tbl) == '<table class="data"><tbody><tr><td>body 1.1</td></tr><tr><td>body 1.2</td></tr></tbody></table>'
def test_table_bodies():
set_up_table()
tbl.addBodyRows(h.tr(h.td('body 2.1')))
tbl.addBodyRows(h.tr(h.td('body 3.1')))
assert str(tbl) == '<table class="data"><tbody><tr><td>body 2.1</td></tr></tbody><tbody><tr><td>body 3.1</td></tr></tbody></table>'
def test_whole_table():
t = get_table()
assert str(t) == '<table class="data"><caption>Caption</caption><colgroup><col class="col1"/><col class="col2"/><col class="col3"/></colgroup><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tfoot><tr><td colspan="3">footer</td></tr></tfoot><tbody><tr><td>1.1</td><td>2.1</td><td>3.1</td></tr><tr><td>1.2</td><td>2.2</td><td>3.2</td></tr><tr><td>1.3</td><td>2.3</td><td>3.3</td></tr></tbody><tbody id="tbody4"><tr><td>1.4</td><td>2.4</td><td>3.4</td></tr></tbody></table>'
def get_table():
t = table(**{'class': 'data'})
t.addCaption('Caption')
columns = [{'class': 'col1'},
{'class': 'col2'},
{'class': 'col3'}]
t.addColGroup(*[h.col(**attr) for attr in columns])
header = ['Column 1', 'Column 2', 'Column 3']
t.addHeadRow(h.tr(*map(h.th, header)))
for i in range(1,4):
t.addBodyRow(h.tr(*map(h.td, ["1.%s"%i,"2.%s"%i,"3.%s"%i])))
for i in range(4,5):
t.addBodyRows(h.tr(*map(h.td, ["1.%s"%i,"2.%s"%i,"3.%s"%i])), id='tbody%s'%i)
t.addFootRow(h.tr(h.td('footer', colspan="3")))
return t
In [4]:
from IPython.display import HTML
HTML(str(get_table()))
Out[4]:
In [5]:
%runtests
Out[5]:
In [6]:
from IPython.core.display import HTML
from urllib import urlopen
url = "https://raw.githubusercontent.com/markomanninen/tagpy/master/runtests.css"
HTML('<style type="text/css">%s</style>' % urlopen(url).read())
Out[6]:
Copyright (c) 2014 Marko Manninen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.