Merge branch 'experimental' of acidburn@git.opendreambox.org:/git/enigma2 into experi...
[enigma2.git] / tests / events.py
1 import time
2 import tests
3
4 recorded_events = [ ]
5
6 def event(self, name, args, kwargs):
7         global recorded_events
8         print "*EVENT*", time.time(), self, name, args, kwargs
9         recorded_events.append((time.time(), self, name, args, kwargs))
10
11 def eventfnc(f):
12         name = f.__name__
13         def wrapper(self, *args, **kwargs):
14                 event(self, name, args, kwargs)
15                 return f(self, *args, **kwargs)
16         return wrapper
17
18 def get_events():
19         global recorded_events
20         r = recorded_events
21         recorded_events = [ ]
22         return r
23
24 def start_log():
25         global base_time
26         base_time = time.time()
27
28 def end_log(test_name):
29         global base_time
30         
31         results = ""
32         
33         for (t, self, method, args, kwargs) in get_events():
34                 results += "%s T+%f: %s::%s(%s, *%s, *%s)\n"  % (time.ctime(t), t - base_time, str(self.__class__), method, self, args, kwargs)
35
36         expected = None
37
38         try:
39                 f = open(test_name + ".results", "rb")
40                 expected = f.read()
41                 f.close()
42         except:
43                 print "NO TEST RESULT FOUND, creating new"
44                 f = open(test_name + ".new_results", "wb")
45                 f.write(results)
46                 f.close()
47         
48         print results
49         
50         if expected is not None:
51                 print "expected:"
52                 if expected != results:
53                         open(test_name + ".bogus_results", "wb").write(results)
54                         raise tests.TestError("test data does not match")
55                 else:
56                         print "test compared ok"
57         else:
58                 print "no test data to compare with."
59
60 def log(fnc, base_time = 0, test_name = "test", *args, **kwargs):
61         import fake_time
62         fake_time.setTime(base_time)
63
64         start_log()
65         try:
66                 fnc(*args, **kwargs)
67                 event(None, "test_completed", [], {"test_name": test_name})
68         except tests.TestError,c:
69                 event(None, "test_failed", [], {"test_name": test_name, "reason": str(c)})
70         end_log(test_name)