forked from mathworks/ci-configuration-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestExamples.m
87 lines (69 loc) · 2.97 KB
/
TestExamples.m
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
classdef TestExamples < matlab.unittest.TestCase
% TestExamples contains a set of 4 simple tests:
% 1) an equality test for a non-leap year date
% 2) an equality test for a leap year date
% 3) a negative test for an invalid date format input
% 4) a negative test for a correct date format but an invalid date
% 5) an equality test for a non-leap year date using the alternate
% dateFormat (COMMENTED OUT)
%
% Notes:
% A) A negative test verifies that the code errors/fails in an
% expected way (e.g., the code gives the right error for a
% specific bad input)
% B) The 5th test is included for completeness, but is commented
% out to illustrate missing code coverage in continous
% integration (CI) systems
% Copyright 2022 The MathWorks, Inc.
methods (Test)
function testNonLeapYear(testCase)
% Create non-leap year date of March 1st, 2021
dateStr = "03/01/2021";
% Calculate expected result
dt = datetime(dateStr,"Format","MM/dd/uuuu");
doyExpected = day(dt,"dayofyear");
% Get actual result
doyActual = dayofyear(dateStr);
% Verify that the two are equal
testCase.verifyEqual(doyActual,doyExpected)
end
function testLeapYear(testCase)
% Create leap year date of March 1st, 2020
dateStr = "03/01/2020";
% Calculate expected result
dt = datetime(dateStr,"Format","MM/dd/uuuu");
doyExpected = day(dt,"dayofyear");
% Get actual result
doyActual = dayofyear(dateStr);
% Verify that the two are equal
testCase.verifyEqual(doyActual,doyExpected)
end
function testInvalidDateFormat(testCase)
% Create invalid date of April 1st, 2021
dateStr = "04-01-2021";
% Verify that our function throws an error
testCase.verifyError(@() dayofyear(dateStr),"dayofyear:InvalidDateFormat");
end
function testCorrectDateFormatButInvalidDate(testCase)
% Create invalid date of February 30th, 2021
dateStr = "02/30/2021";
% Verify that our function throws an error
testCase.verifyError(@() dayofyear(dateStr),"MATLAB:datetime:ParseErr");
end
% function testAlternateDateFormat(testCase)
% % Create date of April 1st, 2021 in alternate date format
% dateStr = "01/04/2021";
% dateFormat = "dd/mm/yyyy";
%
% % Calculate expected result
% dt = datetime(dateStr,"Format","dd/MM/uuuu");
% doyExpected = day(dt,"dayofyear");
%
% % Get actual result
% doyActual = dayofyear(dateStr,dateFormat);
%
% % Verify that the two are equal
% testCase.verifyEqual(doyActual,doyExpected)
% end
end
end