Skip to content

Commit

Permalink
Fix: test cases affected by DST
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Nov 11, 2023
1 parent 43231ca commit 5ebfcc0
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 65 deletions.
6 changes: 3 additions & 3 deletions test/en/en.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ test("Test - Random text", function () {
expect(result.end.get("month")).toBe(7);
});

testSingleCase(chrono, "03-27-2022, 02:00 AM", new Date(2017, 7 - 1, 7), (result) => {
expect(result.text).toBe("03-27-2022, 02:00 AM");
testSingleCase(chrono, "07-27-2022, 02:00 AM", new Date(2017, 7 - 1, 7), (result) => {
expect(result.text).toBe("07-27-2022, 02:00 AM");
expect(result.start.get("day")).toBe(27);
expect(result.start.get("month")).toBe(3);
expect(result.start.get("month")).toBe(7);
expect(result.start.get("year")).toBe(2022);
expect(result.start.get("hour")).toBe(2);
expect(result.start.get("meridiem")).toBe(Meridiem.AM);
Expand Down
29 changes: 29 additions & 0 deletions test/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,32 @@ test("Test - Calendar Checking", () => {
expect(components.isValidDate()).toBe(false);
}
});

test("Test - Checking non-existing date during DST skip", () => {
// Only CET (or CEST) timezones where the DST starts on "Sunday, March 27, 2022" at "02:00 (2 am) local time"
const dateDstPre = new Date(2022, 3 - 1, 27, 2);
const dateDstPost = new Date(2022, 3 - 1, 27, 3);
if (dateDstPre.getTime() == dateDstPost.getTime()) {
const reference = new ReferenceWithTimezone(new Date());

// On "Sunday, March 27, 2022" at "02:00 local time", the clock is moved forward to "03:00 local time".
// Thus, the time between "02:00 and 02:59:59" does not exist.
expect(
new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 2, minute: 0 }).isValidDate()
).toBe(false);
expect(
new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 2, minute: 1 }).isValidDate()
).toBe(false);
expect(
new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 2, minute: 59 }).isValidDate()
).toBe(false);

// Otherwise, it
expect(
new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 1, minute: 59 }).isValidDate()
).toBe(true);
expect(
new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 3, minute: 0 }).isValidDate()
).toBe(true);
}
});
55 changes: 44 additions & 11 deletions test/ru/ru_relative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,103 @@ test("Test - 'This' expressions", () => {
testSingleCase(chrono.ru, "на этой неделе", new Date(2017, 11 - 1, 19, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 11 - 1, 19, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(11);
expect(result.start.get("day")).toBe(19);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в этом месяце", new Date(2017, 11 - 1, 19, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 11 - 1, 1, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(11);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в этом месяце", new Date(2017, 11 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 11 - 1, 1, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(11);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в этом году", new Date(2017, 11 - 1, 19, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 1 - 1, 1, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(1);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});
});

test("Test - Past relative expressions", () => {
testSingleCase(chrono.ru, "на прошлой неделе", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 9 - 1, 24, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(9);
expect(result.start.get("day")).toBe(24);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в прошлом месяце", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 9 - 1, 1, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(9);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в прошлом году", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2015, 10 - 1, 1, 12));
expect(result.start.get("year")).toBe(2015);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});
});

test("Test - Future relative expressions", () => {
testSingleCase(chrono.ru, "на следующей неделе", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 8, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(8);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в следующем месяце", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 11 - 1, 1, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(11);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в следующем квартале", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 1 - 1, 1, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(1);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "в следующем году", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 10 - 1, 1, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});
});
126 changes: 96 additions & 30 deletions test/ru/ru_time_units_casual_relative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,120 +5,186 @@ test("Test - Positive time units", () => {
testSingleCase(chrono.ru, "следующие 2 недели", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 15, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(15);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "следующие 2 дня", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 3, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(3);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "следующие два года", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2018, 10 - 1, 1, 12));
expect(result.start.get("year")).toBe(2018);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "следующие 2 недели 3 дня", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 18, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(18);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "через пару минут", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 1, 12, 2));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
expect(result.start.get("minute")).toBe(2);
});

