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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { DateBuilder } from "./DateBuilder";
describe("DateBuilder", () => {
test("should be able to be initialised", () => {
const db = new DateBuilder();
assert.equal(db instanceof DateBuilder, true);
});
test("should be able to build current date", () => {
const now = new Date();
const db = new DateBuilder();
const built = db.build();
assert.equal(built.getFullYear(), now.getFullYear());
assert.equal(built.getMonth(), now.getMonth());
assert.equal(built.getDate(), now.getDate());
assert.equal(built.getHours(), now.getHours());
assert.equal(built.getMinutes(), now.getMinutes());
assert.equal(built.getSeconds(), now.getSeconds());
});
test("should be able to build timestamp", () => {
const now = new Date();
const db = new DateBuilder();
const built = db.buildTimestamp();
assert.equal(built, now.getTime());
});
test("should be able to add days", () => {
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 1, 2024
db.addDays(30);
const built = db.build();
assert.equal(built.getFullYear(), 2024);
assert.equal(built.getMonth(), 0); // January
assert.equal(built.getDate(), 31); // January has 31 days
});
test("should be able to add months", () => {
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 31, 2024
db.addMonths(1);
const built = db.build();
assert.equal(built.getFullYear(), 2024);
assert.equal(built.getMonth(), 1); // February
});
test("should be able to add years", () => {
const db = new DateBuilder(new Date(2020, 1, 1));
db.addYears(1);
const built = db.build();
assert.equal(built.getFullYear(), 2021);
assert.equal(built.getMonth(), 1); // February
assert.equal(built.getDate(), 1);
});
test("should be able to set date", () => {
const db = new DateBuilder();
db.withDate(2022, 12, 25); // Dec 25, 2022
const built = db.build();
assert.equal(built.getFullYear(), 2022);
assert.equal(built.getMonth(), 11); // December
assert.equal(built.getDate(), 25);
});
test("should be able to set time", () => {
const db = new DateBuilder();
db.withTime(15, 30, 45, 123); // 15:30:45.123
const built = db.build();
assert.equal(built.getHours(), 15);
assert.equal(built.getMinutes(), 30);
assert.equal(built.getSeconds(), 45);
assert.equal(built.getMilliseconds(), 123);
});
test("should be able to set start of day", () => {
const db = new DateBuilder(new Date(2024, 5, 15, 10, 20, 30, 456)); // June 15, 2024, 10:20:30.456
db.atStartOfDay();
const built = db.build();
assert.equal(built.getFullYear(), 2024);
assert.equal(built.getMonth(), 5); // June
assert.equal(built.getDate(), 15);
assert.equal(built.getHours(), 0);
assert.equal(built.getMinutes(), 0);
assert.equal(built.getSeconds(), 0);
assert.equal(built.getMilliseconds(), 0);
});
test("should be able to set end of day", () => {
const db = new DateBuilder(new Date(2024, 5, 15, 10, 20, 30, 456)); // June 15, 2024, 10:20:30.456
db.atEndOfDay();
const built = db.build();
assert.equal(built.getFullYear(), 2024);
assert.equal(built.getMonth(), 5); // June
assert.equal(built.getDate(), 15);
assert.equal(built.getHours(), 23);
assert.equal(built.getMinutes(), 59);
assert.equal(built.getSeconds(), 59);
assert.equal(built.getMilliseconds(), 999);
});
test("should be able to chain methods", () => {
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 1, 2024
db.addDays(1).addMonths(1).addYears(1).withTime(12, 0, 0).atEndOfDay();
const built = db.build();
assert.equal(built.getFullYear(), 2025);
assert.equal(built.getMonth(), 1); // March
assert.equal(built.getDate(), 2);
assert.equal(built.getHours(), 23);
assert.equal(built.getMinutes(), 59);
assert.equal(built.getSeconds(), 59);
assert.equal(built.getMilliseconds(), 999);
});
test("should not mutate original date", () => {
const original = new Date(2024, 0, 1); // Jan 1, 2024
const db = new DateBuilder(original);
db.addDays(10);
const built = db.build();
assert.equal(original.getFullYear(), 2024);
assert.equal(original.getMonth(), 0); // January
assert.equal(original.getDate(), 1); // Original date should remain unchanged
assert.equal(built.getFullYear(), 2024);
assert.equal(built.getMonth(), 0); // January
assert.equal(built.getDate(), 11); // New date should be Jan 11, 2024
});
test("should handle leap years correctly", () => {
const db = new DateBuilder(new Date(2020, 1, 29)); // Feb 29, 2020 (leap year)
db.addYears(1);
const built = db.build();
assert.equal(built.getFullYear(), 2021);
assert.equal(built.getMonth(), 2); // March
assert.equal(built.getDate(), 1); // March 1, 2021 (not a leap year)
});
test("should handle month overflow correctly", () => {
const db = new DateBuilder(new Date(2024, 0, 31)); // Jan 31, 2024
db.addDays(1);
const built = db.build();
assert.equal(built.getFullYear(), 2024);
assert.equal(built.getMonth(), 1); // February
assert.equal(built.getDate(), 1); // Feb 29, 2024 (leap year)
});
});
|