From f414356b6797a024e030cafd6e616089593be3b6 Mon Sep 17 00:00:00 2001 From: Hardirc Date: Fri, 25 Mar 2016 22:25:03 -0400 Subject: [PATCH] More coverage and some PEP8 enhencement to html.py --- gluon/html.py | 10 ++--- gluon/tests/test_html.py | 90 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/gluon/html.py b/gluon/html.py index ba14d7b2..83d53f7d 100644 --- a/gluon/html.py +++ b/gluon/html.py @@ -663,7 +663,7 @@ class XML(XmlComponent): """ to be considered experimental since the behavior of this method is questionable - another option could be `TAG(self.text).elements(*args,**kwargs)` + another option could be `TAG(self.text).elements(*args, **kwargs)` """ return [] @@ -760,7 +760,7 @@ class DIV(XmlComponent): Examples: >>> a=DIV() - >>> a.insert(0,SPAN('x')) + >>> a.insert(0, SPAN('x')) >>> print a
x
""" @@ -856,7 +856,7 @@ class DIV(XmlComponent): """ components = [] for c in self.components: - if isinstance(c, (allowed_parents,CAT)): + if isinstance(c, (allowed_parents, CAT)): pass elif wrap_lambda: c = wrap_lambda(c) @@ -1027,7 +1027,7 @@ class DIV(XmlComponent): Examples: >>> a = DIV(DIV(SPAN('x'),3,DIV(SPAN('y')))) - >>> for c in a.elements('span',first_only=True): c[0]='z' + >>> for c in a.elements('span', first_only=True): c[0]='z' >>> print a
z3
y
>>> for c in a.elements('span'): c[0]='z' @@ -1059,7 +1059,7 @@ class DIV(XmlComponent): >>> a = DIV(DIV(SPAN('x', _class='abc'), DIV(SPAN('y', _class='abc'), SPAN('z', _class='abc')))) >>> b = a.elements('span.abc', replace=P('x', _class='xyz')) - >>> print a + >>> print a # We should .xml() here instead of print

x

x

x

"replace" can be a callable, which will be passed the original element and diff --git a/gluon/tests/test_html.py b/gluon/tests/test_html.py index da1b604d..5c233bc5 100644 --- a/gluon/tests/test_html.py +++ b/gluon/tests/test_html.py @@ -14,11 +14,14 @@ from html import * from html import verifyURL from html import truncate_string from storage import Storage +from html import XML_pickle, XML_unpickle +from html import TAG_pickler, TAG_unpickler class TestBareHelpers(unittest.TestCase): - # TODO: def test_xmlescape(self): + # xmlescape() = covered by other tests + # TODO: def test_call_as_list(self): def test_truncate_string(self): @@ -26,6 +29,8 @@ class TestBareHelpers(unittest.TestCase): 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...') @@ -169,9 +174,9 @@ class TestBareHelpers(unittest.TestCase): self.assertEqual(XML('

Test


Test


', sanitize=True), XML('

Test


Test


