Debian Codegraph @

tests/fife_test/scripts/test.py

raw
1 #!/usr/bin/env python
2
3 # -*- coding: utf-8 -*-
4
5 # ####################################################################
6 # Copyright (C) 2005-2019 by the FIFE team
7 # http://www.fifengine.net
8 # This file is part of FIFE.
9 #
10 # FIFE is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 # License as published by the Free Software Foundation; either
13 # version 2.1 of the License, or (at your option) any later version.
14 #
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with this library; if not, write to the
22 # Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 # ####################################################################
25
26 import os
27
28 class TestManager(object):
29 def __init__(self, engine, application, settings):
30 self._engine = engine
31 self._application = application
32 self._settings = settings
33
34 self._running = None
35
36 self._testdir = "tests"
37 self._tests = []
38
39 files = []
40 for f in os.listdir(self._testdir):
41 path = os.path.join(self._testdir, f)
42 if os.path.isfile(path) and os.path.splitext(f)[1] == ".py" and f != "__init__.py":
43 files.append(os.path.splitext(f)[0])
44
45 for f in files:
46 importtest = self._settings.get("Tests", f, False)
47 if importtest:
48 try:
49 print("Importing test plugin: ", f)
50 exec("import " + self._testdir + "." + f)
51 test = eval(self._testdir + "." + f + "." + f + "()")
52 if isinstance(test, Test) is False:
53 print(f + " is not an instance of Test!")
54 else:
55 self._tests.append(test)
56 except BaseException as error:
57 print("Error: ", error)
58 print("Invalid test: ", f)
59 else:
60 print("Not importing test: ", f)
61
62 self._settings.set("Tests", f, importtest)
63
64 def _getRunningTest(self):
65 return self._running
66
67 def runTest(self, test):
68 if test in self._tests and not self._running:
69 self._running = test
70 self._running.create(self._engine, self._application)
71 self._running.run()
72
73 def stopTest(self):
74 if self._running:
75 if self._running.isRunning():
76 self._running.stop()
77 self._running.destroy()
78
79 self._running = None
80
81 def resetTest(self):
82 if self._running:
83 if self._running.isRunning():
84 self._running.stop()
85
86 self._running.destroy()
87 self._running.create(self._engine, self._application)
88 self._running.run()
89
90 def _getTests(self):
91 return self._tests
92
93 def _getTestNameList(self):
94 namelist = []
95 for t in self._tests:
96 namelist.append(t.getName())
97
98 return namelist
99
100 tests = property(_getTests)
101 testnames = property(_getTestNameList)
102 runningtest = property(_getRunningTest)
103
104 class Test(object):
105 """ The base calss for all tests. All tests must override these functions! """
106
107 def create(self, engine, application):
108 raise NotImplementedError("Test has not implemented the init() function!")
109
110 def destroy(self):
111 raise NotImplementedError("Test has not implemented the destroy() function!")
112
113 def run(self):
114 raise NotImplementedError("Test has not implemented the run() function!")
115
116 def stop(self):
117 raise NotImplementedError("Test has not implemented the stop() function!")
118
119 def isRunning(self):
120 raise NotImplementedError("Test has not implemented the isRunning() function!")
121
122 def getName(self):
123 raise NotImplementedError("Test has not implemented the getName() function!")
124
125 def getAuthor(self):
126 return "unknown"
127
128 def getDescription(self):
129 return "none"
130
131 def getHelp(self):
132 return "You're on your own for this one!"
133
134 def onConsoleCommand(self, cmd):
135 return cmd[0] + ": not found."
136
137 def pump(self):
138 pass
139
140