Tests for remarkuple

Remarkuple XML/HTML tag generator (or factory if you prefer it that way): https://github.com/markomanninen/remarkuple

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.

Load ipyntester extension and runaway function


In [1]:
%load_ext ipynbtester


The ipynbtester module is not an IPython extension.

While loading extension, IPython notebook informs that module is not part of the core distribution, but third party module instead.

Import libraries for tests


In [2]:
from remarkuple import helper as h, table, concat

Define tests


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 'remarkuple.main.tag'>"

def test_get_tag_name():
    set_up()
    assert tag().__class__.__name__ == '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_add_content_method():
    set_up()
    assert str(tag(id=1).addContent(tag(id=2)).addContent(tag(id=3))) == '<tag id="1"><tag id="2"/><tag id="3"/></tag>'

def test_tag_add_content_plus_method():
    set_up()
    t = tag(id=1)
    t += tag(id=2)
    t += tag(id=3)
    assert str(t) == '<tag id="1"><tag id="2"/><tag id="3"/></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_set_attribute_with_dot_notation():
    set_up()
    t = tag()
    t.key = "value"
    assert str(t) == '<tag key="value"/>'

def test_tag_set_get_attribute_with_dot_notation():
    set_up()
    assert tag().setAttribute('key', 'value').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.create('DEL')) == '<DEL/>'

def test_concat_tag2_after_tag1():
    assert str(concat(h.tag(id=1), h.tag(id=2))) == '<tag id="1"/><tag id="2"/>'

def test_concat_number_tag_and_string_together():
    assert str(concat(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

Show example table made with remarkuple


In [4]:
from IPython.display import HTML
get_table()


Out[4]:
Caption
Column 1Column 2Column 3
footer
1.12.13.1
1.22.23.2
1.32.33.3
1.42.43.4

Run actual tests


In [5]:
%runaway


Out[5]:
Collected 35 tests.
Test function nameStatus
Successful35
Failed0
Errors0
Execution0.02162 seconds
test_tablesuccessful
test_table_headsuccessful
test_concat_tag2_after_tag1successful
test_string_uncalled_tagsuccessful
test_single_tag_without_content_and_attributessuccessful
test_tag_set_get_attribute_with_dot_notationsuccessful
test_tag_argument_attribute_intsuccessful
test_tag_reserved_word_attributesuccessful
test_table_footsuccessful
test_concat_number_tag_and_string_togethersuccessful
test_tag_is_tagsuccessful
test_tag_is_called_tagsuccessful
test_table_colgroupsuccessful
test_tag_set_attribute_with_dot_notationsuccessful
test_tag_add_content_methodsuccessful
test_tag_with_tag_contentsuccessful
test_tag_set_attributesuccessful
test_tag_with_list_argssuccessful
test_tag_argument_attribute_stringsuccessful
test_tag_add_content_plus_methodsuccessful
test_table_bodysuccessful
test_tag_with_str_num_tag_contentsuccessful
test_tag_with_text_contentsuccessful
test_tag_dictionary_attribute_stringsuccessful
test_whole_tablesuccessful
test_get_tag_namesuccessful
test_table_captionsuccessful
test_tag_with_two_args_tagssuccessful
test_tag_reserve_word_set_namesuccessful
test_tag_get_attributesuccessful
test_tag_with_uncalled_tag_contentsuccessful
test_tag_reserve_word_namesuccessful
test_single_tag_with_empty_contentsuccessful
test_tag_dictionary_attribute_intsuccessful
test_table_bodiessuccessful

Styles for table


In [6]:
from IPython.core.display import HTML
with open('styles.css') as f:
    css = f.read()
HTML('<style>%s</style>' % css)


Out[6]:

The MIT License

Copyright (c) 2014 Marko Manninen