Passed
Push — master ( bd8417...61b038 )
by
unknown
03:43 queued 01:23
created

XmlCommandTestCase.test_xml_escaping()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 1
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
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 gvm.xml import XmlCommand
22
23
24
class XmlCommandTestCase(unittest.TestCase):
25
26
    def test_should_create_command(self):
27
        cmd = XmlCommand('foo')
28
29
        self.assertEqual(cmd.to_string(), '<foo/>')
30
31
    def test_should_allow_to_add_element(self):
32
        cmd = XmlCommand('foo')
33
        cmd.add_element('bar')
34
35
        self.assertEqual(cmd.to_string(), '<foo><bar/></foo>')
36
37
    def test_should_allow_to_add_element_with_text(self):
38
        cmd = XmlCommand('foo')
39
        cmd.add_element('bar', '1')
40
41
        self.assertEqual(cmd.to_string(), '<foo><bar>1</bar></foo>')
42
43
    def test_should_allow_to_set_attribute(self):
44
        cmd = XmlCommand('foo')
45
        cmd.set_attribute('bar', '1')
46
47
        self.assertEqual(cmd.to_string(), '<foo bar="1"/>')
48
49
    def test_should_allow_to_set_attributes(self):
50
        cmd = XmlCommand('foo')
51
        cmd.set_attributes({'bar': '1', 'baz': '2'})
52
53
        self.assertEqual(cmd.to_string(), '<foo bar="1" baz="2"/>')
54
55
    def test_should_convert_to_string(self):
56
        cmd = XmlCommand('foo')
57
58
        self.assertEqual(str(cmd), '<foo/>')
59
60
    def test_invalid_xml(self):
61
        with self.assertRaises(ValueError):
62
            XmlCommand('Foo & Bar')
63
64
    def test_xml_escaping(self):
65
        cmd = XmlCommand('foo')
66
        cmd.add_element('bar', 'Foo & Bar')
67
68
        self.assertEqual(cmd.to_string(), '<foo><bar>Foo &amp; Bar</bar></foo>')
69
70
        cmd = XmlCommand('foo')
71
        cmd.set_attribute('bar', 'Foo & Bar')
72
73
        self.assertEqual(cmd.to_string(), '<foo bar="Foo &amp; Bar"/>')
74
75
        cmd = XmlCommand('foo')
76
        cmd.set_attribute('bar', 'Foo "Bar"')
77
78
        self.assertEqual(cmd.to_string(), '<foo bar="Foo &quot;Bar&quot;"/>')
79
80
81
if __name__ == '__main__':
82
    unittest.main()
83