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