#!/bin/python
# -*- coding: utf-8 -*-
"""
Unit tests for gluon.html
"""
import unittest
from gluon.html import A, ASSIGNJS, B, BEAUTIFY, P, BODY, BR, BUTTON, CAT, CENTER, CODE, COL, COLGROUP, DIV, SPAN, URL, verifyURL
from gluon.html import truncate_string, EM, FIELDSET, FORM, H1, H2, H3, H4, H5, H6, HEAD, HR, HTML, I, IFRAME, IMG, INPUT, EMBED
from gluon.html import LABEL, LEGEND, LI, LINK, MARKMIN, MENU, META, OBJECT, OL, OPTGROUP, OPTION, PRE, SCRIPT, SELECT, STRONG
from gluon.html import STYLE, TABLE, TR, TD, TAG, TBODY, THEAD, TEXTAREA, TFOOT, TH, TITLE, TT, UL, XHTML, XML, web2pyHTMLParser
from gluon.storage import Storage
from gluon.html import XML_pickle, XML_unpickle
from gluon.html import TAG_pickler, TAG_unpickler
from gluon._compat import xrange, PY2, to_native
from gluon.decoder import decoder
import re
class TestBareHelpers(unittest.TestCase):
# xmlescape() = covered by other tests
# TODO: def test_call_as_list(self):
def test_truncate_string(self):
# Ascii text
self.assertEqual(truncate_string('Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
length=30), 'Lorem ipsum dolor sit amet,...')
self.assertEqual(truncate_string('Short text shorter than the length parameter.', length=100),
'Short text shorter than the length parameter.')
# French text
self.assertEqual(truncate_string('Un texte en français avec des accents et des caractères bizarre.', length=30),
'Un texte en français avec d...')
def test_StaticURL(self):
# test response.static_version coupled with response.static_version_urls
self.assertEqual(URL('a', 'c', 'f'), '/a/c/f')
self.assertEqual(URL('a', 'static', 'design.css'), '/a/static/design.css')
response = Storage()
response.static_version = '1.2.3'
from gluon.globals import current
current.response = response
self.assertEqual(URL('a', 'static', 'design.css'), '/a/static/design.css')
response.static_version_urls = True
self.assertEqual(URL('a', 'static', 'design.css'), '/a/static/_1.2.3/design.css')
def test_URL(self):
self.assertEqual(URL('a', 'c', 'f', args='1'), '/a/c/f/1')
self.assertEqual(URL('a', 'c', 'f', args=('1', '2')), '/a/c/f/1/2')
self.assertEqual(URL('a', 'c', 'f', args=['1', '2']), '/a/c/f/1/2')
self.assertEqual(URL('a', 'c', '/f'), '/a/c/f')
self.assertEqual(URL('a', 'c', 'f.json'), '/a/c/f.json')
from gluon.globals import current
current.request = None
self.assertRaises(SyntaxError, URL, *['a'])
request = Storage()
request.application = 'a'
request.controller = 'c'
request.function = 'f'
request.env = {}
from gluon.globals import current # Can't be moved with other import
current.request = request
must_return = '/a/c/f'
self.assertEqual(URL(), must_return)
self.assertEqual(URL('f'), must_return)
self.assertEqual(URL('c', 'f'), must_return)
self.assertEqual(URL('a', 'c', 'f'), must_return)
self.assertEqual(URL('a', 'c', 'f', extension='json'), '/a/c/f.json')
def weird():
pass
self.assertEqual(URL('a', 'c', weird), '/a/c/weird')
self.assertRaises(SyntaxError, URL, *['a', 'c', 1])
# test signature
rtn = URL(a='a', c='c', f='f', args=['x', 'y', 'z'],
vars={'p': (1, 3), 'q': 2}, anchor='1', hmac_key='key')
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1')
# test _signature exclusion
rtn = URL(a='a', c='c', f='f', args=['x', 'y', 'z'],
vars={'p': (1, 3), 'q': 2, '_signature': 'abc'},
anchor='1', hmac_key='key')
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1')
# emulate user_signature
current.session = Storage(auth=Storage(hmac_key='key'))
self.assertEqual(URL(user_signature=True), '/a/c/f?_signature=c4aed53c08cff08f369dbf8b5ba51889430cf2c2')
# hash_vars combination
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key')
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars=True)
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars=False)
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=0b5a0702039992aad23c82794b8496e5dcd59a5b')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars=['p'])
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'p': (1, 3), 'q': 2}, hmac_key='key', hash_vars='p')
self.assertEqual(rtn, '/a/c/f/x/y/z?p=1&p=3&q=2&_signature=5d01b982fd72b39674b012e0288071034e156d7a')
# test CRLF detection
self.assertRaises(SyntaxError, URL, *['a\n', 'c', 'f'])
self.assertRaises(SyntaxError, URL, *['a\r', 'c', 'f'])
# test url_encode
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'maï': (1, 3), 'lié': 2}, url_encode=True)
self.assertEqual(rtn, '/a/c/f/x/y/z?li%C3%A9=2&ma%C3%AF=1&ma%C3%AF=3')
@unittest.skipIf(not PY2, "Skipping Python 3.x tests for test_URL_encode")
def test_URL_encode(self):
rtn = URL('a', 'c', 'f', args=['x', 'y', 'z'], vars={'maï': (1, 3), 'lié': 2}, url_encode=False)
self.assertEqual(rtn, '/a/c/f/x/y/z?li\xc3\xa9=2&ma\xc3\xaf=1&ma\xc3\xaf=3')
def test_verifyURL(self):
r = Storage()
r.application = 'a'
r.controller = 'c'
r.function = 'f'
r.extension = 'html'
r.env = {}
r.get_vars = Storage()
# missing signature as request.get_vars returns False
rtn = verifyURL(r, 'key')
self.assertEqual(rtn, False)
# reverse tests from previous testcase with hash_vars combinations
r.args = ['x', 'y', 'z']
r.get_vars = Storage(p=(1, 3), q=2)
# add signature
r.get_vars['_signature'] = 'a32530f0d0caa80964bb92aad2bedf8a4486a31f'
rtn = verifyURL(r, 'key')
self.assertEqual(rtn, True)
r.get_vars['_signature'] = 'a32530f0d0caa80964bb92aad2bedf8a4486a31f'
rtn = verifyURL(r, 'key', hash_vars=True)
self.assertEqual(rtn, True)
r.get_vars['_signature'] = '0b5a0702039992aad23c82794b8496e5dcd59a5b'
rtn = verifyURL(r, 'key', hash_vars=False)
self.assertEqual(rtn, True)
r.get_vars['_signature'] = '5d01b982fd72b39674b012e0288071034e156d7a'
rtn = verifyURL(r, 'key', hash_vars=['p'])
self.assertEqual(rtn, True)
r.get_vars['_signature'] = '5d01b982fd72b39674b012e0288071034e156d7a'
rtn = verifyURL(r, 'key', hash_vars='p')
self.assertEqual(rtn, True)
# without session, user_signature returns always False
rtn = verifyURL(r, user_signature=True)
self.assertEqual(rtn, False)
# same goes if you don't use an hmac_key
rtn = verifyURL(r)
self.assertEqual(rtn, False)
# emulate user signature
from gluon.globals import current
current.session = Storage(auth=Storage(hmac_key='key'))
r.get_vars['_signature'] = 'a32530f0d0caa80964bb92aad2bedf8a4486a31f'
rtn = verifyURL(r, user_signature=True)
self.assertEqual(rtn, True)
# TODO: def test_XmlComponent(self):
def test_XML(self):
# sanitization process
self.assertEqual(XML('
').xml(),
b'')
# with sanitize, data-attributes are not permitted
self.assertEqual(XML('', sanitize=True).xml(),
b'HelloWorld ')
# stringify by default
# FIXME PY3
# seams that __repr__ is no longer enough
##self.assertEqual(XML('1.3'), '1.3')
self.assertEqual(XML(u'è
').xml(), b'\xc3\xa8
')
# make sure unicode works with sanitize
self.assertEqual(XML(u'è
', sanitize=True).xml(), b'\xc3\xa8
')
# you can calc len on the class, that equals the xml() and the str()
##self.assertEqual(len(XML('1.3')), len('1.3'))
self.assertEqual(len(XML('1.3').xml()), len('1.3'))
##self.assertEqual(len(str(XML('1.3'))), len('1.3'))
# you can concatenate them to strings (check for __add__ and __radd__ methods)
##self.assertEqual(XML('a') + 'b', 'ab')
##self.assertEqual(XML('a') + XML('b'), 'ab')
##self.assertEqual('a' + XML('b'), 'ab')
# you can compare them
##self.assertEqual(XML('a') == XML('a'), True)
# beware that the comparison is made on the XML repr
self.assertEqual(XML('', sanitize=True).__repr__(),
XML('HelloWorld ').__repr__())
# bug check for the sanitizer for closing no-close tags
self.assertEqual(XML('Test
Test
', sanitize=True).xml(),
XML('Test
Test
').xml())
# basic flatten test
self.assertEqual(XML('Test
').flatten(), 'Test
')
self.assertEqual(XML('Test
').flatten(render=lambda text, tag, attr: text), 'Test
')
def test_XML_pickle_unpickle(self):
self.assertEqual(str(XML_unpickle(XML_pickle('data to be pickle')[1][0])), 'data to be pickle')
def test_DIV(self):
# Empty DIV()
self.assertEqual(DIV().xml(), b'
')
self.assertEqual(DIV('<>', _a='1', _b='2').xml(),
b'<>
')
# attributes can be updated like in a dict
div = DIV('<>', _a='1')
div['_b'] = '2'
self.assertEqual(div.xml(),
b'<>
')
# also with a mapping
div.update(_b=2, _c=3)
self.assertEqual(div.xml(),
b'<>
')
# length of the DIV is the number of components
self.assertEqual(len(DIV('a', 'bc')), 2)
# also if empty, DIV is True in a boolean evaluation
self.assertTrue(True if DIV() else False)
# parent and siblings
a = DIV(SPAN('a'), DIV('b'))
s = a.element('span')
d = s.parent
d['_class'] = 'abc'
self.assertEqual(a.xml(), b'')
self.assertEqual([el.xml() for el in s.siblings()], [b'b
'])
self.assertEqual(s.sibling().xml(), b'b
')
# siblings with wrong args
self.assertEqual(s.siblings('a'), [])
# siblings with good args
self.assertEqual(s.siblings('div')[0].xml(), b'b
')
# Check for siblings with wrong kargs and value
self.assertEqual(s.siblings(a='d'), [])
# Check for siblings with good kargs and value
# Can't figure this one out what is a right value here??
# Commented for now...
# self.assertEqual(s.siblings(div='b
'), ???)
# No other sibling should return None
self.assertEqual(DIV(P('First element')).element('p').sibling(), None)
# --------------------------------------------------------------------------------------------------------------
# This use unicode to hit xmlescape() line :
# """
# elif isinstance(data, unicode):
# data = data.encode('utf8', 'xmlcharrefreplace')
# """
self.assertEqual(DIV(u'Texte en français avec des caractères accentués...').xml(),
b'Texte en fran\xc3\xa7ais avec des caract\xc3\xa8res accentu\xc3\xa9s...
')
# --------------------------------------------------------------------------------------------------------------
self.assertEqual(DIV('Test with an ID', _id='id-of-the-element').xml(),
b'Test with an ID
')
self.assertEqual(DIV().element('p'), None)
# Corner case for raise coverage of one line
# I think such assert fail cause of python 2.6
# Work under python 2.7
# with self.assertRaises(SyntaxError) as cm:
# DIV(BR('<>')).xml()
# self.assertEqual(cm.exception[0], ' tags cannot have components')
# test .get('attrib')
self.assertEqual(DIV('Test
', _class="class_test").get('_class'), 'class_test')
self.assertEqual(DIV(b'a').xml(), b'a
')
def test_decoder(self):
tag_html = ''
a = decoder(tag_html)
self.assertEqual(a, tag_html)
def test_CAT(self):
# Empty CAT()
self.assertEqual(CAT().xml(), b'')
# CAT('')
self.assertEqual(CAT('').xml(), b'')
# CAT(' ')
self.assertEqual(CAT(' ').xml(), b' ')
def test_TAG_pickler_unpickler(self):
# weird test
self.assertEqual(TAG_unpickler(TAG_pickler(TAG.div('data to be pickle'))[1][0]).xml(),
b'data to be pickle
')
def test_TAG(self):
self.assertEqual(TAG.first(TAG.second('test'), _key=3).xml(),
b'test ')
# ending in underscore "triggers" style
self.assertEqual(TAG.first_(TAG.second('test'), _key=3).xml(),
b' ')
# unicode test for TAG
self.assertEqual(TAG.div(u'Texte en français avec des caractères accentués...').xml(),
b'Texte en fran\xc3\xa7ais avec des caract\xc3\xa8res accentu\xc3\xa9s...
')
def test_HTML(self):
self.assertEqual(HTML('<>', _a='1', _b='2').xml(),
b'\n<>')
self.assertEqual(HTML('<>', _a='1', _b='2', doctype='strict').xml(),
b'\n<>')
self.assertEqual(HTML('<>', _a='1', _b='2', doctype='transitional').xml(),
b'\n<>')
self.assertEqual(HTML('<>', _a='1', _b='2', doctype='frameset').xml(),
b'\n<>')
self.assertEqual(HTML('<>', _a='1', _b='2', doctype='html5').xml(),
b'\n<>')
self.assertEqual(HTML('<>', _a='1', _b='2', doctype='').xml(),
b'<>')
self.assertEqual(HTML('<>', _a='1', _b='2', doctype='CustomDocType').xml(),
b'CustomDocType\n<>')
def test_XHTML(self):
# Empty XHTML test
self.assertEqual(XHTML().xml(),
b'\n')
# Not Empty XHTML test
self.assertEqual(XHTML('<>', _a='1', _b='2').xml(),
b'\n<>')
self.assertEqual(XHTML('<>', _a='1', _b='2', doctype='').xml(),
b'\n<>')
self.assertEqual(XHTML('<>', _a='1', _b='2', doctype='strict').xml(),
b'\n<>')
self.assertEqual(XHTML('<>', _a='1', _b='2', doctype='transitional').xml(),
b'\n<>')
self.assertEqual(XHTML('<>', _a='1', _b='2', doctype='frameset').xml(),
b'\n<>')
self.assertEqual(XHTML('<>', _a='1', _b='2', doctype='xmlns').xml(),
b'xmlns\n<>')
self.assertEqual(XHTML('<>', _a='1', _b='2', _xmlns='xmlns').xml(),
b'\n<>')
def test_HEAD(self):
self.assertEqual(HEAD('<>', _a='1', _b='2').xml(),
b'<>')
def test_TITLE(self):
self.assertEqual(TITLE('<>', _a='1', _b='2').xml(),
b'<> ')
def test_META(self):
self.assertEqual(META(_a='1', _b='2').xml(),
b' ')
def test_LINK(self):
self.assertEqual(LINK(_a='1', _b='2').xml(),
b' ')
def test_SCRIPT(self):
self.assertEqual(SCRIPT('<>', _a='1', _b='2').xml(),
b'''''')
self.assertEqual(SCRIPT('<>').xml(),
b'''''')
self.assertEqual(SCRIPT().xml(), b'')
self.assertEqual(SCRIPT(';').xml() + DIV().xml(),
b'
')
def test_STYLE(self):
self.assertEqual(STYLE('<>', _a='1', _b='2').xml(),
b'')
# Try to hit : return DIV.xml(self)
self.assertEqual(STYLE().xml(), b'')
def test_IMG(self):
self.assertEqual(IMG(_a='1', _b='2').xml(),
b' ')
def test_SPAN(self):
self.assertEqual(SPAN('<>', _a='1', _b='2').xml(),
b'<> ')
def test_BODY(self):
self.assertEqual(BODY('<>', _a='1', _b='2').xml(),
b'<>')
def test_H1(self):
self.assertEqual(H1('<>', _a='1', _b='2').xml(),
b'<> ')
def test_H2(self):
self.assertEqual(H2('<>', _a='1', _b='2').xml(),
b'<> ')
def test_H3(self):
self.assertEqual(H3('<>', _a='1', _b='2').xml(),
b'<> ')
def test_H4(self):
self.assertEqual(H4('<>', _a='1', _b='2').xml(),
b'<> ')
def test_H5(self):
self.assertEqual(H5('<>', _a='1', _b='2').xml(),
b'<> ')
def test_H6(self):
self.assertEqual(H6('<>', _a='1', _b='2').xml(),
b'<> ')
def test_P(self):
self.assertEqual(P('<>', _a='1', _b='2').xml(),
b'<>
')
# test cr2br
self.assertEqual(P('a\nb').xml(), b'a\nb
')
self.assertEqual(P('a\nb', cr2br=True).xml(), b'a b
')
def test_STRONG(self):
self.assertEqual(STRONG('<>', _a='1', _b='2').xml(),
b'<> ')
def test_B(self):
self.assertEqual(B('<>', _a='1', _b='2').xml(),
b'<> ')
def test_BR(self):
# empty BR()
self.assertEqual(BR().xml(), b' ')
self.assertEqual(BR(_a='1', _b='2').xml(), b' ')
def test_HR(self):
self.assertEqual(HR(_a='1', _b='2').xml(), b' ')
def test_A(self):
self.assertEqual(A('<>', _a='1', _b='2').xml(),
b'<> ')
self.assertEqual(A('a', cid='b').xml(),
b'a ')
self.assertEqual(A('a', callback='b', _id='c').xml(),
b'a ')
# Callback with no id trigger web2py_uuid() call
from gluon.html import web2pyHTMLParser
#a = A('a', callback='b').xml()
#for tag in web2pyHTMLParser(a).tree.elements('a'):
# uuid_generated = tag.attributes['_id']
#self.assertEqual(a,
# b'a '.format(id=uuid_generated))
self.assertEqual(A('a', delete='tr').xml(),
b'a ')
self.assertEqual(A('a', _id='b', target='').xml(),
b'a ')
self.assertEqual(A('a', component='b').xml(),
b'a ')
self.assertEqual(A('a', _id='b', callback='c', noconfirm=True).xml(),
b'a ')
self.assertEqual(A('a', cid='b').xml(),
b'a ')
self.assertEqual(A('a', cid='b', _disable_with='processing...').xml(),
b'a ')
self.assertEqual(A('a', callback='b', delete='tr', noconfirm=True, _id='c').xml(),
b'a ')
self.assertEqual(A('a', callback='b', delete='tr', confirm='Are you sure?', _id='c').xml(),
b'a ')
def test_BUTTON(self):
self.assertEqual(BUTTON('test', _type='button').xml(),
b'test ')
def test_EM(self):
self.assertEqual(EM('<>', _a='1', _b='2').xml(),
b'<> ')
def test_EMBED(self):
self.assertEqual(EMBED(_a='1', _b='2').xml(),
b' ')
def test_TT(self):
self.assertEqual(TT('<>', _a='1', _b='2').xml(),
b'<> ')
def test_PRE(self):
self.assertEqual(PRE('<>', _a='1', _b='2').xml(),
b'<> ')
def test_CENTER(self):
self.assertEqual(CENTER('<>', _a='1', _b='2').xml(),
b'<> ')
def test_CODE(self):
self.assertEqual(CODE("print 'hello world'",
language='python',
link=None,
counter=1,
styles={},
highlight_line=None).xml(),
'')
def test_LABEL(self):
self.assertEqual(LABEL('<>', _a='1', _b='2').xml(),
b'<> ')
def test_LI(self):
self.assertEqual(LI('<>', _a='1', _b='2').xml(),
b'<> ')
def test_UL(self):
self.assertEqual(UL('<>', _a='1', _b='2').xml(),
b'')
def test_OL(self):
self.assertEqual(OL('<>', _a='1', _b='2').xml(),
b'<> ')
def test_TD(self):
self.assertEqual(TD('<>', _a='1', _b='2').xml(),
b'<> ')
def test_TH(self):
self.assertEqual(TH('<>', _a='1', _b='2').xml(),
b'<> ')
def test_TR(self):
self.assertEqual(TR('<>', _a='1', _b='2').xml(),
b'<> ')
def test_THEAD(self):
self.assertEqual(THEAD('<>', _a='1', _b='2').xml(),
b'<> ')
# self.assertEqual(THEAD(TRHEAD('<>'), _a='1', _b='2').xml(),
# '<> ')
self.assertEqual(THEAD(TR('<>'), _a='1', _b='2').xml(),
b'<> ')
def test_TBODY(self):
self.assertEqual(TBODY('<>', _a='1', _b='2').xml(),
b'<> ')
def test_TFOOT(self):
self.assertEqual(TFOOT('<>', _a='1', _b='2').xml(),
b'<> ')
def test_COL(self):
# Empty COL test
self.assertEqual(COL().xml(), b' ')
# Not Empty COL test
self.assertEqual(COL(_span='2').xml(), b' ')
# Commented for now not so sure how to make it pass properly was passing locally
# I think this test is interesting and add value
# This fail relate to python 2.6 limitation I think
# Failing COL test
# with self.assertRaises(SyntaxError) as cm:
# COL('<>').xml()
# self.assertEqual(cm.exception[0], ' tags cannot have components')
# For now
self.assertRaises(SyntaxError, COL, b'<>')
def test_COLGROUP(self):
# Empty COLGROUP test
self.assertEqual(COLGROUP().xml(), b' ')
# Not Empty COLGROUP test
self.assertEqual(COLGROUP('<>', _a='1', _b='2').xml(), b'<> ')
def test_TABLE(self):
self.assertEqual(TABLE('<>', _a='1', _b='2').xml(),
b'')
def test_I(self):
self.assertEqual(I('<>', _a='1', _b='2').xml(),
b'<> ')
def test_IFRAME(self):
self.assertEqual(IFRAME('<>', _a='1', _b='2').xml(),
b'')
def test_INPUT(self):
self.assertEqual(INPUT(_a='1', _b='2').xml(), b' ')
# list value
self.assertEqual(INPUT(_value=[1, 2, 3]).xml(), b' ')
def test_TEXTAREA(self):
self.assertEqual(TEXTAREA('<>', _a='1', _b='2').xml(),
b'')
# override _rows and _cols
self.assertEqual(TEXTAREA('<>', _a='1', _b='2', _rows=5, _cols=20).xml(),
b'')
self.assertEqual(TEXTAREA('<>', value='bla bla bla...', _rows=10, _cols=40).xml(),
b'')
def test_OPTION(self):
self.assertEqual(OPTION('<>', _a='1', _b='2').xml(),
b'<>' +
b' ')
def test_OBJECT(self):
self.assertEqual(OBJECT('<>', _a='1', _b='2').xml(),
b'<> ')
def test_OPTGROUP(self):
# Empty OPTGROUP test
self.assertEqual(OPTGROUP().xml(),
b' ')
# Not Empty OPTGROUP test
self.assertEqual(OPTGROUP('<>', _a='1', _b='2').xml(),
b'<> ')
# With an OPTION
self.assertEqual(OPTGROUP(OPTION('Option 1', _value='1'), _label='Group 1').xml(),
b'Option 1 ')
def test_SELECT(self):
self.assertEqual(SELECT('<>', _a='1', _b='2').xml(),
b'' +
b'<> ')
self.assertEqual(SELECT(OPTION('option 1', _value='1'),
OPTION('option 2', _value='2')).xml(),
b'option 1 option 2 ')
self.assertEqual(SELECT(OPTION('option 1', _value='1', _selected='selected'),
OPTION('option 2', _value='2'),
_multiple='multiple').xml(),
b'option 1 option 2 ')
# More then one select with mutilple
self.assertEqual(SELECT(OPTION('option 1', _value='1', _selected='selected'),
OPTION('option 2', _value='2', _selected='selected'),
_multiple='multiple').xml(),
b'option 1 option 2 '
)
# OPTGROUP
self.assertEqual(SELECT(OPTGROUP(OPTION('option 1', _value='1'),
OPTION('option 2', _value='2'),
_label='Group 1',)).xml(),
b'option 1 option 2 ')
# List
self.assertEqual(SELECT([1, 2, 3, 4, 5]).xml(),
b'1 2 3 4 5 ')
# Tuple
self.assertEqual(SELECT((1, 2, 3, 4, 5)).xml(),
b'1 2 3 4 5 ')
# String value
self.assertEqual(SELECT('Option 1', 'Option 2').xml(),
b'Option 1 Option 2 ')
# list as a value
self.assertEqual(SELECT(OPTION('option 1', _value=[1, 2, 3]),
OPTION('option 2', _value=[4, 5, 6], _selected='selected'),
_multiple='multiple').xml(),
b'option 1 option 2 ')
def test_FIELDSET(self):
self.assertEqual(FIELDSET('<>', _a='1', _b='2').xml(),
b'<> ')
def test_LEGEND(self):
self.assertEqual(LEGEND('<>', _a='1', _b='2').xml(),
b'<> ')
def test_FORM(self):
self.assertEqual(FORM('<>', _a='1', _b='2').xml(),
b'')
# These 2 crash AppVeyor and Travis with: "ImportError: No YAML serializer available"
# self.assertEqual(FORM('<>', _a='1', _b='2').as_yaml(),
# "accepted: null\nattributes: {_a: '1', _action: '#', _b: '2', _enctype: multipart/form-data, _method: post}\ncomponents: [<>]\nerrors: {}\nlatest: {}\nparent: null\nvars: {}\n")
# TODO check tags content
self.assertEqual(len(FORM('<>', _a='1', _b='2').as_xml()), 334)
def test_BEAUTIFY(self):
#self.assertEqual(BEAUTIFY(['a', 'b', {'hello': 'world'}]).xml(),
# '')
# unicode
self.assertEqual(BEAUTIFY([P(u'àéèûôç'), 'a', 'b', {'hello': 'world'}]).xml(),
b'\xc3\xa0\xc3\xa9\xc3\xa8\xc3\xbb\xc3\xb4\xc3\xa7
a
b
')
def test_MENU(self):
self.assertEqual(MENU([('Home', False, '/welcome/default/index', [])]).xml(),
b'')
# Multiples entries menu
self.assertEqual(MENU([('Home', False, '/welcome/default/index', []),
('Item 1', False, '/welcome/default/func_one', []),
('Item 2', False, '/welcome/default/func_two', []),
('Item 3', False, '/welcome/default/func_three', []),
('Item 4', False, '/welcome/default/func_four', [])]).xml(),
b''
)
# mobile=True
self.assertEqual(MENU([('Home', False, '/welcome/default/index', [])], mobile=True).xml(),
b'')
# Multiples entries menu for mobile
self.assertEqual(MENU([('Home', False, '/welcome/default/index', []),
('Item 1', False, '/welcome/default/func_one', []),
('Item 2', False, '/welcome/default/func_two', []),
('Item 3', False, '/welcome/default/func_three', []),
('Item 4', False, '/welcome/default/func_four', [])], mobile=True).xml(),
b'')
# TODO: def test_embed64(self):
def test_web2pyHTMLParser(self):
#tag should not be a byte
self.assertEqual(web2pyHTMLParser("
").tree.components[0].tag, 'div')
a = str(web2pyHTMLParser('ab
c').tree)
self.assertEqual(a, "ab
c")
tree = web2pyHTMLParser('helloworld
').tree
tree.element(_a='b')['_c']=5
self.assertEqual(str(tree), 'helloworld
')
a = str(web2pyHTMLParser('', closed=['img']).tree)
self.assertEqual(a, '')
#greater-than sign ( > ) --> decimal > --> hexadecimal >
#Less-than sign ( < ) --> decimal < --> hexadecimal <
# test decimal
a = str(web2pyHTMLParser('< >
').tree)
self.assertEqual(a, '< >
')
# test hexadecimal
a = str(web2pyHTMLParser('< >
').tree)
self.assertEqual(a, '< >
')
def test_markdown(self):
def markdown(text, tag=None, attributes={}):
r = {None: re.sub('\s+',' ',text), \
'h1':'#'+text+'\\n\\n', \
'p':text+'\\n'}.get(tag,text)
return r
a=TAG('Header this is a test
')
ret = a.flatten(markdown)
self.assertEqual(ret, '#Header\\n\\nthis is a test\\n')
# TODO: def test_markdown_serializer(self):
# TODO: def test_markmin_serializer(self):
def test_MARKMIN(self):
# This test pass with python 2.7 but expected to fail under 2.6
# with self.assertRaises(TypeError) as cm:
# MARKMIN().xml()
# self.assertEqual(cm.exception[0], '__init__() takes at least 2 arguments (1 given)')
# For now
self.assertRaises(TypeError, MARKMIN)
self.assertEqual(MARKMIN('').xml(), b'')
self.assertEqual(MARKMIN('<>').xml(),
b'<>
')
self.assertEqual(MARKMIN("``hello_world = 'Hello World!'``:python").xml(),
b'hello_world = \'Hello World!\'
')
self.assertEqual(MARKMIN('<>').flatten(), b'<>')
def test_ASSIGNJS(self):
# empty assignation
self.assertEqual(ASSIGNJS().xml(), b'')
# text assignation
self.assertEqual(ASSIGNJS(var1='1').xml(), b'var var1 = "1";\n')
# int assignation
self.assertEqual(ASSIGNJS(var2=2).xml(), b'var var2 = 2;\n')
class TestData(unittest.TestCase):
def test_Adata(self):
self.assertEqual(A('<>', data=dict(abc='', cde='standard'), _a='1', _b='2').xml(),
b'<> ')