')) - # TODO: def test_XML_unpickle(self): - - # TODO: def test_XML_pickle(self): + def test_XML_pickle_unpickle(self): + # weird test + self.assertEqual(XML_unpickle(XML_pickle('data to be pickle')[1][0]), 'data to be pickle') def test_DIV(self): # Empty DIV() @@ -199,7 +204,31 @@ class TestBareHelpers(unittest.TestCase): self.assertEqual(a.xml(), '
a
b
') self.assertEqual([el.xml() for el in s.siblings()], ['
b
']) self.assertEqual(s.sibling().xml(), '
b
') + # siblings with wrong args self.assertEqual(s.siblings('a'), []) + # siblings with good args + self.assertEqual(s.siblings('div')[0].xml(), '
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(), + '
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(), + '
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 @@ -215,9 +244,10 @@ class TestBareHelpers(unittest.TestCase): # CAT(' ') self.assertEqual(CAT(' ').xml(), ' ') - # TODO: def test_TAG_unpickler(self): - - # TODO: def test_TAG_pickler(self): + def test_TAG_pickler_unpickler(self): + # weird test + self.assertEqual(TAG_unpickler(TAG_pickler(TAG.div('data to be pickle'))[1][0]).xml(), + '
data to be pickle
') def test_TAG(self): self.assertEqual(TAG.first(TAG.second('test'), _key=3).xml(), @@ -225,6 +255,9 @@ class TestBareHelpers(unittest.TestCase): # ending in underscore "triggers" style self.assertEqual(TAG.first_(TAG.second('test'), _key=3).xml(), '') + # unicode test for TAG + self.assertEqual(TAG.div(u'Texte en français avec des caractères accentués...').xml(), + '
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(), @@ -259,6 +292,8 @@ class TestBareHelpers(unittest.TestCase): '\n<>') self.assertEqual(XHTML('<>', _a='1', _b='2', doctype='xmlns').xml(), 'xmlns\n<>') + self.assertEqual(XHTML('<>', _a='1', _b='2', _xmlns='xmlns').xml(), + '\n<>') def test_HEAD(self): self.assertEqual(HEAD('<>', _a='1', _b='2').xml(), @@ -290,6 +325,8 @@ class TestBareHelpers(unittest.TestCase): def test_STYLE(self): self.assertEqual(STYLE('<>', _a='1', _b='2').xml(), '') + # Try to hit : return DIV.xml(self) + self.assertEqual(STYLE().xml(), '') def test_IMG(self): self.assertEqual(IMG(_a='1', _b='2').xml(), @@ -515,8 +552,9 @@ class TestBareHelpers(unittest.TestCase): '') def test_INPUT(self): - self.assertEqual(INPUT(_a='1', _b='2').xml(), - '') + self.assertEqual(INPUT(_a='1', _b='2').xml(), '') + # list value + self.assertEqual(INPUT(_value=[1, 2, 3]).xml(), '') def test_TEXTAREA(self): self.assertEqual(TEXTAREA('<>', _a='1', _b='2').xml(), @@ -560,6 +598,12 @@ class TestBareHelpers(unittest.TestCase): OPTION('option 2', _value='2'), _multiple='multiple').xml(), '') + # 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(), + '' + ) # OPTGROUP self.assertEqual(SELECT(OPTGROUP(OPTION('option 1', _value='1'), OPTION('option 2', _value='2'), @@ -574,6 +618,11 @@ class TestBareHelpers(unittest.TestCase): # String value self.assertEqual(SELECT('Option 1', 'Option 2').xml(), '') + # 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(), + '') def test_FIELDSET(self): self.assertEqual(FIELDSET('<>', _a='1', _b='2').xml(), @@ -586,17 +635,40 @@ class TestBareHelpers(unittest.TestCase): def test_FORM(self): self.assertEqual(FORM('<>', _a='1', _b='2').xml(), '
<>
') + # 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") + # self.assertEqual(FORM('<>', _a='1', _b='2').as_xml(), + # 'None<_enctype>multipart/form-data<_action>#<_b>2<_a>1<_method>post&lt;&gt;None') def test_BEAUTIFY(self): self.assertEqual(BEAUTIFY(['a', 'b', {'hello': 'world'}]).xml(), '
a
b
hello:
world
') + # unicode + self.assertEqual(BEAUTIFY([P(u'àéèûôç'), 'a', 'b', {'hello': 'world'}]).xml(), + '

\xc3\xa0\xc3\xa9\xc3\xa8\xc3\xbb\xc3\xb4\xc3\xa7

a
b
hello:
world
') def test_MENU(self): self.assertEqual(MENU([('Home', False, '/welcome/default/index', [])]).xml(), '') + # 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(), + '' + ) # mobile=True self.assertEqual(MENU([('Home', False, '/welcome/default/index', [])], mobile=True).xml(), '') + # 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(), + '') # TODO: def test_embed64(self):