7 from unittest
import TestCase, main
8 from gnucash.function_class
import ClassFromFunctions, default_arguments_decorator
12 """instance class for ClassFromFunction tests""" 17 def prefix_new_function():
18 """new function for ClassFromFunction tests 20 returns instance of Instance class""" 24 def prefix_test_function(self):
25 """test function for ClassFromFunction tests""" 29 def prefix_test_function_return_args(self, *args, **kargs):
30 return self, args, kargs
33 b_default =
"b default value" 36 def prefix_test_function_return_arg_karg(self, a, b=b_default):
37 return {
"self": self,
"a": a,
"b": b}
40 def other_function(self, arg=None):
45 _module = sys.modules[__name__]
51 def test_add_constructor_and_methods_with_prefix(self):
52 TestClass.add_constructor_and_methods_with_prefix(
"prefix_",
"new_function")
55 self.assertIsInstance(self.
testClass.instance, Instance)
56 self.assertTrue(self.
testClass.test_function())
59 """test if add_method adds method and if in case of FunctionClass 60 Instance instances get returned instead of FunctionClass instances""" 61 TestClass.add_method(
"other_function",
"other_method")
63 obj, arg = self.
t.other_method()
64 self.assertIsInstance(obj, Instance)
65 obj, arg = self.
t.other_method(self.
t)
66 self.assertIsInstance(arg, Instance)
67 obj, arg = self.
t.other_method(arg=self.
t)
68 self.assertIsInstance(arg, Instance)
71 """test if ya_add_method adds method and if in case of FunctionClass 72 Instance instances get returned instead of FunctionClass instances 73 with the exception of self (first) argument""" 74 TestClass.ya_add_method(
"other_function",
"other_method")
76 obj, arg = self.
t.other_method()
77 self.assertIsInstance(obj, TestClass)
78 obj, arg = self.
t.other_method(self.
t)
79 self.assertIsInstance(arg, Instance)
80 obj, arg = self.
t.other_method(arg=self.
t)
81 self.assertIsInstance(arg, Instance)
84 """test default_arguments_decorator()""" 85 TestClass.backup_test_function_return_args = TestClass.test_function_return_args
86 TestClass.backup_test_function_return_arg_karg = (
87 TestClass.test_function_return_arg_karg
95 TestClass.decorate_method(
96 default_arguments_decorator,
"test_function_return_args", arg1, arg2
99 self.
t.test_function_return_args(), (self.
t.instance, (arg2,), {})
102 self.
t.test_function_return_args(arg3), (self.
t.instance, (arg3,), {})
105 self.
t.test_function_return_args(arg1, arg3),
106 (self.
t.instance, (arg1, arg3), {}),
109 self.
t.test_function_return_args(arg1, arg3, arg4=arg4),
110 (self.
t.instance, (arg1, arg3), {
"arg4": arg4}),
113 TestClass.test_function_return_args = TestClass.backup_test_function_return_args
114 TestClass.decorate_method(
115 default_arguments_decorator,
116 "test_function_return_args",
122 self.
t.test_function_return_args(),
123 (self.
t.instance, (arg2,), {
"arg4": arg4}),
126 self.
t.test_function_return_args(arg1, arg3, arg4=arg2),
127 (self.
t.instance, (arg1, arg3), {
"arg4": arg2}),
130 with self.assertRaises(TypeError):
132 TestClass.decorate_method(
133 default_arguments_decorator,
134 "test_function_return_arg_karg",
138 kargs_pos={
"a": 1,
"b": 2},
140 TestClass.decorate_method(
141 default_arguments_decorator,
142 "test_function_return_arg_karg",
145 kargs_pos={
"a": 1,
"b": 2},
148 self.
t.test_function_return_arg_karg(),
149 {
"self": self.
t.instance,
"a": arg1,
"b": b_default},
152 TestClass.test_function_return_arg_karg = (
153 TestClass.backup_test_function_return_arg_karg
155 TestClass.decorate_method(
156 default_arguments_decorator,
157 "test_function_return_arg_karg",
160 kargs_pos={
"a": 1,
"b": 2},
163 self.
t.test_function_return_arg_karg(),
164 {
"self": self.
t.instance,
"a": arg1,
"b": b_default},
167 self.
t.test_function_return_arg_karg(arg2),
168 {
"self": self.
t.instance,
"a": arg2,
"b": b_default},
171 self.
t.test_function_return_arg_karg(arg2, arg3),
172 {
"self": self.
t.instance,
"a": arg2,
"b": arg3},
176 if __name__ ==
"__main__":
def test_ya_add_method(self)
def test_default_arguments_decorator(self)
def test_add_method(self)