Skip to content

Commit 53bbcd0

Browse files
committed
Improve unit test coverage for HDate
1 parent 0e809a5 commit 53bbcd0

File tree

3 files changed

+96
-20
lines changed

3 files changed

+96
-20
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"docs": "typedoc",
3535
"clean": "gts clean",
3636
"compile": "tsc",
37+
"coverage": "jest --coverage",
3738
"fix": "gts fix",
3839
"prepare": "npm run build",
3940
"pretest": "npm run build",

src/hdate.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,15 @@ export class HDate {
454454
this.getMonth(),
455455
this.getFullYear() + amount
456456
);
457-
} else if (units === UNITS_MONTH) {
457+
} else {
458+
// units === UNITS_MONTH
458459
let hd = new HDate(this);
459460
const sign = amount > 0 ? 1 : -1;
460461
amount = Math.abs(amount);
461462
for (let i = 0; i < amount; i++) {
462463
hd = new HDate(hd.abs() + sign * hd.daysInMonth());
463464
}
464465
return hd;
465-
} else {
466-
throw new TypeError(`Invalid units '${units}'`);
467466
}
468467
}
469468

@@ -507,9 +506,6 @@ export class HDate {
507506
* @param other Hebrew date to compare
508507
*/
509508
deltaDays(other: HDate): number {
510-
if (!HDate.isHDate(other)) {
511-
throw new TypeError(`Bad argument: ${other}`);
512-
}
513509
return this.abs() - other.abs();
514510
}
515511

@@ -522,12 +518,7 @@ export class HDate {
522518
* hd1.isSameDate(hd2); // true
523519
*/
524520
isSameDate(other: HDate): boolean {
525-
if (HDate.isHDate(other)) {
526-
return (
527-
this.yy === other.yy && this.mm === other.mm && this.dd === other.dd
528-
);
529-
}
530-
return false;
521+
return this.yy === other.yy && this.mm === other.mm && this.dd === other.dd;
531522
}
532523

533524
/**

test/hdate.spec.ts

+92-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ test('daysInMonth', () => {
2525
expect(HDate.daysInMonth(KISLEV, 5784)).toBe(29);
2626
});
2727

28+
test('daysInMonth-instance', () => {
29+
expect(new HDate(3, IYYAR, 5780).daysInMonth()).toBe(29);
30+
expect(new HDate(3, SIVAN, 5780).daysInMonth()).toBe(30);
31+
expect(new HDate(3, CHESHVAN, 5782).daysInMonth()).toBe(29);
32+
expect(new HDate(3, CHESHVAN, 5783).daysInMonth()).toBe(30);
33+
expect(new HDate(3, KISLEV, 5783).daysInMonth()).toBe(30);
34+
expect(new HDate(3, KISLEV, 5784).daysInMonth()).toBe(29);
35+
});
36+
2837
test('ctor-mdy', () => {
2938
let d = new HDate(29, CHESHVAN, 5769);
3039
let dt = d.greg(); // 2008-11-27
@@ -115,6 +124,42 @@ test('ctor-copy', () => {
115124
expect(d5.abs()).toBe(d6.abs());
116125
});
117126

127+
test('isLeapYear', () => {
128+
expect(new HDate(15, 'Cheshvan', 5783).isLeapYear()).toBe(false);
129+
expect(new HDate(15, 'Cheshvan', 5784).isLeapYear()).toBe(true);
130+
});
131+
132+
test('isLeapYear-static', () => {
133+
expect(HDate.isLeapYear(5783)).toBe(false);
134+
expect(HDate.isLeapYear(5784)).toBe(true);
135+
});
136+
137+
test('monthsInYear', () => {
138+
expect(HDate.monthsInYear(5783)).toBe(12);
139+
expect(HDate.monthsInYear(5784)).toBe(13);
140+
});
141+
142+
test('daysInYear', () => {
143+
expect(HDate.daysInYear(5783)).toBe(355);
144+
expect(HDate.daysInYear(5784)).toBe(383);
145+
});
146+
147+
test('longCheshvan', () => {
148+
expect(HDate.longCheshvan(5783)).toBe(true);
149+
expect(HDate.longCheshvan(5784)).toBe(false);
150+
});
151+
152+
test('shortKislev', () => {
153+
expect(HDate.shortKislev(5783)).toBe(false);
154+
expect(HDate.shortKislev(5784)).toBe(true);
155+
});
156+
157+
test('throws-ctor-1-NaN', () => {
158+
expect(() => {
159+
new HDate(NaN);
160+
}).toThrow('HDate called with bad argument: NaN');
161+
});
162+
118163
test('throws-ctor-2', () => {
119164
expect(() => {
120165
new HDate(17, 'Cheshvan');
@@ -135,6 +180,21 @@ test('throws-ctor-NaN', () => {
135180
}).toThrow( 'Invalid month number: NaN');
136181
});
137182

183+
test('getTishreiMonth', () => {
184+
const hd = new HDate(15, 'Cheshvan', 5769);
185+
expect(hd.getTishreiMonth()).toBe(2);
186+
});
187+
188+
test('hebrew2abs', () => {
189+
expect(HDate.hebrew2abs(5769, CHESHVAN, 15)).toBe(733359);
190+
});
191+
192+
test('throws-invalid-units', () => {
193+
expect(() => {
194+
new HDate(NaN, 'Sivan', 5780);
195+
}).toThrow('HDate called with bad day argument: NaN');
196+
197+
});
138198

139199
test('toString', () => {
140200
const d = new HDate(new Date(1751, 0, 1));
@@ -153,7 +213,7 @@ test('renderGematriya-suppressNikud', () => {
153213
});
154214

155215
test('render', () => {
156-
const hd = new HDate(15, months.CHESHVAN, 5769);
216+
const hd = new HDate(15, CHESHVAN, 5769);
157217
expect(hd.render('')).toBe('15th of Cheshvan, 5769');
158218
expect(hd.render('en')).toBe('15th of Cheshvan, 5769');
159219
expect(hd.render('s')).toBe('15th of Cheshvan, 5769');
@@ -170,7 +230,7 @@ test('render', () => {
170230
});
171231

172232
test('render-shvat', () => {
173-
const hd = new HDate(15, months.SHVAT, 5789);
233+
const hd = new HDate(15, SHVAT, 5789);
174234
expect(hd.render('')).toBe('15th of Sh’vat, 5789');
175235
expect(hd.render('en')).toBe('15th of Sh’vat, 5789');
176236
expect(hd.render('s')).toBe('15th of Sh’vat, 5789');
@@ -185,7 +245,7 @@ test('render-shvat', () => {
185245

186246

187247
test('render-tevet-ashkenazi', () => {
188-
const hd = new HDate(3, months.TEVET, 5769);
248+
const hd = new HDate(3, TEVET, 5769);
189249
expect(hd.render('en', false)).toBe('3rd of Tevet');
190250
expect(hd.render('s', false)).toBe('3rd of Tevet');
191251
expect(hd.render('ashkenazi', false)).toBe('3rd of Teves');
@@ -226,6 +286,18 @@ test('monthFromName', () => {
226286
HDate.monthFromName(sample);
227287
}).toThrow(`Unable to parse month name: ${sample}`);
228288
}
289+
290+
expect(() => {
291+
HDate.monthFromName(25);
292+
}).toThrow(`Invalid month name: 25`);
293+
294+
expect(() => {
295+
HDate.monthFromName(-1);
296+
}).toThrow(`Invalid month name: -1`);
297+
298+
expect(() => {
299+
HDate.monthFromName(NaN);
300+
}).toThrow(`Invalid month name: NaN`);
229301
});
230302

231303
test('getMonthName-throws', () => {
@@ -336,6 +408,11 @@ test('add', () => {
336408
expect(hd.getDate()).toBe(7);
337409
expect(hd.getFullYear()).toBe(5769);
338410

411+
hd = cheshvan29.add(1, 'w');
412+
expect(hd.getMonth()).toBe(KISLEV);
413+
expect(hd.getDate()).toBe(7);
414+
expect(hd.getFullYear()).toBe(5769);
415+
339416
hd = cheshvan29.add(-3, 'Days');
340417
expect(hd.getMonth()).toBe(CHESHVAN);
341418
expect(hd.getDate()).toBe(26);
@@ -427,12 +504,13 @@ test('deltaDays', () => {
427504
});
428505

429506
test('throws-invalid-units', () => {
430-
507+
const hd = new HDate(29, CHESHVAN, 5769);
431508
expect(() => {
432-
433-
const hd = new HDate(29, CHESHVAN, 5769);
434-
hd.add(1, 'foobar');
435-
}).toThrow('Invalid units \'foobar\'');
509+
hd.add(1, 'x');
510+
}).toThrow('Invalid units \'x\'');
511+
expect(() => {
512+
hd.subtract(1, 'zs');
513+
}).toThrow('Invalid units \'zs\'');
436514
});
437515

438516
test('fromGematriyaString', () => {
@@ -453,6 +531,12 @@ test('fromGematriyaString whitespace', () => {
453531
expect(HDate.fromGematriyaString('ה באדר א תשי"ט ').toString()).toBe('5 Adar I 5719');
454532
});
455533

534+
test('fromGematriyaString-throws', () => {
535+
expect(() => {
536+
HDate.fromGematriyaString('abc def ghi jkl mno pqr');
537+
}).toThrow('Unable to parse gematriya string: "abc def ghi jkl mno pqr"');
538+
});
539+
456540
test('HDate-rollover-leap', () => {
457541
const hd = new HDate(30, IYYAR, 5784);
458542
expect(hd.getFullYear()).toBe(5784);

0 commit comments

Comments
 (0)