Completed
Push — master ( 6dd72a...5005a3 )
by Björn
14s queued 12s
created

tests.xml.test_xml_command   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 42
dl 0
loc 84
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A XmlCommandTestCase.test_xml_escaping() 0 15 1
A XmlCommandTestCase.test_should_convert_to_string() 0 4 1
A XmlCommandTestCase.test_invalid_xml() 0 3 2
A XmlCommandTestCase.test_should_allow_to_add_element() 0 5 1
A XmlCommandTestCase.test_should_allow_to_add_element_with_text() 0 5 1
A XmlCommandTestCase.test_should_allow_to_set_attributes() 0 5 1
A XmlCommandTestCase.test_should_allow_to_set_attribute() 0 5 1
A XmlCommandTestCase.test_should_create_command() 0 4 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
21
from collections import OrderedDict
22
23
from gvm.xml import XmlCommand
24
25
26
class XmlCommandTestCase(unittest.TestCase):
27
    def test_should_create_command(self):
28
        cmd = XmlCommand('foo')
29
30
        self.assertEqual(cmd.to_string(), '<foo/>')
31
32
    def test_should_allow_to_add_element(self):
33
        cmd = XmlCommand('foo')
34
        cmd.add_element('bar')
35
36
        self.assertEqual(cmd.to_string(), '<foo><bar/></foo>')
37
38
    def test_should_allow_to_add_element_with_text(self):
39
        cmd = XmlCommand('foo')
40
        cmd.add_element('bar', '1')
41
42
        self.assertEqual(cmd.to_string(), '<foo><bar>1</bar></foo>')
43
44
    def test_should_allow_to_set_attribute(self):
45
        cmd = XmlCommand('foo')
46
        cmd.set_attribute('bar', '1')
47
48
        self.assertEqual(cmd.to_string(), '<foo bar="1"/>')
49
50
    def test_should_allow_to_set_attributes(self):
51
        cmd = XmlCommand('foo')
52
        cmd.set_attributes(OrderedDict([('bar', '1'), ('baz', '2')]))
53
54
        self.assertEqual(cmd.to_string(), '<foo bar="1" baz="2"/>')
55
56
    def test_should_convert_to_string(self):
57
        cmd = XmlCommand('foo')
58
59
        self.assertEqual(str(cmd), '<foo/>')
60
61
    def test_invalid_xml(self):
62
        with self.assertRaises(ValueError):
63
            XmlCommand('Foo & Bar')
64
65
    def test_xml_escaping(self):
66
        cmd = XmlCommand('foo')
67
        cmd.add_element('bar', 'Foo & Bar')
68
69
        self.assertEqual(cmd.to_string(), '<foo><bar>Foo &amp; Bar</bar></foo>')
70
71
        cmd = XmlCommand('foo')
72
        cmd.set_attribute('bar', 'Foo & Bar')
73
74
        self.assertEqual(cmd.to_string(), '<foo bar="Foo &amp; Bar"/>')
75
76
        cmd = XmlCommand('foo')
77
        cmd.set_attribute('bar', 'Foo "Bar"')
78
79
        self.assertEqual(cmd.to_string(), '<foo bar="Foo &quot;Bar&quot;"/>')
80
81
82
if __name__ == '__main__':
83
    unittest.main()
84