2
# Copyright (c) 2008 Canonical
4
# Written by Marc Tardif <marc@interunion.ca>
6
# This file is part of Checkbox.
8
# Checkbox is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# Checkbox is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
23
from xml.dom.minidom import Document, Node
25
from checkbox.report import ReportManager, Report
28
class StubReport(Report):
30
def register_dumps(self):
31
self._manager.handle_dumps(str, self.dumps_test)
33
def register_loads(self):
34
self._manager.handle_loads("test", self.loads_test)
35
self._manager.handle_loads("default", self.loads_default)
37
def dumps_test(self, obj, parent):
38
text_node = self._manager.document.createTextNode(obj)
39
parent.appendChild(text_node)
41
def loads_test(self, node):
42
return node.childNodes[0].nodeValue.strip()
44
def loads_default(self, node):
46
for child in (c for c in node.childNodes if c.nodeType != Node.TEXT_NODE):
47
if child.hasAttribute("name"):
48
name = child.getAttribute("name")
50
name = child.localName
51
default[str(name)] = self._manager.call_loads(child)
55
class ReportManagerTest(unittest.TestCase):
59
def create_element(self, name):
61
element = document.createElement(name)
62
document.appendChild(element)
65
def create_text_node(self, name, text):
66
element = self.create_element(name)
67
text_node = element.createTextNode(text)
68
element.appendChild(text_node)
71
def test_constructor(self):
72
rm = ReportManager(self.name)
73
self.assertTrue(rm.name == self.name)
75
def test_handle_dumps(self):
76
rm = ReportManager(self.name)
78
str_func = lambda obj, parent: "str"
79
rm.handle_dumps(str, str_func)
80
self.assertTrue(rm.call_dumps("test1", None) == "str")
82
int_func = lambda obj, parent: "int"
83
rm.handle_dumps(int, int_func)
84
self.assertTrue(rm.call_dumps(1, None) == "int")
85
self.assertTrue(rm.call_dumps("test2", None) == "str")
87
def test_handle_loads(self):
88
rm = ReportManager(self.name)
90
bar_func = lambda node: "bar"
91
foo_element = self.create_element("foo")
92
rm.handle_loads("foo", bar_func)
93
self.assertTrue(rm.call_loads(foo_element) == "bar")
95
baz_func = lambda node: "baz"
96
bar_element = self.create_element("bar")
97
rm.handle_loads("bar", baz_func)
98
self.assertTrue(rm.call_loads(bar_element) == "baz")
99
self.assertTrue(rm.call_loads(foo_element) == "bar")
101
def test_handle_dumps_and_loads(self):
102
rm = ReportManager(self.name)
105
document = rm.dumps("test1")
106
str = document.toprettyxml("")
107
self.assertTrue("test1" in str)
110
self.assertTrue(data.has_key("test"))
111
self.assertTrue(data["test"] == "test1")