Skip to content

Commit

Permalink
Fix SmokeTests for Arrays and Algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
eszmw committed Jun 24, 2024
1 parent 242999a commit d6eb22a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 76 deletions.
Binary file modified Scripts/Algorithms.mlx
Binary file not shown.
164 changes: 88 additions & 76 deletions SoftwareTests/SmokeTests.m
Original file line number Diff line number Diff line change
@@ -1,108 +1,120 @@
% Run these tests with runMyTests
% All tests so far are on code expected to run without errors
% If/when we end up with a version that _should_ error,
% please add it to this set of examples
classdef SmokeTests < matlab.unittest.TestCase

properties (ClassSetupParameter)
Project = {''};
end

properties
rootProject
results
properties (TestParameter)
Scripts;
end

methods (TestParameterDefinition,Static)

methods (TestClassSetup)
function Scripts = GetScriptName(Project)
RootFolder = currentProject().RootFolder;
Scripts = dir(fullfile(RootFolder,"Scripts","*.mlx"));
Scripts = {Scripts.name};
end

function setUpPath(testCase)
end

methods (TestClassSetup)

function SetUpSmokeTest(testCase,Project)
try
project = currentProject;
testCase.rootProject = project.RootFolder;
cd(testCase.rootProject)
catch
error("Load project prior to run tests")
currentProject;
catch ME
warning("Project is not loaded.")
end
end

testCase.log("Running in " + version)

end

end % function setUpPath

function setUpResults(testCase)
files = dir(fullfile(testCase.rootProject,"Scripts","*.mlx"));
testCase.results = struct;
testCase.results.Name = strings(size(files));
testCase.results.Passed = false(size(files));
testCase.results.Time = zeros(size(files));
testCase.results.Message = strings(size(files));
for k = 1:length(files)
testCase.results.Name(k) = string(files(k).name);
end

methods(Test)

end % function setUpResults
function SmokeRun(testCase,Scripts)
Filename = string(Scripts);
switch (Filename)
case "Introduction.mlx"
SmokeTestWithError(testCase,Filename,"Unrecognized function or variable 'Monday'.")
otherwise
SimpleSmokeTest(testCase,Filename)
end
end

end

end % methods (TestClassSetup)

methods(Test)
methods (Access = private)

function smokeTest(testCase)
myFiles = testCase.results.Name;
fid = fopen(fullfile("SoftwareTests","TestResults_"+release_version+".txt"),"w");
fprintf(fid,"Version,File,Status,ElapsedTime\n");
for kTest = 1:length(myFiles)
[flag,msg,time] = SmokeTestMyFile(myFiles(kTest));
testCase.results.Time(kTest) = time;
if flag
testCase.results.Time(kTest) = toc;
disp("Failed " + myFiles(kTest) + " because " + ...
newline + msg)
testCase.results.Message(kTest) = msg;
fprintf(fid,"%s,%s,%s,%s\n",release_version,myFiles(kTest),"failed",testCase.results.Time(kTest));
function SmokeTestWithError(testCase,Filename,MyError)
% Run the Smoke test
RootFolder = currentProject().RootFolder;
cd(RootFolder)
disp(">> Running " + Filename);
try
run(fullfile("Scripts",Filename));
catch ME
if ME.message == MyError
MyErrorMessage = "Expected error.";
else
disp("Finished " + myFiles(kTest))
testCase.results.Passed(kTest) = true;
fprintf(fid,"%s,%s,%s,%s\n",release_version,myFiles(kTest),"passed",testCase.results.Time(kTest));
MyErrorMessage = ME.message;
testCase.verifyTrue(false,ME.message);
end
end

% Log the opened figures to the test reports
Figures = findall(groot,'Type','figure');
Figures = flipud(Figures);
if ~isempty(Figures)
for f = 1:size(Figures,1)
FigDiag = matlab.unittest.diagnostics.FigureDiagnostic(Figures(f));
log(testCase,1,FigDiag);
end
end
fclose(fid);
struct2table(testCase.results)
assert(all([testCase.results.Passed]))
close all

end

function SimpleSmokeTest(testCase,Filename)

% Run the Smoke test
RootFolder = currentProject().RootFolder;
cd(RootFolder)
disp(">> Running " + Filename);
try
run(fullfile("Scripts",Filename));
catch ME
testCase.verifyTrue(false,ME.message);
end

% Log the opened figures to the test reports
Figures = findall(groot,'Type','figure');
Figures = flipud(Figures);
if ~isempty(Figures)
for f = 1:size(Figures,1)
try
FigDiag = matlab.unittest.diagnostics.FigureDiagnostic(Figures(f));
log(testCase,1,FigDiag);
catch ME
disp("Figures not logged ...")
end
end
end
close all

end
end

methods (TestClassTeardown)

function closeAllFigure(testCase)
close all force % Close figure windows
bdclose all % Close Simulink models
end

end % methods (TestClassTeardown)

end

function [flag,MyErrorMessage,time] = SmokeTestMyFile(myFile)
try
disp("Running " + myFile)
tic
run(myFile)
time = toc;
catch ME
time = toc;
if myFile == "Introduction.mlx"
if ME.message == "Unrecognized function or variable 'Monday'."
flag = 0;
MyErrorMessage = "Expected error.";
else
flag = 1;
MyErrorMessage = ME.message;
end
else
flag = 1;
MyErrorMessage = ME.message;
end
end
if ~exist("flag","var")
flag = 0;
MyErrorMessage = "";
end
end

0 comments on commit d6eb22a

Please sign in to comment.