0
|
1 |
# *-* coding: utf-8 *-*
|
|
2 |
#
|
|
3 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
# All rights reserved.
|
|
5 |
# This component and the accompanying materials are made available
|
|
6 |
# under the terms of "Eclipse Public License v1.0"
|
|
7 |
# which accompanies this distribution, and is available
|
|
8 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
9 |
#
|
|
10 |
# Initial Contributors:
|
|
11 |
# Nokia Corporation - initial contribution.
|
|
12 |
#
|
|
13 |
# Contributors:
|
|
14 |
#
|
|
15 |
# Description:
|
|
16 |
#
|
|
17 |
|
|
18 |
import sys, os
|
|
19 |
import unittest
|
|
20 |
import StringIO
|
|
21 |
import __init__
|
|
22 |
|
|
23 |
from cone.public import utils, exceptions
|
|
24 |
|
|
25 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
26 |
|
|
27 |
class ElementTreeBackendTester(object):
|
|
28 |
"""
|
|
29 |
Base tester class that contains all test cases.
|
|
30 |
|
|
31 |
The actual test case classes derive from this and set the
|
|
32 |
class attributes BACKEND_ID and LINE_NUMBERS.
|
|
33 |
"""
|
|
34 |
|
|
35 |
DATA = u"""<?xml version="1.0" encoding="UTF-8"?>
|
|
36 |
<root xmlns="http://www.test.com/xml/1">
|
|
37 |
<!-- Comment -->
|
|
38 |
<elem1 attr1="test" attr2="test 2"/>
|
|
39 |
<elem2>カタカナ</elem2>
|
|
40 |
<elem3>
|
|
41 |
<!-- Comment 2 -->
|
|
42 |
some text
|
|
43 |
</elem3>
|
|
44 |
</root>""".encode('utf-8')
|
|
45 |
|
|
46 |
# ID of the ElementTree back-end to use in the tests
|
|
47 |
BACKEND_ID = None
|
|
48 |
|
|
49 |
# Whether to check if line numbers in elements are correct or None
|
|
50 |
LINE_NUMBERS = False
|
|
51 |
|
|
52 |
def setUp(self):
|
|
53 |
self.orig_backend_id = utils.etree.get_backend_id()
|
|
54 |
utils.etree.set_backend_id(self.BACKEND_ID)
|
|
55 |
|
|
56 |
def tearDown(self):
|
|
57 |
utils.etree.set_backend_id(self.orig_backend_id)
|
|
58 |
|
|
59 |
def assert_lineno_equals(self, actual, expected):
|
|
60 |
if self.LINE_NUMBERS:
|
|
61 |
self.assertEquals(actual, expected)
|
|
62 |
else:
|
|
63 |
self.assertEquals(actual, None)
|
|
64 |
|
|
65 |
def test_correct_parser_set(self):
|
|
66 |
self.assertEquals(utils.etree.get_backend_id(), self.BACKEND_ID)
|
|
67 |
|
|
68 |
def test_fromstring_successful(self):
|
|
69 |
root = utils.etree.fromstring(self.DATA)
|
|
70 |
self.assertEquals(root.tag, '{http://www.test.com/xml/1}root')
|
|
71 |
children = [e for e in root]
|
|
72 |
self.assertEquals(len(children), 3)
|
|
73 |
self.assertEquals(children[0].tag, '{http://www.test.com/xml/1}elem1')
|
|
74 |
self.assertEquals(children[1].tag, '{http://www.test.com/xml/1}elem2')
|
|
75 |
self.assertEquals(children[2].tag, '{http://www.test.com/xml/1}elem3')
|
|
76 |
self.assertEquals(children[1].text, u'カタカナ')
|
|
77 |
|
|
78 |
self.assert_lineno_equals(utils.etree.get_lineno(root), 2)
|
|
79 |
self.assert_lineno_equals(utils.etree.get_lineno(children[0]), 4)
|
|
80 |
self.assert_lineno_equals(utils.etree.get_lineno(children[1]), 5)
|
|
81 |
self.assert_lineno_equals(utils.etree.get_lineno(children[2]), 6)
|
|
82 |
|
|
83 |
def test_tostring_ascii(self):
|
|
84 |
root = utils.etree.fromstring(self.DATA)
|
|
85 |
output = utils.etree.tostring(root)
|
|
86 |
|
|
87 |
def test_tostring_utf_8(self):
|
|
88 |
root = utils.etree.fromstring(self.DATA)
|
|
89 |
output = utils.etree.tostring(root, 'UTF-8')
|
|
90 |
|
|
91 |
def test_tostring_utf_16(self):
|
|
92 |
root = utils.etree.fromstring(self.DATA)
|
|
93 |
output = utils.etree.tostring(root, 'UTF-16')
|
|
94 |
|
|
95 |
def test_fromstring_failed(self):
|
|
96 |
data = """<?xml version="1.0" encoding="UTF-8"?>
|
|
97 |
<root xmlns="http://www.test.com/xml/1">
|
|
98 |
<elem1 attr1="test" attr2="test 2"/>
|
|
99 |
<elem2>testing</elem3>
|
|
100 |
</root>"""
|
|
101 |
try:
|
|
102 |
etree = utils.etree.fromstring(data)
|
|
103 |
self.fail("XmlParseError not raised!")
|
|
104 |
except exceptions.XmlParseError, e:
|
|
105 |
self.assertEquals(e.lineno, 4)
|
|
106 |
|
|
107 |
# ============================================================================
|
|
108 |
# Actual test cases
|
|
109 |
# ============================================================================
|
|
110 |
|
|
111 |
# NOTE:
|
|
112 |
# The test classes MUST inherit the two super-classes in the order
|
|
113 |
# (ElementTreeBackendTester, unittest.TestCase), or otherwise setUp() and
|
|
114 |
# tearDown() will not be overridden correctly
|
|
115 |
|
|
116 |
class TestElementTreeBackend(ElementTreeBackendTester, unittest.TestCase):
|
|
117 |
BACKEND_ID = utils.etree.BACKEND_ELEMENT_TREE
|
|
118 |
LINE_NUMBERS = True
|
|
119 |
|
|
120 |
class TestCElementTreeBackend(ElementTreeBackendTester, unittest.TestCase):
|
|
121 |
BACKEND_ID = utils.etree.BACKEND_C_ELEMENT_TREE
|
|
122 |
LINE_NUMBERS = False
|
|
123 |
|
|
124 |
class TestLxmlBackend(ElementTreeBackendTester, unittest.TestCase):
|
|
125 |
BACKEND_ID = utils.etree.BACKEND_LXML
|
|
126 |
LINE_NUMBERS = True
|
|
127 |
|
|
128 |
|
|
129 |
if __name__ == '__main__':
|
|
130 |
unittest.main()
|