Skip to content

Commit 8113cb3

Browse files
PlotlyTestCase: show the failing assertion's file, line and source line in diagnostics
1 parent 96e31b6 commit 8113cb3

1 file changed

Lines changed: 47 additions & 3 deletions

File tree

‎plotly/testing/PlotlyTestCase.m‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ function recordPass(testCase)
4848
function recordFail(testCase, diagnostic)
4949
testCase.Failed = testCase.Failed + 1;
5050
testCase.p_curFailed = testCase.p_curFailed + 1;
51-
testCase.p_curDiagnostics{end+1} = diagnostic;
51+
diag = [diagnostic testCase.callSiteInfo()];
5252
if isempty(testCase.CurrentTest)
53-
testCase.Failures{end+1} = diagnostic;
53+
testCase.Failures{end+1} = diag;
5454
else
55-
testCase.Failures{end+1} = [testCase.CurrentTest ': ' diagnostic];
55+
testCase.Failures{end+1} = [testCase.CurrentTest ': ' diag];
5656
end
57+
testCase.p_curDiagnostics{end+1} = diag;
5758
end
5859

5960
function verifyEqual(testCase, actual, expected, varargin)
@@ -170,6 +171,24 @@ function verifyLessThan(testCase, value, ceiling, varargin)
170171
end
171172

172173
methods (Access = private)
174+
function info = callSiteInfo(testCase)
175+
%-the first stack frame outside this class file: the
176+
%-assertion's location in the test file-%
177+
info = '';
178+
st = dbstack;
179+
thisFile = [mfilename('fullpath') '.m'];
180+
for i = 2:numel(st)
181+
if ~isempty(st(i).file) && ~strcmp(st(i).file, thisFile)
182+
info = sprintf('\n at %s:%d', st(i).file, st(i).line);
183+
line = PlotlyTestCase.readSourceLine(st(i).file, st(i).line);
184+
if ~isempty(line)
185+
info = sprintf('%s\n %s', info, strtrim(line));
186+
end
187+
return;
188+
end
189+
end
190+
end
191+
173192
function compareHelper(testCase, actual, expected, path, extraArgs)
174193
if isa(expected, 'PlotlyTestCaseAny')
175194
matchResult = expected.match(actual);
@@ -273,6 +292,31 @@ function compareHelper(testCase, actual, expected, path, extraArgs)
273292
end
274293
end
275294

295+
function line = readSourceLine(file, lineNum)
296+
%-the source line at lineNum of file, or '' when
297+
%-unreadable-%
298+
line = '';
299+
fid = fopen(file, 'r');
300+
if fid < 0
301+
return;
302+
end
303+
try
304+
for k = 1:lineNum
305+
ln = fgetl(fid);
306+
if ln == -1
307+
ln = '';
308+
break;
309+
end
310+
end
311+
if ischar(ln)
312+
line = ln;
313+
end
314+
fclose(fid);
315+
catch
316+
try fclose(fid); catch, end
317+
end
318+
end
319+
276320
function ok = isEqualCheck(a, b, tol)
277321
if isequal(a, b)
278322
ok = true; return;

0 commit comments

Comments
 (0)