|
| 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