testSingleCase(chrono.ru, "через полчаса", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 1, 12, 30));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
expect(result.start.get("minute")).toBe(30);
});

testSingleCase(chrono.ru, "через 2 часа", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 1, 14));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(14);
});

testSingleCase(chrono.ru, "спустя 2 часа", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 1, 14));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(14);
});

testSingleCase(chrono.ru, "через три месяца", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2017, 1 - 1, 1, 12));
expect(result.start.get("year")).toBe(2017);
expect(result.start.get("month")).toBe(1);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "через неделю", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 10 - 1, 8, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(8);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "через месяц", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 11 - 1, 1, 12));
});

testSingleCase(chrono.ru, "через год", new Date(2020, 11 - 1, 22, 12, 11, 32, 6), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2021, 11 - 1, 22, 12, 11, 32, 6));
});

testSingleCase(chrono.ru, "спустя год", new Date(2020, 11 - 1, 22, 12, 11, 32, 6), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2021, 11 - 1, 22, 12, 11, 32, 6));
});
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(11);
expect(result.start.get("day")).toBe(1);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(
chrono.ru,
"через год",
{ instant: new Date("2020-11-22T03:11:02.006"), timezone: "GMT" },
(result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date("2021-11-22T03:11:02.006"));
}
);
testSingleCase(
chrono.ru,
"через год",
{ instant: new Date("2020-11-22T12:11:32.006"), timezone: "GMT" },
(result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date("2021-11-22T12:11:32.006"));
}
);
});

test("Test - Negative time units", () => {
testSingleCase(chrono.ru, "прошлые 2 недели", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 9 - 1, 17, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(9);
expect(result.start.get("day")).toBe(17);
expect(result.start.get("hour")).toBe(12);
});

testSingleCase(chrono.ru, "прошлые два дня", new Date(2016, 10 - 1, 1, 12), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2016, 9 - 1, 29, 12));
expect(result.start.get("year")).toBe(2016);
expect(result.start.get("month")).toBe(9);
expect(result.start.get("day")).toBe(29);
expect(result.start.get("hour")).toBe(12);
});
});

test("Test - Plus '+' sign", () => {
testSingleCase(chrono.ru.casual, "+15 минут", new Date(2012, 7 - 1, 10, 12, 14), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2012, 7 - 1, 10, 12, 29));
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(7);
expect(result.start.get("day")).toBe(10);
expect(result.start.get("hour")).toBe(12);
expect(result.start.get("minute")).toBe(29);
});

testSingleCase(chrono.ru.casual, "+15мин", new Date(2012, 7 - 1, 10, 12, 14), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2012, 7 - 1, 10, 12, 29));
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(7);
expect(result.start.get("day")).toBe(10);
expect(result.start.get("hour")).toBe(12);
expect(result.start.get("minute")).toBe(29);
});

testSingleCase(chrono.ru.casual, "+1 день 2 часа", new Date(2012, 7 - 1, 10, 12, 14), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2012, 7 - 1, 11, 14, 14));
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(7);
expect(result.start.get("day")).toBe(11);
expect(result.start.get("hour")).toBe(14);
expect(result.start.get("minute")).toBe(14);
});
});

test("Test - Minus '-' sign", () => {
testSingleCase(chrono.ru.casual, "-3 года", new Date(2015, 7 - 1, 10, 12, 14), (result, text) => {
expect(result.index).toBe(0);
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2012, 7 - 1, 10, 12, 14));
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(7);
expect(result.start.get("day")).toBe(10);
expect(result.start.get("hour")).toBe(12);
expect(result.start.get("minute")).toBe(14);
});
});
Loading

0 comments on commit 5ebfcc0

Please sign in to comment.