-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestAssert.prg
212 lines (138 loc) · 5.18 KB
/
TestAssert.prg
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
202
203
204
205
206
207
208
209
210
211
212
//-- copyright
// hbunit is a unit-testing framework for the Harbour language.
//
// Copyright (C) 2019 Manuel Calero Solis <manuelcalerosolis _at_ gmail _dot_ com>
//
// Based on hbunit from Enderson maia <endersonmaia _at_ gmail _dot_ com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// See COPYRIGHT for more details.
//++
#include "hbunit.ch"
//---------------------------------------------------------------------------//
CLASS TAssert
DATA oResult
METHOD new( oResult ) CONSTRUCTOR
METHOD equals( xExp, xAct, cMsg )
METHOD notEquals( xExp, xAct, cMsg )
METHOD true( xAct, cMsg )
METHOD false( xAct, cMsg )
METHOD null( xAct, cMsg )
METHOD notNull( xAct, cMsg )
PROTECTED:
METHOD isEqual( xExp, xAct )
METHOD assert( xExp, xAct, cMsg, lInvert )
METHOD fail( cMsg )
METHOD toStr( xVal, lUseQuote )
ENDCLASS
//---------------------------------------------------------------------------//
METHOD new( oResult ) CLASS TAssert
::oResult := oResult
RETURN ( self )
//---------------------------------------------------------------------------//
METHOD fail( cMsg ) CLASS TAssert
RETURN ( ::assert( .f.,, "Failure: " + cMsg ) )
//---------------------------------------------------------------------------//
METHOD equals( xExp, xAct, cMsg ) CLASS TAssert
local cErrMsg := ""
cErrMsg += "Exp: " + ::toStr( xExp, .t. )
cErrMsg += ", Act: " + ::toStr( xAct, .t. )
cErrMsg += "( " + cMsg + " )"
RETURN ( ::assert( xExp, xAct, cErrMsg ) )
//---------------------------------------------------------------------------//
METHOD notEquals( xExp, xAct, cMsg ) CLASS TAssert
local cErrMsg := ""
cErrMsg += "Exp: not " + ::toStr( xExp, .t. )
cErrMsg += ", Act: " + ::toStr( xAct )
cErrMsg += "( " + cMsg + " )"
RETURN ( ::assert( xExp, xAct, cErrMsg, .t. ) )
//---------------------------------------------------------------------------//
METHOD true( xAct, cMsg ) CLASS TAssert
local cErrMsg := ""
cErrMsg += "Exp: .t., Act: "
cErrMsg += ::toStr( xAct, .t. )
cErrMsg += "( " + cMsg + " )"
RETURN ( ::assert( .t., xAct , cErrMsg ) )
//---------------------------------------------------------------------------//
METHOD false( xAct, cMsg ) CLASS TAssert
local cErrMsg := ""
cErrMsg += "Exp: .f., Act: "
cErrMsg += ::toStr( xAct, .t. )
cErrMsg += "( " + cMsg + " )"
RETURN ( ::assert( .f., xAct , cErrMsg ) )
//---------------------------------------------------------------------------//
METHOD null( xAct, cMsg ) CLASS TAssert
local cErrMsg := ""
cErrMsg += "Exp: nil, Act: "
cErrMsg += ::toStr( xAct, .t. )
cErrMsg += "( " + cMsg + " )"
RETURN ( ::assert( nil, xAct , cErrMsg ) )
//---------------------------------------------------------------------------//
METHOD notNull( xAct, cMsg ) CLASS TAssert
local cErrMsg := ""
cErrMsg += "Exp: not nil, Act: "
cErrMsg += ::toStr( xAct, .t. )
cErrMsg += "( " + cMsg + " )"
RETURN ( ::assert( nil, xAct , cErrMsg, .t. ) )
//---------------------------------------------------------------------------//
METHOD assert( xExp, xAct, cMsg, lInvert ) CLASS TAssert
local oError
if( lInvert == nil, lInvert := .f., )
BEGIN SEQUENCE
::oResult:oData:incrementAssertCount()
if ( ( lInvert .and. ::isEqual( xExp, xAct ) ) .or. ( !( lInvert ) .and. ( !( ::isEqual( xExp, xAct ) ) ) ) )
oError := ErrorNew()
oError:description := procfile( 2 ) + ":" + ltrim( str( procline( 2 ) ) ) + ":" + procname( 2 ) + " => "
oError:description += cMsg
oError:filename := Procfile(2)
::oResult:oData:addFailure( oError )
else
::oResult:oData:addAssert( cMsg )
endif
RECOVER USING oError
::oResult:oData:addError( oError )
END
RETURN ( nil )
//---------------------------------------------------------------------------//
METHOD isEqual( xExp, xAct ) CLASS TAssert
local lResult := .f.
do case
case ValType( xExp ) != ValType( xAct )
case ( !( xExp == xAct ))
otherwise
lResult := .t.
endcase
RETURN ( lResult )
//---------------------------------------------------------------------------//
METHOD toStr( xVal, lUseQuote ) CLASS TAssert
local cStr, i
if( lUseQuote == nil, lUseQuote := .f., )
do case
case ( valtype( xVal ) == "C" )
cStr := xVal
case ( valtype( xVal ) == "M" )
cStr := xVal
case ( valtype( xVal ) == "L" )
cStr := if( xVal, ".t.", ".f." )
case ( valtype( xVal ) == "D" )
cStr := DToC( xVal )
case ( valtype( xVal ) == "N" )
cStr := alltrim( str( xVal ) )
case ( valtype( xVal ) == "A" )
cStr := hb_valtoexp( xVal )
case ( valtype( xVal ) == "O" )
cStr := "obj"
case ( valtype( xVal ) == "B" )
cStr := "blk"
otherwise
cStr := "nil"
end
if ( lUseQuote .and. ValType( xVal ) == "C" )
cStr := "'" + cStr+ "'"
endif
RETURN ( cStr )
//---------------------------------------------------------------------------//