2020-04-30 03:01:37 +02:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- coding: utf-8 -*-
|
2020-04-30 18:32:47 +02:00
|
|
|
# __coconut_hash__ = 0x48175ba9
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
# Compiled with Coconut version 1.4.3 [Ernest Scribbler]
|
|
|
|
|
|
|
|
""" XML Helpers tests """
|
|
|
|
|
|
|
|
# Coconut Header: -------------------------------------------------------------
|
|
|
|
|
|
|
|
from __future__ import print_function, absolute_import, unicode_literals, division
|
|
|
|
import sys as _coconut_sys, os.path as _coconut_os_path
|
|
|
|
_coconut_file_path = _coconut_os_path.dirname(_coconut_os_path.abspath(__file__))
|
|
|
|
_coconut_cached_module = _coconut_sys.modules.get(b"__coconut__")
|
|
|
|
if _coconut_cached_module is not None and _coconut_os_path.dirname(_coconut_cached_module.__file__) != _coconut_file_path:
|
|
|
|
del _coconut_sys.modules[b"__coconut__"]
|
|
|
|
_coconut_sys.path.insert(0, _coconut_file_path)
|
|
|
|
from __coconut__ import *
|
|
|
|
from __coconut__ import _coconut, _coconut_MatchError, _coconut_igetitem, _coconut_base_compose, _coconut_forward_compose, _coconut_back_compose, _coconut_forward_star_compose, _coconut_back_star_compose, _coconut_forward_dubstar_compose, _coconut_back_dubstar_compose, _coconut_pipe, _coconut_back_pipe, _coconut_star_pipe, _coconut_back_star_pipe, _coconut_dubstar_pipe, _coconut_back_dubstar_pipe, _coconut_bool_and, _coconut_bool_or, _coconut_none_coalesce, _coconut_minus, _coconut_map, _coconut_partial, _coconut_get_function_match_error, _coconut_base_pattern_func, _coconut_addpattern, _coconut_sentinel, _coconut_assert, _coconut_mark_as_match
|
|
|
|
|
|
|
|
|
|
|
|
# Compiled Coconut: -----------------------------------------------------------
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright 2019-2020 Fabien Bourgeois <fabien@yaltik.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import xml.etree.ElementTree as ET
|
2020-04-30 18:32:47 +02:00
|
|
|
from os import unlink
|
2020-04-30 03:01:37 +02:00
|
|
|
from xml_base import xmln
|
|
|
|
from xml_base import xmlroot
|
|
|
|
from xml_base import xmlchild
|
2020-04-30 18:32:47 +02:00
|
|
|
from xml_base import xml_write
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestXMLBase(unittest.TestCase):
|
|
|
|
""" XML Helpers tests """
|
|
|
|
|
|
|
|
def test_xmln(self):
|
|
|
|
# Tags
|
2020-04-30 10:58:36 +02:00
|
|
|
(self.assertEquals)(*(xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []}))
|
|
|
|
(self.assertEquals)(*((_coconut_partial(getattr, {1: 'tag'}, 2))((xmln)('a tag')), 'a tag'))
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
# Attrs
|
2020-04-30 10:58:36 +02:00
|
|
|
(self.assertEquals)(*(xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'}))
|
|
|
|
(self.assertEquals)(*((_coconut_partial(getattr, {1: 'attrs'}, 2))((xmln)(**{'attrs': {'a good': 'one'}})), {'a good': 'one'}))
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
# Childrens
|
|
|
|
attrs = {'children': [1, 2, 3]}
|
2020-04-30 10:58:36 +02:00
|
|
|
(self.assertTrue)(((_coconut_partial(getattr, {1: 'children'}, 2))((xmln)(**attrs)) == [1, 2, 3]))
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
attrs = {'children': 'Some text'}
|
2020-04-30 10:58:36 +02:00
|
|
|
(self.assertTrue)(((_coconut_partial(getattr, {1: 'children'}, 2))((xmln)(**attrs)) == ['Some text']))
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
(xmln)(**{'children': False})
|
|
|
|
except TypeError as err:
|
|
|
|
(self.assertIn)(*('Invalid arguments', err.message))
|
|
|
|
|
|
|
|
|
|
|
|
def test_xmlchild(self):
|
|
|
|
parent = (xmlroot)({'tag': 'root', 'attrs': {}, 'children': []})
|
|
|
|
xmlc_par = (_coconut.functools.partial(_coconut.functools.partial, xmlchild))(parent)
|
|
|
|
|
2020-04-30 10:58:36 +02:00
|
|
|
# Bad arguments
|
2020-04-30 03:01:37 +02:00
|
|
|
try:
|
|
|
|
(xmlc_par)(False)
|
|
|
|
except TypeError as err:
|
2020-04-30 10:58:36 +02:00
|
|
|
(self.assertIn)('Invalid arguments for xmlchild', err.message)
|
|
|
|
try:
|
|
|
|
(xmlc_par)([])
|
|
|
|
except TypeError as err:
|
|
|
|
(self.assertIn)('Invalid arguments for xmlchild', err.message)
|
|
|
|
try: # Need XMLDictElement, not dict
|
|
|
|
(xmlc_par)([{'tag': 't', 'attrs': {'a': 'b'}, 'children': []}])
|
|
|
|
except TypeError as err:
|
|
|
|
(self.assertIn)('Invalid arguments for xmlchild', err.message)
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
(xmlc_par)(['some text'])
|
|
|
|
(self.assertEquals)(parent.text, 'some text')
|
|
|
|
|
2020-04-30 10:58:36 +02:00
|
|
|
(xmlc_par)([(xmln)(**{'tag': 't', 'attrs': {'a': 'b'}, 'children': []})])
|
2020-04-30 03:01:37 +02:00
|
|
|
child = (next)((parent.iter)('t'))
|
|
|
|
(self.assertEquals)(child.tag, 't')
|
|
|
|
(self.assertEquals)(child.attrib, {'a': 'b'})
|
|
|
|
(self.assertEquals)(((list)(child)), [])
|
|
|
|
|
2020-04-30 10:58:36 +02:00
|
|
|
(xmlc_par)([(xmln)(**{'tag': 't2', 'attrs': {1: 2}, 'children': []})])
|
2020-04-30 03:01:37 +02:00
|
|
|
child = (next)((parent.iter)('t2'))
|
|
|
|
(self.assertEquals)(child.attrib, {'1': '2'})
|
|
|
|
|
2020-04-30 10:58:36 +02:00
|
|
|
(xmlc_par)([(xmln)(**{'tag': 'tchildren', 'attrs': {}, 'children': [(xmln)(**{'tag': 'subchild', 'attrs': {}, 'children': []})]})])
|
2020-04-30 03:01:37 +02:00
|
|
|
child = (next)((parent.iter)('tchildren'))
|
|
|
|
subchildren = ((list)(child))
|
|
|
|
(self.assertEquals)(((len)(subchildren)), 1)
|
|
|
|
(self.assertEquals)(subchildren[0].tag, 'subchild')
|
|
|
|
|
|
|
|
|
|
|
|
def test_xmlroot(self):
|
|
|
|
root = (xmlroot)({'tag': 'root', 'attrs': {}, 'children': []})
|
|
|
|
(self.assertTrue)((isinstance)(*(root, ET.Element)))
|
|
|
|
|
|
|
|
try:
|
|
|
|
(xmlroot)(False)
|
|
|
|
except TypeError as err:
|
|
|
|
(self.assertIn)(*('has no attribute', err.message))
|
|
|
|
try:
|
|
|
|
(xmlroot)({})
|
|
|
|
except KeyError as err:
|
|
|
|
(self.assertIn)(*('tag', err.message))
|
|
|
|
try:
|
|
|
|
(xmlroot)({'tag': 'root'})
|
|
|
|
except KeyError as err:
|
|
|
|
(self.assertIn)(*('attrs', err.message))
|
|
|
|
try:
|
|
|
|
(xmlroot)({'tag': 'root', 'attrs': {}})
|
|
|
|
except KeyError as err:
|
|
|
|
(self.assertIn)(*('children', err.message))
|
|
|
|
|
|
|
|
|
|
|
|
def test_xml_write(self):
|
2020-04-30 18:32:47 +02:00
|
|
|
children = [(xmln)(*('child1', {'attr': 'value'}, [])), (xmln)(*('child2', {}, "Some text"))]
|
|
|
|
tree = xmlroot({'tag': 'root', 'attrs': {}, 'children': children})
|
|
|
|
xmlw = _coconut_partial(xml_write, {1: tree}, 2)
|
|
|
|
|
|
|
|
(self.assertEquals)(((xmlw)('/badpath')), None)
|
|
|
|
(self.assertEquals)(((xmlw)('/bad.ext')), None)
|
|
|
|
|
|
|
|
xmlw(__file__)
|
|
|
|
filepath = __file__.replace('.py', '_views.xml')
|
|
|
|
with open(filepath, 'r') as output_file:
|
|
|
|
output_xml = output_file.read()
|
|
|
|
(self.assertIn)('<?xml version', output_xml)
|
|
|
|
(self.assertIn)('<root>', output_xml)
|
|
|
|
(self.assertIn)('<child1 attr="value"/>', output_xml)
|
|
|
|
(self.assertIn)('<child2>Some text</child2>', output_xml)
|
|
|
|
unlink(filepath)
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|