-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
122 lines (103 loc) · 4.67 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import datetime
import unittest
from timer import Timer
from events import Event, MouseEvent, TimeEvent
from scripts import ScriptManager
class TestTimer(unittest.TestCase):
def test_checkIfAllMethodsAreCovered(self):
test_members = list(TestTimer.__dict__.keys())
class_members = list(Timer.__dict__.keys())
check_for = ['test_' + x for x in class_members if not x.startswith('_')]
are_covered = all(test in test_members for test in check_for)
if not are_covered:
diff = list(set(check_for) - set(test_members))
print("Timer functions that are not covered: ", diff)
self.assertTrue(False)
def test_fromClock(self):
timer = Timer.fromClock(12, 0, 1, 123456)
timeout = datetime.datetime(Timer.IDC, Timer.IDC, Timer.IDC, 12, 0, 1, 123456).time()
self.assertTrue(timer._timeout, timeout)
with self.assertRaises(ValueError):
Timer.fromClock(0, 0, 0, 1000000)
Timer.fromClock(0, 0, 60, 0)
Timer.fromClock(0, 60, 0, 0)
Timer.fromClock(24, 0, 0, 0)
def test_fromDuration(self):
d_hour = 21
d_min = 37
d_sec = 59
d_usec = 100
timer = Timer.fromDuration(d_hour, d_min, d_sec, d_usec)
now = datetime.datetime.now()
future = datetime.timedelta(hours=d_hour, minutes=d_min, seconds=d_sec, microseconds=d_usec)
timeout = (now + future).time()
h, m, s, u = (now + timer._timeout).strftime('%H %M %S %f').split(' ')
h2, m2, s2, u2 = timeout.strftime('%H %M %S %f').split(' ')
self.assertEqual(h, h2)
self.assertEqual(m, m2)
self.assertEqual(s, s2)
self.assertEqual(u, u2) # This is kind of danger due to time needed to perform the test
def getWaitTime(self, h, m, s, u, ret):
timer = Timer.fromDuration(h, m, s, u)
timer._timeout = (datetime.datetime.now() + timer._timeout).time()
self.assertEqual(ret, timer.getWaitTime())
def test_getWaitTime(self):
from timer import WAIT
self.getWaitTime(2, 20, 3, 300, WAIT.MIN)
self.getWaitTime(0, 20, 3, 300, WAIT.MIN)
self.getWaitTime(0, 2, 3, 300, WAIT.MIN)
self.getWaitTime(0, 0, 30, 300, WAIT.SEC)
self.getWaitTime(0, 0, 4, 300, WAIT.SEC)
self.getWaitTime(0, 0, 3, 300, WAIT.SEC)
self.getWaitTime(0, 0, 0, 300, WAIT.USEC)
with self.assertRaises(ValueError):
self.getWaitTime(0, 0, 0, 0, WAIT.USEC)
self.getWaitTime(0, 0, 0, -1, WAIT.USEC)
self.getWaitTime(0, 0, -1, 0, WAIT.USEC)
self.getWaitTime(0, -1, 0, 0, WAIT.USEC)
self.getWaitTime(-1, 0, 0, 0, WAIT.USEC)
def test_waitForTimeout(self):
"""Passed here due to time dilatation. Tested in testrealtime.py"""
pass
def test_IDC(self):
self.assertTrue(Timer.IDC)
class TestEvent(unittest.TestCase):
def test_createEvent(self):
e = Event.createEvent(MouseEvent, 1, 2)
self.assertTrue(isinstance(e, MouseEvent))
self.assertEqual(e._x, 1)
self.assertEqual(e._y, 2)
self.assertIsNone(Event.createEvent(Timer))
class TestScriptManager(unittest.TestCase):
def test_validateEventString(self):
s = ScriptManager()
# as in temple
self.assertTrue(s._validateEventString("test;x=str;y=10;z=abc;ff=10.0"))
# other order
self.assertTrue(s._validateEventString("test;ff=10.0;x=str;z=abc;y=10"))
# int can be str
self.assertTrue(s._validateEventString("test;ff=0;x=0;z=0;y=0"))
# wrong type
self.assertFalse(s._validateEventString("test;x=str;y=10;z=abc;ff=str"))
# missing arg
self.assertFalse(s._validateEventString("test;ff=10.0;x=str;z=abc"))
# doubled arg
self.assertFalse(s._validateEventString("test;ff=0;x=0;z=0;y=0;ff=0"))
# corrupted
self.assertFalse(s._validateEventString("test;ff===0;;=;0;=;;y=0;f;f=0"))
# from __str__
t = TimeEvent('clock', 13, 10, 5, 400)
m = MouseEvent(999, 200)
self.assertTrue(s._validateEventString(t.__str__()))
self.assertTrue(s._validateEventString(m.__str__()))
def test_createEvent(self):
e = ScriptManager._createEvent("mouse;y=100;x=50")
self.assertEqual(e._x, 50)
self.assertEqual(e._y, 100)
e = ScriptManager._createEvent("time;t=clock;h=12;m=0;s=30;u=500")
self.assertEqual(e._timer._timeout.hour, 12)
self.assertEqual(e._timer._timeout.minute, 0)
self.assertEqual(e._timer._timeout.second, 30)
self.assertEqual(e._timer._timeout.microsecond, 500)
if __name__ == '__main__':
unittest.main()