Debian Codegraph @

lib/tests/test_decorator.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2011 QBzr Developers
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 import time
21 from PyQt5 import QtCore
22 from breezy.plugins.qbrz.lib.decorators import lazy_call
23 from breezy.tests import TestCase, TestCaseWithTransport
24 from breezy.plugins.qbrz.lib import tests as qtests
25
26
27 test_value = 0
28 test2_value = 0
29
30 @lazy_call(50)
31 def method_test(arg):
32 global test_value
33 test_value += arg
34
35 @lazy_call(50)
36 def method_test2(arg):
37 global test2_value
38 test2_value += arg
39
40 class ClassTest(object):
41 def __init__(self):
42 self.value = 0
43 self.value2 = 0
44
45 @lazy_call(50, per_instance=True)
46 def test(self, value):
47 self.value += value
48
49 @lazy_call(50)
50 def test2(self, value):
51 self.value2 += value
52
53
54 class TestLazyCall(qtests.QTestCase):
55
56 def setUp(self):
57 qtests.QTestCase.setUp(self)
58 global test_value
59 global test2_value
60 test_value = test2_value = 0
61
62 def test_single(self):
63 method_test(1)
64 time.sleep(0.01)
65 QtCore.QCoreApplication.processEvents()
66 method_test(2)
67 time.sleep(0.01)
68 QtCore.QCoreApplication.processEvents()
69 method_test(4)
70 time.sleep(0.1)
71 QtCore.QCoreApplication.processEvents()
72 # Execute only last one call.
73 self.assertEqual(test_value, 4)
74
75 def test_twice(self):
76 method_test(1)
77 time.sleep(0.01)
78 QtCore.QCoreApplication.processEvents()
79 method_test(2)
80 time.sleep(0.1)
81 QtCore.QCoreApplication.processEvents()
82 method_test(4)
83 time.sleep(0.1)
84 QtCore.QCoreApplication.processEvents()
85 # Execute 2nd and 3rd calls.
86 self.assertEqual(test_value, 6)
87
88 def test_count_millisec_from_last_call(self):
89 for i in range(5):
90 method_test(1)
91 time.sleep(0.01)
92 QtCore.QCoreApplication.processEvents()
93 # No call is executed
94 self.assertEqual(test_value, 0)
95 time.sleep(0.1)
96 QtCore.QCoreApplication.processEvents()
97 # Execute only last one call.
98 self.assertEqual(test_value, 1)
99
100 def test_2_methods(self):
101 method_test(1)
102 method_test2(2)
103 time.sleep(0.01)
104 QtCore.QCoreApplication.processEvents()
105
106 method_test(2)
107 method_test2(4)
108 time.sleep(0.1)
109 QtCore.QCoreApplication.processEvents()
110
111 self.assertEqual(test_value, 2)
112 self.assertEqual(test2_value, 4)
113
114 def test_instance_method(self):
115 a = ClassTest()
116 b = ClassTest()
117 a.test(1)
118 b.test(10)
119 time.sleep(0.1)
120 QtCore.QCoreApplication.processEvents()
121 self.assertEqual(a.value, 1)
122 self.assertEqual(b.value, 10)
123
124 a.test(2)
125 b.test(20)
126 time.sleep(0.01)
127 QtCore.QCoreApplication.processEvents()
128
129 a.test(3)
130 b.test(30)
131 time.sleep(0.1)
132 QtCore.QCoreApplication.processEvents()
133 self.assertEqual(a.value, 4)
134 self.assertEqual(b.value, 40)
135
136 def test_without_instance_method_flag(self):
137 a = ClassTest()
138 b = ClassTest()
139 a.test2(1)
140 b.test2(10)
141 time.sleep(0.1)
142 QtCore.QCoreApplication.processEvents()
143 # a.test2 does not called.
144 self.assertEqual(a.value2, 0)
145 self.assertEqual(b.value2, 10)
146