1 #Copyright (C) 2008 Codethink Ltd
3 #his program is free software; you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation; either version 2 of the License, or
6 #(at your option) any later version.
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 #GNU General Public License for more details.
12 #You should have received a copy of the GNU General Public License
13 #along with this program; if not, write to the Free Software
14 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 from Events import Events
21 PASY_TEST_NOT_STARTED = 0
22 PASY_TEST_IN_PROGRESS = 1
26 class PasyEvents(Events):
27 __events__ = ('finished', )
29 class PasyTestStep(object):
31 def __init__(self, name):
32 self.events = PasyEvents()
33 self._state = PASY_TEST_NOT_STARTED
38 if self._state == PASY_TEST_IN_PROGRESS:
39 self._state = PASY_TEST_WIN
40 self.events.finished()
43 if self._state == PASY_TEST_IN_PROGRESS:
44 self._state = PASY_TEST_FAIL
46 self.events.finished()
48 def assertEqual(self, a, b, msg):
52 def assertNotEqual(self, a, b, msg):
57 self._state = PASY_TEST_IN_PROGRESS
61 if self._state == PASY_TEST_WIN:
62 return "%s - PASSED" % (self._name,)
63 elif self._state == PASY_TEST_FAIL:
64 return "%s - FAILED - %s" % (self._name, self.failMsg)
66 return "%s - INCOMPLETE" % (self._name,)
72 class PasyTestFunc(PasyTestStep):
74 def __init__(self, name, func):
75 PasyTestStep.__init__(self, name)
86 class PasyTest(PasyTestStep):
90 def __init__(self, name, cont):
91 PasyTestStep.__init__(self, name)
96 for name in self.__tests__:
97 func = getattr(self, name)
98 self._addfunc(func.func_name, func)
100 def _addfunc(self, name, func):
101 functest = PasyTestFunc(func.func_name, func)
102 self._tests.append(functest)
105 self._iter = self._test_iterator()
106 gobject.idle_add(self.idle_handler)
108 def idle_handler(self):
110 step = self._iter.next()
111 def finished_handler():
112 if step.state == PASY_TEST_WIN or self._cont == True:
113 gobject.idle_add(self.idle_handler)
114 elif step.state == PASY_TEST_FAIL and self._cont == False:
115 self.fail("Sub test %s Failed" % step._name)
116 step.events.finished += finished_handler
118 except StopIteration:
119 # No More tests, check for success or fail
121 for test in self._tests:
122 succeeded = succeeded and (test.state == PASY_TEST_WIN)
129 def _test_iterator(self):
130 for test in self._tests:
134 if self._state == PASY_TEST_WIN:
135 header = "%s - PASSED" % (self._name,)
136 elif self._state == PASY_TEST_FAIL:
137 header = "%s - FAILED" % (self._name,)
139 header = "%s - INCOMPLETE" %s (self._name,)
142 for test in self._tests:
143 step_messages.append(test.report())
145 step_report = "\n\t".join(step_messages)
146 return header + "\n\t" + step_report