@@ -92,6 +92,25 @@ function render(mode::RenderMode, actual)
92
92
println (" -------------------------------" )
93
93
end
94
94
95
+
96
+ default_rendermode (:: TestMode , :: File , actual) = BeforeAfterFull ()
97
+
98
+ # plain TXT
99
+ default_rendermode (:: TestMode , :: File{format"TXT"} , actual) = BeforeAfterLimited ()
100
+ default_rendermode (:: TestMode , :: File{format"TXT"} , actual:: AbstractString ) = Diff ()
101
+ default_rendermode (:: TestMode , :: File{format"TXT"} , actual:: Number ) = BeforeAfterFull ()
102
+
103
+ # SHA256
104
+ default_rendermode (:: TestMode , :: File{format"SHA256"} , actual) = BeforeAfterFull ()
105
+
106
+ # images
107
+ function default_rendermode (:: TestMode , :: File , actual:: AbstractArray{<:Colorant} )
108
+ return BeforeAfterImage ()
109
+ end
110
+ function default_rendermode (:: TestMode , :: File{format"TXT"} , actual:: AbstractArray{<:Colorant} )
111
+ return BeforeAfterFull ()
112
+ end
113
+
95
114
# ######################################
96
115
# IO
97
116
# Right now this basically just extends FileIO to support some things as text files
@@ -118,17 +137,69 @@ function savefile(file::TextFile, content)
118
137
write (file. filename, content)
119
138
end
120
139
140
+ # ######################################
141
+ # Preprocess
142
+
143
+ preprocess (:: File , actual; kw... ) = actual
144
+
145
+ # plain TXT
146
+ preprocess (:: File{format"TXT"} , actual; kw... ) = string (actual)
147
+ preprocess (:: File{format"TXT"} , actual:: Number ; kw... ) = actual
148
+ function preprocess (:: File{format"TXT"} , actual:: AbstractArray{<:AbstractString} ; kw... )
149
+ return join (actual, ' \n ' )
150
+ end
151
+
152
+ # SHA256
153
+ preprocess (:: File{format"SHA256"} , actual; kw... ) = bytes2hex (sha256 (string (actual)))
154
+
155
+ # images
156
+ function preprocess (
157
+ :: File{format"TXT"} , actual:: AbstractArray{<:Colorant} ;
158
+ size = (20 ,40 ), kw... )
159
+
160
+ strs = @withcolor ImageInTerminal. encodeimg (
161
+ ImageInTerminal. SmallBlocks (),
162
+ ImageInTerminal. TermColor256 (),
163
+ actual,
164
+ size... )[1 ]
165
+ str = join (strs,' \n ' )
166
+ end
167
+
168
+ function preprocess (:: File{format"SHA256"} , actual:: AbstractArray{<:Colorant} ; kw... )
169
+ size_str = bytes2hex (sha256 (reinterpret (UInt8,[map (Int64,size (actual))... ])))
170
+ img_str = bytes2hex (sha256 (reinterpret (UInt8,vec (rawview (channelview (actual))))))
171
+
172
+ return size_str * img_str
173
+ end
174
+
175
+
121
176
# #########################################
122
177
123
178
# Final function
124
179
# all other functions should hit one of this eventually
125
180
# Which handles the actual testing and user prompting
126
181
127
- function _test_reference (equiv, rendermode, file:: File , actual:: T ) where T
128
- path = file. filename
129
- dir, filename = splitdir (path)
182
+ function test_reference (
183
+ filename:: AbstractString , actual;
184
+ by = nothing , render = nothing , kw... )
185
+
186
+ test_reference (query_extended (filename), actual, render, by; kw... )
187
+ end
188
+
189
+ function test_reference (
190
+ file:: File ,
191
+ actual:: T ,
192
+ rendermode:: Union{RenderMode, Nothing} = nothing ,
193
+ equiv:: Union{Function, Nothing} = nothing ;
194
+ kw... ) where T
195
+
130
196
testmode = TESTMODE ()
197
+ if isnothing (rendermode)
198
+ rendermode = default_rendermode (testmode, file, actual)
199
+ end
131
200
201
+ path = file. filename
202
+ dir, filename = splitdir (path)
132
203
if ! isfile (path)
133
204
println (" Reference file for \" $filename \" does not exist." )
134
205
render (rendermode, actual)
@@ -141,8 +212,14 @@ function _test_reference(equiv, rendermode, file::File, actual::T) where T
141
212
end
142
213
143
214
# file exists
215
+ actual = preprocess (file, actual; kw... )
144
216
reference = loadfile (T, file)
145
217
218
+ if isnothing (equiv)
219
+ # generally, `reference` and `actual` are of the same type after preprocessing
220
+ equiv = default_equality (typeof (reference), typeof (actual))
221
+ end
222
+
146
223
if equiv (reference, actual)
147
224
@test true
148
225
else
0 commit comments