Skip to content

Commit a413836

Browse files
committed
add more tests and update view
1 parent 82f871c commit a413836

15 files changed

Lines changed: 386 additions & 3 deletions

File tree

Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cd(getenv('MW_ORIG_WORKING_FOLDER'));
2+
import matlab.unittest.TestRunner; addpath(genpath('code')); addpath(genpath('tests')); suite = testsuite(pwd, 'IncludeSubfolders', true); runner = TestRunner.withDefaultPlugins(); results = runner.run(suite); display(results); assertSuccess(results);

sample/visualization/buildfile.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
% function plan = buildfile
2+
% plan = buildplan(localfunctions);
3+
% plan("test").Dependencies = "build";
4+
% plan("deploy").Dependencies = "test";
5+
%
6+
% plan.DefaultTasks = "test";
7+
%
8+
% function buildTask(~)
9+
% f = fopen('buildlog.txt', 'a+'); fprintf(f, 'building\n'); fclose(f);
10+
%
11+
% function testTask(~,tests,options)
12+
% arguments
13+
% ~
14+
% tests string = pwd
15+
% options.OutputDetail (1,1) string = "terse"
16+
% end
17+
% f = fopen('buildlog.txt', 'a+');
18+
% fprintf(f, 'testing\n');
19+
% fprintf(f, '%s\n', tests);
20+
% fprintf(f, '%s\n', options.OutputDetail);
21+
% fclose(f);
22+
%
23+
% function deployTask(~)
24+
% f = fopen('buildlog.txt', 'a+'); fprintf(f, 'deploying\n'); fclose(f);
25+
%
26+
% function checkTask(~)
27+
% f = fopen('buildlog.txt', 'a+'); fprintf(f, 'checking\n'); fclose(f);
28+
29+
%%
30+
31+
function plan = buildfile
32+
import matlab.buildtool.tasks.TestTask
33+
34+
% Create a plan with no tasks
35+
plan = buildplan(localfunctions);
36+
plan.DefaultTasks = "test";
37+
38+
% Add a task to run the tests in the project
39+
plan("test") = TestTask;
40+
end
41+
42+
function myTask(~)
43+
disp('Hello World!')
44+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function doy = dayofyear(mmddyy,dateFormat)
2+
%DAYOFYEAR Converts a date string ("mm/dd/yyyy") to the day number of the
3+
%year.
4+
5+
% NOTE: MATLAB already does easily this using:
6+
% doy = day(d,"dayofyear")
7+
% where d is a datetime object
8+
9+
% Copyright 2022 The MathWorks, Inc.
10+
11+
arguments
12+
mmddyy string;
13+
dateFormat (1,1) string {mustBeMember(dateFormat,["mm/dd/yyyy","dd/mm/yyyy"])} = "mm/dd/yyyy";
14+
end
15+
16+
% Check that mmddyy was provided in the appropriate format
17+
if numel(split(mmddyy,"/")) ~= 3
18+
error("dayofyear:InvalidDateFormat","Invalid date string. Expected date formatted as dd/mm/yyyy.")
19+
end
20+
21+
% Create a datetime object depending on the dateFormat provided
22+
if dateFormat == "mm/dd/yyyy"
23+
d = datetime(mmddyy,"Format","MM/dd/uuuu");
24+
else
25+
d = datetime(mmddyy,"Format","dd/MM/uuuu");
26+
end
27+
28+
% Initialize the days per month
29+
daysPerMonth = [ ...
30+
31; % January
31+
28; % February
32+
31; % March
33+
30; % April
34+
31; % May
35+
30; % June
36+
31; % July
37+
31; % August
38+
30; % September
39+
31; % October
40+
30; % November
41+
31]; % December
42+
43+
% Check for leap year
44+
if mod(d.Year,4) == 0
45+
% This is a leap year, so change February to 29 days
46+
daysPerMonth(2) = 29;
47+
end
48+
49+
% Calculate day of year
50+
doy = sum(daysPerMonth(1:d.Month-1)) + d.Day;
51+
52+
53+
end
54+

