-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathPlotlyTestCase.m
More file actions
431 lines (403 loc) · 15.8 KB
/
Copy pathPlotlyTestCase.m
File metadata and controls
431 lines (403 loc) · 15.8 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
classdef PlotlyTestCase < handle
properties
CurrentTest = ''
Passed = 0
Failed = 0
Failures = {}
TestVerdicts = []
ErrorTrace = ''
end
properties (Access = private)
p_curPassed = 0
p_curFailed = 0
p_curDiagnostics = {}
end
methods
function startTest(testCase, name)
testCase.CurrentTest = name;
testCase.p_curPassed = 0;
testCase.p_curFailed = 0;
testCase.p_curDiagnostics = {};
testCase.ErrorTrace = '';
end
function v = finishTest(testCase)
v = struct('Name', testCase.CurrentTest, ...
'Passed', testCase.p_curFailed == 0, ...
'VerificationFailures', testCase.p_curFailed, ...
'Diagnostics', {testCase.p_curDiagnostics}, ...
'ErrorTrace', testCase.ErrorTrace, ...
'Duration', 0, ...
'Errored', false);
testCase.CurrentTest = '';
if isempty(testCase.TestVerdicts)
testCase.TestVerdicts = v;
else
testCase.TestVerdicts(end+1) = v;
end
end
function recordPass(testCase)
testCase.Passed = testCase.Passed + 1;
testCase.p_curPassed = testCase.p_curPassed + 1;
end
function recordFail(testCase, diagnostic)
testCase.Failed = testCase.Failed + 1;
testCase.p_curFailed = testCase.p_curFailed + 1;
diag = [diagnostic testCase.callSiteInfo()];
if isempty(testCase.CurrentTest)
testCase.Failures{end+1} = diag;
else
testCase.Failures{end+1} = [testCase.CurrentTest ': ' diag];
end
testCase.p_curDiagnostics{end+1} = diag;
end
function verifyEqual(testCase, actual, expected, varargin)
[absTol, msg] = testCase.parseVerifyArgs(varargin{:});
if isstruct(actual) && isstruct(expected)
testCase.compareHelper(actual, expected, '', varargin);
return;
end
if isa(actual, 'PlotlyTestCaseAny') || isa(expected, 'PlotlyTestCaseAny')
testCase.compareHelper(actual, expected, '', varargin);
return;
end
ok = testCase.isEqualCheck(actual, expected, absTol);
if ok
testCase.recordPass();
else
detail = sprintf(' Actual : %s\n Expected: %s', ...
testCase.formatValue(actual), testCase.formatValue(expected));
if absTol > 0 && isnumeric(actual) && isnumeric(expected)
detail = [detail sprintf('\n AbsTol : %s', strtrim(sprintf('%.17g', absTol)))];
end
if isnumeric(actual) && isnumeric(expected) && isscalar(actual) && isscalar(expected)
detail = [detail sprintf('\n |a-e| : %s', strtrim(sprintf('%.17g', abs(double(actual) - double(expected)))))];
end
if isempty(msg)
testCase.recordFail(sprintf('Values are not equal.\n%s', detail));
else
testCase.recordFail(sprintf('%s\n%s', msg, detail));
end
end
end
function verifyEqualStructs(testCase, actual, expected, varargin)
testCase.compareHelper(actual, expected, '<Value>', varargin);
end
function verifyNumElements(testCase, value, expectedCount)
actual = numel(value);
if actual == expectedCount
testCase.recordPass();
else
testCase.recordFail(sprintf( ...
'Expected %d elements but found %d.', expectedCount, actual));
end
end
function verifyTrue(testCase, condition, diagnostic)
if nargin < 3, diagnostic = ''; end
diag = diagnostic;
if condition
testCase.recordPass();
else
if isempty(diag)
diag = 'Condition was false.';
end
testCase.recordFail(diag);
end
end
function verifyFalse(testCase, condition, diagnostic)
if nargin < 3, diagnostic = ''; end
testCase.verifyTrue(~condition, diagnostic);
end
function verifyNotEmpty(testCase, value)
if ~isempty(value)
testCase.recordPass();
else
testCase.recordFail('Expected non-empty value but got empty.');
end
end
function verifyGreaterThan(testCase, value, floor, varargin)
if value > floor
testCase.recordPass();
else
if numel(varargin) >= 1 && ischar(varargin{1})
msg = varargin{1};
testCase.recordFail(msg);
else
testCase.recordFail(sprintf( ...
'Expected value > %s but got %s.', testCase.formatValue(floor), testCase.formatValue(value)));
end
end
end
function verifyGreaterThanOrEqual(testCase, value, floor, varargin)
if value >= floor
testCase.recordPass();
else
if numel(varargin) >= 1 && ischar(varargin{1})
msg = varargin{1};
testCase.recordFail(msg);
else
testCase.recordFail(sprintf( ...
'Expected value >= %s but got %s.', testCase.formatValue(floor), testCase.formatValue(value)));
end
end
end
function verifyLessThan(testCase, value, ceiling, varargin)
if value < ceiling
testCase.recordPass();
else
if numel(varargin) >= 1 && ischar(varargin{1})
msg = varargin{1};
testCase.recordFail(msg);
else
testCase.recordFail(sprintf( ...
'Expected value < %s but got %s.', testCase.formatValue(ceiling), testCase.formatValue(value)));
end
end
end
end
methods (Access = private)
function info = callSiteInfo(testCase)
%-the first stack frame outside this class file: the
%-assertion's location in the test file-%
info = '';
st = dbstack;
thisFile = [mfilename('fullpath') '.m'];
for i = 2:numel(st)
if ~isempty(st(i).file) && ~strcmp(st(i).file, thisFile)
info = sprintf('\n at %s:%d', st(i).file, st(i).line);
line = PlotlyTestCase.readSourceLine(st(i).file, st(i).line);
if ~isempty(line)
info = sprintf('%s\n %s', info, strtrim(line));
end
return;
end
end
end
function compareHelper(testCase, actual, expected, path, extraArgs)
if isa(expected, 'PlotlyTestCaseAny')
matchResult = expected.match(actual);
if ~matchResult.passed
diagnostic = sprintf('Path to failure: %s\n%s', path, matchResult.diagnostic);
testCase.verifyTrue(false, diagnostic);
end
return;
end
if isa(actual, 'PlotlyTestCaseAny')
matchResult = actual.match(expected);
if ~matchResult.passed
diagnostic = sprintf('Path to failure: %s\n%s', path, matchResult.diagnostic);
testCase.verifyTrue(false, diagnostic);
end
return;
end
bothStructs = isstruct(actual) && isstruct(expected);
onlyOneStruct = isstruct(actual) ~= isstruct(expected);
bothCells = iscell(actual) && iscell(expected);
onlyOneCell = iscell(actual) ~= iscell(expected);
if onlyOneStruct
diagnostic = sprintf('Path to failure: %s\nType mismatch (one is struct, one is not).', path);
testCase.verifyEqual(actual, expected, diagnostic, extraArgs{:});
return;
end
if onlyOneCell
diagnostic = sprintf('Path to failure: %s\nType mismatch (one is cell array, one is not).', path);
testCase.verifyEqual(actual, expected, diagnostic, extraArgs{:});
return;
end
if bothStructs
actualFields = fieldnames(actual);
expectedFields = fieldnames(expected);
if ~isequal(sort(actualFields), sort(expectedFields))
diagnostic = sprintf('Path to failure: %s\nField names do not match.', path);
testCase.verifyEqual(sort(actualFields), sort(expectedFields), diagnostic, extraArgs{:});
return;
end
for i = 1:length(expectedFields)
fieldName = expectedFields{i};
newPath = sprintf('%s.%s', path, fieldName);
testCase.compareHelper(actual.(fieldName), expected.(fieldName), newPath, extraArgs);
end
elseif bothCells
if ~isequal(size(actual), size(expected))
diagnostic = sprintf('Path to failure: %s\nCell array sizes do not match.', path);
testCase.verifyEqual(size(actual), size(expected), diagnostic, extraArgs{:});
return;
end
for i = 1:numel(expected)
newPath = sprintf('%s{%d}', path, i);
testCase.compareHelper(actual{i}, expected{i}, newPath, extraArgs);
end
else
if ~isequal(actual, expected)
diagnostic = sprintf('Path to failure: %s', path);
testCase.verifyEqual(actual, expected, diagnostic, extraArgs{:});
end
end
end
end
methods (Static)
function obj = Any()
obj = PlotlyTestCaseAny();
end
function obj = AnyColorString()
obj = PlotlyTestCaseAnyColorString();
end
function obj = AnyInteger(positiveOnly)
if nargin < 1, positiveOnly = false; end
obj = PlotlyTestCaseAnyInteger(positiveOnly);
end
function obj = AnyNumber(positiveOnly)
if nargin < 1, positiveOnly = false; end
obj = PlotlyTestCaseAnyNumber(positiveOnly);
end
function [absTol, msg] = parseVerifyArgs(varargin)
absTol = 0;
msg = '';
i = 1;
while i <= numel(varargin)
arg = varargin{i};
if ischar(arg)
if i + 1 <= numel(varargin) && strcmp(arg, 'AbsTol')
absTol = varargin{i+1};
i = i + 2;
continue;
elseif i + 1 <= numel(varargin) && strcmp(arg, 'RelTol')
i = i + 2;
continue;
end
msg = arg;
end
i = i + 1;
end
end
function line = readSourceLine(file, lineNum)
%-the source line at lineNum of file, or '' when
%-unreadable-%
line = '';
fid = fopen(file, 'r');
if fid < 0
return;
end
try
for k = 1:lineNum
ln = fgetl(fid);
if ln == -1
ln = '';
break;
end
end
if ischar(ln)
line = ln;
end
fclose(fid);
catch
try fclose(fid); catch, end
end
end
function ok = isEqualCheck(a, b, tol)
if isequal(a, b)
ok = true; return;
end
if isequaln(a, b)
ok = true; return;
end
if isnumeric(a) && isnumeric(b) && isequal(size(a), size(b)) && ~isempty(a)
if any(isnan(a) ~= isnan(b))
ok = false; return;
end
bothNaN = isnan(a) & isnan(b);
d = abs(double(a) - double(b));
d(bothNaN) = 0;
ok = all(d <= tol);
return;
end
ok = false;
end
function s = formatCell(v)
if isempty(v)
s = sprintf('%dx%d empty cell', size(v, 1), size(v, 2));
elseif numel(v) <= 20 && all(cellfun(@ischar, v))
s = '{';
for i = 1:numel(v)
if i > 1, s = [s ', ']; end
s = [s '''' v{i} ''''];
end
s = [s '}'];
else
sz = size(v);
dims = '';
for d = 1:(numel(sz) - 1)
dims = [dims num2str(sz(d)) 'x'];
end
s = [dims num2str(sz(end)) ' cell'];
end
end
function s = formatStruct(v)
flds = fieldnames(v);
if isempty(flds)
s = 'empty struct';
else
s = 'struct(';
for i = 1:numel(flds)
if i > 1, s = [s ', ']; end
s = [s flds{i}];
end
s = [s ')'];
end
end
function s = formatValue(v)
cls = class(v);
sz = size(v);
dimStr = '';
for d = 1:numel(sz)
if d > 1, dimStr = [dimStr 'x']; end
dimStr = [dimStr num2str(sz(d))];
end
dimStr = [dimStr ' '];
if ischar(v)
if isempty(v)
q = char(39);
s = sprintf('%s%s %s%s', dimStr, cls, q, q);
elseif numel(v) < 200
s = sprintf('%s%s ''%s''', dimStr, cls, v);
else
s = sprintf('%s%s', dimStr, cls);
end
elseif isstring(v)
if isscalar(v)
s = sprintf('%s "%s"', cls, char(v));
elseif numel(v) <= 10
parts = cell(1, numel(v));
for j = 1:numel(v)
parts{j} = sprintf('"%s"', char(v(j)));
end
s = sprintf('%s%s [%s]', dimStr, cls, strjoin(parts, ' '));
else
s = sprintf('%s%s', dimStr, cls);
end
elseif isnumeric(v)
if isscalar(v)
s = sprintf('%s%s %s', dimStr, cls, strtrim(sprintf('%.17g', v)));
else
nShow = min(10, numel(v));
parts = cell(1, nShow);
for j = 1:nShow
parts{j} = strtrim(sprintf('%.17g', v(j)));
end
if numel(v) <= 10
s = sprintf('%s%s [%s]', dimStr, cls, strjoin(parts, ' '));
else
s = sprintf('%s%s [%s ...]', dimStr, cls, strjoin(parts, ' '));
end
end
elseif islogical(v) && isscalar(v)
if v, s = sprintf('%s%s true', dimStr, cls); else, s = sprintf('%s%s false', dimStr, cls); end
elseif iscell(v)
s = PlotlyTestCase.formatCell(v);
elseif isstruct(v)
s = PlotlyTestCase.formatStruct(v);
else
s = sprintf('%s%s', dimStr, cls);
end
end
end
end