-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfluentexpr.prw
159 lines (135 loc) · 4.61 KB
/
fluentexpr.prw
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
#include 'protheus.ch'
#include 'testsuite.ch'
#xcommand Throw <cMsg> With <aValues> ;
=> ;
__MSG__ := Format( <cMsg>, <aValues> ) ;;
aAdd( aTestReport, { .F., __MSG__ } ) ;;
UserException( __MSG__ ) ;;
Return Self
#xcommand Passed <cMsg> With <aValues> ;
=> ;
__MSG__ := Format( <cMsg>, <aValues> ) ;;
aAdd( aTestReport, { .T., __MSG__ } ) ;;
Return Self
Static Function Format( cString, aValues )
Local cResult := cString
Local nIndex
For nIndex := 1 To Len( aValues )
cResult := StrTran( cResult, '{' + AllTrim( Str( nIndex ) ) + '}', ToString( aValues[ nIndex ] ) )
Next
Return cResult
Static Function ToString( xValue )
Local cType := ValType( xValue )
If cType == "A"
Return '{ ' + ArrTokStr( xValue, ', ') + ' }'
ElseIf cType == "B"
Return GetCBSource( xValue )
ElseIf cType == "O"
Return ArrTokStr( ClassMethArr( xValue, .T. ), ' | ' )
EndIf
Return cValToChar( xValue )
Class FluentExpr
Data xValue
Data lNot
Method New( xValue ) Constructor
Method Not()
Method ToBe( xOther )
Method ToBeAFile()
Method ToBeAFileWithContents( cContent )
Method ToBeAFolder()
Method ToHaveType( cType )
Method ToThrowError()
EndClass
Method New( xValue ) Class FluentExpr
::xValue := xValue
::lNot := .F.
Return Self
Method Not() Class FluentExpr
::lNot := .T.
Return Self
Method ToBe( xOther ) Class FluentExpr
Local cValue := ToString( ::xValue )
Local cOther := ToString( xOther )
If ::lNot
If cValue == cOther
Throw 'Expected {1} to not be {2}' With { cValue, cOther }
EndIf
Passed 'Expected {1} to not be {2}' With { cValue, cOther }
Else
If !(cValue == cOther)
Throw 'Expected {1} to be {2}' With { cValue, cOther }
EndIf
Passed 'Expected {1} to be {2}' With { cValue, cOther }
EndIf
Return Self
Method ToBeAFile() Class FluentExpr
Local lIsFile := File( ::xValue )
If ::lNot
If lIsFile
Throw 'Expected {1} to not be a file' with { ::xValue }
EndIf
Passed 'Expected {1} to not be a file' With { ::xValue }
Else
If !lIsFile
Throw 'Expected {1} to be a file' with { ::xValue }
EndIf
Passed 'Expected {1} to be a file' With { ::xValue }
EndIf
Return Self
Method ToBeAFileWithContents( cContent ) Class FluentExpr
Local lIsFile := File( ::xValue )
If ::lNot
If lIsFile .And. ReadFileContents( ::xValue ) == cContent
Throw 'Expected {1} to not be a file with contents "{2}"' with { ::xValue, cContent }
EndIf
Passed 'Expected {1} to not be a file with contents "{2}"' With { ::xValue, cContent }
Else
If !lIsFile .Or. !(ReadFileContents( ::xValue ) == cContent)
Throw 'Expected {1} to be a file with contents "{2}"' with { ::xValue, cContent }
EndIf
Passed 'Expected {1} to be a file with contents "{2}"' With { ::xValue, cContent }
EndIf
Return Self
Method ToBeAFolder() Class FluentExpr
Local lIsFolder := ExistDir( ::xValue )
If ::lNot
If lIsFolder
Throw 'Expected {1} to not be a folder' with { ::xValue }
EndIf
Passed 'Expected {1} to not be a folder' With { ::xValue }
Else
If !lIsFolder
Throw 'Expected {1} to be a folder' with { ::xValue }
EndIf
Passed 'Expected {1} to be a folder' With { ::xValue }
EndIf
Return Self
Method ToHaveType( cType ) Class FluentExpr
Local cMyType := ValType( ::xValue )
If ::lNot
If cMyType == cType
Throw 'Expected {1} to not have type {2}, but it does' With { ::xValue, cType }
EndIf
Passed 'Expected {1} to not have type {2} (it is a {3})' With { ::xValue, cType, cMyType }
Else
If !(cMyType == cType)
Throw 'Expected {1} to have type {2}, but it has type {3}' With { ::xValue, cType, cMyType }
EndIf
Passed 'Expected {1} to have type {2}' With { ::xValue, cType }
EndIf
Return Self
Method ToThrowError() Class FluentExpr
Local oError
Local bError := ErrorBlock( { |oExc| oError := oExc } )
Local cSource := GetCBSource( ::xValue )
Begin Sequence
Eval( ::xValue )
End Sequence
ErrorBlock( bError )
If oError == Nil
aAdd( aTestReport, { .F., 'Expected {1} to throw an error', { cSource } } )
UserException( 'Expected ' + cSource + ' to throw error' )
Return Self
EndIf
aAdd( aTestReport, { .T., 'Expected {1} to throw an error', { cSource } } )
Return Self