sample/visualization/myruntests.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import matlab.unittest.TestRunner;
2+
addpath(genpath('code'));
3+
addpath(genpath('tests'));
4+
suite = testsuite(pwd, 'IncludeSubfolders', true);
5+
runner = TestRunner.withDefaultPlugins();
6+
plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat("results.xml");
7+
addPlugin(runner,plugin)
8+
results = runner.run(suite);
9+
display(results);
10+
assertSuccess(results);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import matlab.unittest.TestRunner;
2+
addpath(genpath('code'));
3+
addpath(genpath('tests'));
4+
suite = testsuite([pwd, '/tests']);
5+
runner = TestRunner.withDefaultPlugins();
6+
plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat("results.xml");
7+
addPlugin(runner,plugin)
8+
results = runner.run(suite);
9+
display(results);
10+
assertSuccess(results);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
classdef CITestRunnerPluginService < matlab.unittest.internal.services.plugins.TestRunnerPluginService
2+
% CITestRunnerPluginService - Interface for services that provide TestRunnerPlugins
3+
% for CI only.
4+
%
5+
% See Also: matlab.unittest.internal.services.plugins.TestRunnerPluginService,
6+
% matlab.unittest.internal.services.Service,
7+
% matlab.automation.internal.services.ServiceLocator,
8+
% matlab.unittest.internal.services.ServiceFactory
9+
10+
% Copyright 2024 The MathWorks, Inc.
11+
end
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
classdef ParameterizedTestExample < matlab.unittest.TestCase
2+
% Creates 12 test points, one test point for the 15th day of every month of 2021
3+
4+
% Copyright 2022 The MathWorks, Inc.
5+
6+
properties (TestParameter)
7+
monthNum = num2cell(1:12);
8+
dayNum = {15};
9+
yearNum = {2021};
10+
end
11+
12+
methods (Test, TestTags = {'FeatureA'})
13+
function testDayofyear(testCase,monthNum,dayNum,yearNum)
14+
% Convert numeric values to mm/dd/yyyy string
15+
% Note: MATLAB will automatically convert numbers to strings
16+
% when performing number+string arithmetic
17+
% fatalAssertEqual(7, 20);
18+
dateStr = monthNum + "/" + dayNum + "/" + yearNum;
19+
20+
% Compute expected result
21+
dt = datetime(dateStr,"Format","MM/dd/uuuu");
22+
doyExpected = day(dt,"dayofyear");
23+
24+
% Compute actual result
25+
doyActual = dayofyear(dateStr);
26+
27+
% Verify that the actual result matches the expected result
28+
% assertEqual(testCase, 1, 2);
29+
testCase.verifyEqual(doyActual,doyExpected)
30+
end
31+
end
32+
33+
end
34+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
classdef TestExamples < matlab.unittest.TestCase
2+
% TestExamples contains a set of 4 simple tests:
3+
% 1) an equality test for a non-leap year date
4+
% 2) an equality test for a leap year date
5+
% 3) a negative test for an invalid date format input
6+
% 4) a negative test for a correct date format but an invalid date
7+
% 5) an equality test for a non-leap year date using the alternate
8+
% dateFormat (COMMENTED OUT)
9+
%
10+
% Notes:
11+
% A) A negative test verifies that the code errors/fails in an
12+
% expected way (e.g., the code gives the right error for a
13+
% specific bad input)
14+
% B) The 5th test is included for completeness, but is commented
15+
% out to illustrate missing code coverage in continous
16+
% integration (CI) systems
17+
18+
% Copyright 2022 The MathWorks, Inc.
19+
20+
methods (Test, TestTags = {'FeatureB'})
21+
22+
function testNonLeapYear(testCase)
23+
% Create non-leap year date of March 1st, 2021
24+
dateStr = "03/01/2021";
25+
26+
% Calculate expected result
27+
dt = datetime(dateStr,"Format","MM/dd/uuuu");
28+
doyExpected = day(dt,"dayofyear");
29+
30+
% Get actual result
31+
doyActual = dayofyear(dateStr);
32+
33+
% Verify that the two are equal
34+
testCase.verifyEqual(doyActual,doyExpected)
35+
% testCase.fatalAssertEqual(1,2)
36+
% fatalAssertEqual(7, 20);
37+
end
38+
39+
function testLeapYear(testCase)
40+
% Create leap year date of March 1st, 2020
41+
dateStr = "03/01/2020";
42+
43+
% Calculate expected result
44+
dt = datetime(dateStr,"Format","MM/dd/uuuu");
45+
doyExpected = day(dt,"dayofyear");
46+
47+
% Get actual result
48+
doyActual = dayofyear(dateStr);
49+
50+
% Verify that the two are equal
51+
testCase.verifyEqual(doyActual,doyExpected)
52+
end
53+
54+
function testInvalidDateFormat(testCase)
55+
% Create invalid date of April 1st, 2021
56+
dateStr = "04-01-2021";
57+
58+
% Verify that our function throws an error
59+
testCase.verifyError(@() dayofyear(dateStr),"dayofyear:InvalidDateFormat");
60+
end
61+
62+
function testCorrectDateFormatButInvalidDate(testCase)
63+
% Create invalid date of February 30th, 2021
64+
dateStr = "02/30/2021";
65+
66+
% Verify that our function throws an error
67+
testCase.verifyError(@() dayofyear(dateStr),"MATLAB:datetime:ParseErr");
68+
end
69+
70+
function testAlternateDateFormat(testCase)
71+
% Create date of April 1st, 2021 in alternate date format
72+
dateStr = "01/04/2021";
73+
dateFormat = "dd/mm/yyyy";
74+
75+
% Calculate expected result
76+
dt = datetime(dateStr,"Format","dd/MM/uuuu");
77+
doyExpected = day(dt,"dayofyear");
78+
79+
% Get actual result
80+
doyActual = dayofyear(dateStr,dateFormat);
81+
82+
% Verify that the two are equal
83+
testCase.verifyEqual(doyActual,doyExpected)
84+
end
85+
86+
end
87+
88+
end
89+

0 commit comments

Comments
 (0)