Website Summary Files Commits Branches @commit @diff @stats
path: root / tests / Test_DateTime.cpp
blob: 0bbca144bc956418688ca34f88d7bfb90be5519a (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdexcept>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "../doctest/doctest/doctest.h"
#include "../src/DateTime/DateTime.h"

TEST_SUITE_BEGIN("ClassDateTime");

const unsigned short h = 5, m = 12, d = 19, M = 4, y = 2022; // valid values

TEST_SUITE("Constructors") {
	TEST_CASE("Ctor_SavesValues_WhenGivenValidValues") {
		DateTime dt(h, m, d, M, y);

		CHECK_EQ(dt.get_hour(), h);
		CHECK_EQ(dt.get_minute(), m);
		CHECK_EQ(dt.get_day(), d);
		CHECK_EQ(dt.get_month(), M);
		CHECK_EQ(dt.get_year(), y);
	}
	TEST_CASE("Ctor_ThrowsInvalidArgumentException_WhenGivenInvalidValues") {
		unsigned short nh = h, nm = m, nd = d, nM = M, ny = y;
		SUBCASE("HourIsTooBig") { nh = 194; }
		SUBCASE("MinutesAreTooBig") { nm = 7032; }
		SUBCASE("DayIsZero") { nd = 0; }
		SUBCASE("DayIsTooBig") { nd = 5721; }
		SUBCASE("MonthIsZero") { nM = 0; }
		SUBCASE("MonthIsTooBig") { nM = 15661; }

		CHECK_THROWS_AS(DateTime dt(nh, nm, nd, nM, ny), std::invalid_argument);
	}
}

TEST_SUITE("GettersAndSetters") {
	TEST_CASE("HourGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
		DateTime dt(h, m, d, M, y);

		CHECK_EQ(dt.get_hour(), h);
	}
	TEST_CASE("HourSetter_ReturnTrue_WhenGivenCorrectValue") {
		DateTime dt(h, m, d, M, y);
		unsigned nh = h;
		SUBCASE("SingleDigits") { nh = 2; }
		SUBCASE("DoubleDigits") { nh = 10; }
		SUBCASE("24hour") { nh = 17; }
		SUBCASE("Zero") { nh = 0; }

		CHECK_UNARY(dt.set_hour(nh));
	}
	TEST_CASE("HourSetter_SetsValue_WhenGivenCorrectValue") {
		DateTime dt(h, m, d, M, y);
		unsigned nh = h;
		SUBCASE("SingleDigits") { nh = 2; }
		SUBCASE("DoubleDigits") { nh = 10; }
		SUBCASE("24hour") { nh = 17; }
		SUBCASE("Zero") { nh = 0; }
		dt.set_hour(nh);

		CHECK_EQ(dt.get_hour(), nh);
	}
	TEST_CASE("HourSetter_ReturnsFalse_WhenGivenIncorrectValue") {
		DateTime dt(h, m, d, M, y);
		unsigned nh = h;
		SUBCASE("TooBig") { nh = 891; }

		CHECK_UNARY_FALSE(dt.set_hour(nh));
	}

	TEST_CASE("MinuteGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
		DateTime dt(h, m, d, M, y);

		CHECK_EQ(dt.get_minute(), h);
	}
	TEST_CASE("MinuteSetter_ReturnTrue_WhenGivenCorrectValue") {
		DateTime dt(h, m, d, M, y);
		unsigned nm = m;
		SUBCASE("SingleDigits") { nm = 2; }
		SUBCASE("DoubleDigits") { nm = 52; }
		SUBCASE("Zero") { nm = 0; }

		CHECK_UNARY(dt.set_minute(nm));
	}
	TEST_CASE("MinuteSetter_SetsValue_WhenGivenCorrectValue") {
		DateTime dt(h, m, d, M, y);
		unsigned nm = m;
		SUBCASE("SingleDigits") { nm = 2; }
		SUBCASE("DoubleDigits") { nm = 42; }
		SUBCASE("Zero") { nm = 0; }
		dt.set_minute(nm);

		CHECK_EQ(dt.get_minute(), nm);
	}
	TEST_CASE("MinuteSetter_ReturnsFalse_WhenGivenIncorrectValue") {
		DateTime dt(h, m, d, M, y);
		unsigned nm = m;
		SUBCASE("TooBig") { nm = 847; }

		CHECK_UNARY_FALSE(dt.set_minute(nm));
	}
}

TEST_SUITE_END();