1 __author__ = 'ggegiya1'
2
3
7
8
11 self.argument = None
12 self.extra_arguments = None
13 self.validation_function = None
14
16
17
18 if hasattr(arg, "__call__"):
19 self.validation_function = arg
20 self.extra_arguments = arguments
21 else:
22 self.argument = arg
23 return self
24
26 """
27 Embedded method to compare the passed argument to the self.argument
28 :param argument2:
29 :return: True if arguments are equal, else throw an error
30 """
31 if self.argument == argument2:
32 return True
33 self.throw_error("%s and %s are not equal"
34 % (self._short(self.argument),
35 self._short( argument2)))
36
38 if (not hasattr(self.argument, "__iter__")) and (type(self.argument) not in [str]):
39 raise ValueError("The first argument is not iterable")
40 if argument2 in self.argument:
41 return True
42 self.throw_error("The list [%s..] does not contain %s"
43 % (self._short(self.argument),
44 self._short(argument2)))
45
48
51
53 """
54 Validates the first argument or validation function
55 :param expected: Expected boolean value of the validation function
56 :return: True if validation function result == expected
57 """
58 if self.argument is not None:
59 if bool(self.argument) is expected:
60 return True
61 else: self.throw_error("%s is not %s" % (self._short(self.argument), expected))
62 elif self.validation_function is not None:
63 test_status, test_report = self.validation_function(*self.extra_arguments)
64 if test_status is expected: return True
65 self.throw_error(test_report)
66 else:
67 raise ValueError("Invalid arguments passed to assertion")
68
70 raise TestFailure("\r\n" * 2 + "-" * 40 + "\r\nTest Failed!!!\r\n"
71 + (message or "")
72 + "\r\n")
73
74 - def _short(self, arg, length=5):
75 """
76 Shorten the string representation of the argument
77 :param arg: an arbitrary value
78 :param length: the length of the resulting string
79 :return: shortened string
80 """
81 if hasattr(arg, "__iter__"):
82 result = ", ".join([str(arg[i]) for i in xrange(0, min(len(arg), length))])
83 else:
84 result = str(arg)[:80]
85 return result
86
87
88
89 -def equals(argument1, argument2):
90 if argument1 == argument2:
91 return True, "%s and %s are equal" % (argument1, argument2)
92 return False, "%s and %s are not equal" % (argument1, argument2)
93
94
95
96
98
99
100
103
106
109
112
115