-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbUnit.asserts.cb
201 lines (160 loc) · 7.25 KB
/
cbUnit.asserts.cb
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// PUBLIC FUNCTIONS
// Keep the functions ordered by the following rules:
// - Group by category.
// - Sort alphabetically.
// - Exception to aplhabetical order: AssertNot*() functions should follow immediately after their non-not counterparts.
//
// 'Not' functions should have the 'Not' appear right after 'Assert'! For example, correct: AssertNotFileExists(), incorrect: AssertFileNotExists()
Function Assert(condition, message$, default_message$="", a$="",b$="",c$="",d$="")
If condition Then
// Test succeeded
cbUnit_WriteMessage(CBUNIT_MESSAGE_SUCCESS)
Return True
Else
// Test failed
message = cbUnit_FillMessage(message, default_message, a,b,c,d)
cbUnit_WriteMessage(CBUNIT_MESSAGE_FAILURE, message)
cbUnit_HasFailures = True
If Upper(Str(CBUNIT_STOP_AT)) = "ASSERT" Then
cbUnit_SetCurrentContext("hook_CleanupTest")
hook_CleanupTest(cbUnit_CurrentContext) // Hook For test_*.cb To do cleanup. Not called in cbUnit_EndProgram() because otherwise the hook would be called twice For the Last test Function of the file, because test-call.skeleton.cb calls this too.
cbUnit_SetCurrentContext("")
cbUnit_EndProgram()
EndIf
Return False
EndIf
EndFunction
Function AssertNot(condition, message$="")
Return Assert(Not condition, message, "Assertion condition should be false.")
EndFunction
// Equals
Function AssertEquals(a$, b$, message$="")
Return Assert(a = b, message, "a$ should be equal to b$.", a,b)
EndFunction
Function AssertNotEquals(a$, b$, message$="")
Return Assert(a <> b, message,"a$ should not be equal to b$.", a,b)
EndFunction
Function AssertGreater(a$, b$, message$)
Return Assert(a > b, message,"a$ should be greater than b$.", a,b)
EndFunction
Function AssertGreaterOrEqual(a$, b$, message$="")
Return Assert(a >= b, message,"a$ should be greater than or equal to b$.", a,b)
EndFunction
Function AssertLess(a$, b$, message$="")
Return Assert(a < b, message,"a$ should be less than b$.", a,b)
EndFunction
Function AssertLessOrEqual(a$, b$, message$="")
Return Assert(a <= b, message,"a$ should be less than or equal to b$.", a,b)
EndFunction
// Strings
Function AssertBeginsWith(a$, b$, ignore_case=0, message="")
If ignore_case Then a = Lower(a) : b = Lower(b)
Return Assert(Left(a,Len(b)) = b, message,"a$ should begin with b$.", a,b)
EndFunction
Function AssertNotBeginsWith(a$, b$, ignore_case=0, message="")
If ignore_case Then a = Lower(a) : b = Lower(b)
Return Assert(Left(a,Len(b)) <> b, message,"a$ should not begin with b$.", a,b)
EndFunction
Function AssertEmpty(a$, message$="")
Return Assert(a = "", message,"a$ should be an empty string.", a)
EndFunction
Function AssertNotEmpty(a$, message$="")
Return Assert(a <> "", message,"a$ should not be an empty string.", a)
EndFunction
Function AssertEndsWith(a$, b$, ignore_case=0, message="")
If ignore_case Then a = Lower(a) : b = Lower(b)
Return Assert(Right(a,Len(b)) = b, message,"a$ should end with b$.", a,b)
EndFunction
Function AssertNotEndsWith(a$, b$, ignore_case=0, message="")
If ignore_case Then a = Lower(a) : b = Lower(b)
Return Assert(Right(a,Len(b)) <> b, message,"a$ should not end with b$.", a,b)
EndFunction
Function AssertInStr(a$, b$, ignore_case=0, message$="")
If ignore_case Then a = Lower(a) : b = Lower(b)
Return Assert(InStr(a,b) > 0, message,"b$ should be present in a$.", a,b)
EndFunction
Function AssertNotInStr(a$, b$, ignore_case=0, message$="")
If ignore_case Then a = Lower(a) : b = Lower(b)
Return Assert(InStr(a,b) = 0, message,"b$ should not be present in a$.", a,b)
EndFunction
Function AssertIsLower(a$, message$="")
Return Assert(Lower(a) = a, message,"a$ should not contain any uppercase letters.", a)
EndFunction
Function AssertIsUpper(a$, message$="")
Return Assert(Upper(a) = a, message,"a$ should not contain any lowercase letters.", a)
EndFunction
Function AssertLen(a$, b$, message$="")
Return Assert(Len(a) = Int(b), message,"The length of a$ should be b$, now it's c$.", a, b, Len(a))
EndFunction
// Math
Function AssertMaxDifference(a#, b#, c#, message$="")
Dim difference#
difference = Abs(a-b)
Return Assert(difference <= c, message,"The difference between a$ and $b should be $c at maximum. Now the difference is d$.", a,b,c,difference)
EndFunction
Function AssertMinDifference(a#, b#, c#, message$="")
Dim difference#
difference = Abs(a-b)
Return Assert(difference >= c, message,"The difference between a$ and $b should be $c at minimum. Now the difference is d$.", a,b,c,difference)
EndFunction
// a: The value whose sign should be tested.
// b: The assumed sign, either -1 or 1. Or in some cases 0, but then a is supposed to be zero too, so then you could be using AssertZero(a) instead.
// zero_sign: If a happens to be zero, this determines the sign for it, because it might depend on case, which sign we want to use for zero. Defaults to 0.
Function AssertSign(a#,b, zero_sign=0, message$="")
Dim sign
If a = 0 Then
sign = zero_sign
Else
sign = a/Abs(a)
EndIf
Return Assert(sign = b, message,"The sign of number a$ should be b$, but it is c$.", a,b,sign)
EndFunction
Function AssertZero(a$, message$="") // Use a$ instead of a# so that we can assure that "" is not accidentally interpreted as 0.
Return Assert(a="0" Or a="0.0", message,"a$ should be zero.", a)
EndFunction
// Data types
Function AssertBool(a$, message$="")
Return Assert(a = "0" Or a = "1", message,"a$ should be 0 or 1.", a) // Should we consider 0.0 And 1.0 too? Maybe Not.
EndFunction
Function AssertFloat(a$, message$="")
Return Assert(Str(Float(a)) = a, message,"a$ should be a float.", a)
EndFunction
Function AssertInteger(a$, message$="")
Return Assert(Str(Int(a)) = a, message,"a$ should be an integer.", a)
EndFunction
Function AssertIntegerOrFloat(a$, message$="")
Dim is_integer, is_float
is_integer = Str(Int(a)) = a
is_float = Str(Float(a)) = a
Return Assert(is_integer Or is_float, message,"a$ should be an integer or a float.", a)
EndFunction
// Files
Function AssertFileExists(a$, accept_directory=0, message$="")
Dim condition, default_message$
If accept_directory Then
condition = FileExists(a)
default_message = "File o"+"r directory a$ should exist."
Else
condition = FileExists(a) And False=IsDirectory(a)
default_message = "File a$ should exist And must n"+"ot be a directory."
EndIf
Return Assert(condition, message,default_message, a)
EndFunction
Function AssertNotFileExists(a$, accept_directory=0, message$="")
Dim condition, default_message$
If accept_directory Then
condition = Not FileExists(a)
default_message = "File o"+"r directory a$ should not exist."
Else
condition = False=FileExists(a) Or IsDirectory(a)
default_message = "File a$ should not exist (but a directory with that name would be allowed, although that's n"+"ot the c"+"ase now)."
EndIf
Return Assert(condition, message,default_message, a)
EndFunction
Function AssertDirectoryExists(a$, accept_directory=1, message$="")
Return Assert(IsDirectory(a), message,"Directory a$ should exist.", a)
EndFunction
Function AssertNotDirectoryExists(a$, accept_directory=1, message$="")
Return Assert(Not IsDirectory(a), message,"Directory a$ should exist.", a)
EndFunction