Passed
Pull Request — master (#225)
by Juan José
01:21
created

tests.test_xml   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 118
Duplicated Lines 32.2 %

Importance

Changes 0
Metric Value
eloc 62
dl 38
loc 118
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A EscapeText.test_escape_xml_printable_char() 20 20 1
A ElementsAsText.test_simple_elements() 0 7 1
A EscapeText.test_escape_xml_invalid_char() 18 18 1
A EscapeText.test_escape_xml_valid_text() 0 5 1
A ElementsAsText.test_elements() 0 20 1
A ElementsAsText.test_simple_element() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# Copyright (C) 2020 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
from collections import OrderedDict
20
21
from unittest import TestCase
22
23
from ospd.xml import elements_as_text, escape_ctrl_chars
24
25
from xml.etree.ElementTree import Element, tostring, fromstring
26
27
28
class ElementsAsText(TestCase):
29
    def test_simple_element(self):
30
        elements = {'foo': 'bar'}
31
        text = elements_as_text(elements)
32
33
        self.assertEqual(text, '\t  foo                    bar\n')
34
35
    def test_simple_elements(self):
36
        elements = OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')])
37
        text = elements_as_text(elements)
38
39
        self.assertEqual(
40
            text,
41
            '\t  foo                    bar\n'
42
            '\t  lorem                  ipsum\n',
43
        )
44
45
    def test_elements(self):
46
        elements = OrderedDict(
47
            [
48
                ('foo', 'bar'),
49
                (
50
                    'lorem',
51
                    OrderedDict(
52
                        [
53
                            ('dolor', 'sit amet'),
54
                            ('consectetur', 'adipiscing elit'),
55
                        ]
56
                    ),
57
                ),
58
            ]
59
        )
60
        text = elements_as_text(elements)
61
62
        self.assertEqual(
63
            text,
64
            '\t  foo                    bar\n'
65
            '\t  lorem                  \n'
66
            '\t    dolor                  sit amet\n'
67
            '\t    consectetur            adipiscing elit\n',
68
        )
69
70
71
class EscapeText(TestCase):
72
    def test_escape_xml_valid_text(self):
73
        text = 'this is a valid xml'
74
        res = escape_ctrl_chars(text)
75
76
        self.assertEqual(text, res)
77
78 View Code Duplication
    def test_escape_xml_invalid_char(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
79
        text = 'End of transmission is not printable \x04.'
80
        res = escape_ctrl_chars(text)
81
        self.assertEqual(res, 'End of transmission is not printable \\x0004.')
82
83
        # Create element
84
        elem = Element('text')
85
        elem.text = res
86
        self.assertEqual(
87
            tostring(elem),
88
            b'<text>End of transmission is not printable \\x0004.</text>',
89
        )
90
91
        # The string format of the element does not break the xml.
92
        elem_as_str = tostring(elem, encoding='utf-8')
93
        new_elem = fromstring(elem_as_str)
94
        self.assertEqual(
95
            b'<text>' + new_elem.text.encode('utf-8') + b'</text>', elem_as_str
96
        )
97
98 View Code Duplication
    def test_escape_xml_printable_char(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
99
        text = 'Latin Capital Letter A With Circumflex \xc2 is printable.'
100
        res = escape_ctrl_chars(text)
101
        self.assertEqual(
102
            res, 'Latin Capital Letter A With Circumflex  is printable.'
103
        )
104
105
        # Create the element
106
        elem = Element('text')
107
        elem.text = res
108
        self.assertEqual(
109
            tostring(elem),
110
            b'<text>Latin Capital Letter A With Circumflex &#194; is printable.</text>',
111
        )
112
113
        # The string format of the element does not break the xml
114
        elem_as_str = tostring(elem, encoding='utf-8')
115
        new_elem = fromstring(elem_as_str)
116
        self.assertEqual(
117
            b'<text>' + new_elem.text.encode('utf-8') + b'</text>', elem_as_str
118
        )
119