mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
chore(datetime): use FakeTime for testing parse (#2109)
This commit is contained in:
parent
403f226a20
commit
bcec5f9d8b
9
.github/workflows/ci.yml
vendored
9
.github/workflows/ci.yml
vendored
@ -28,18 +28,9 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
deno-version: ${{ matrix.deno }}
|
deno-version: ${{ matrix.deno }}
|
||||||
|
|
||||||
- name: Install fakedate
|
|
||||||
if: matrix.os == 'ubuntu-latest'
|
|
||||||
run: |
|
|
||||||
sudo apt install faketime
|
|
||||||
|
|
||||||
- name: Run tests canary
|
- name: Run tests canary
|
||||||
run: deno task test
|
run: deno task test
|
||||||
|
|
||||||
- name: Run test with the date 2021-12-31
|
|
||||||
if: matrix.os == 'ubuntu-latest'
|
|
||||||
run: deno task test:faketime
|
|
||||||
|
|
||||||
- name: Type check browser compatible modules
|
- name: Type check browser compatible modules
|
||||||
shell: bash
|
shell: bash
|
||||||
run: deno task test:browser
|
run: deno task test:browser
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||||
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
||||||
|
import { FakeTime } from "../testing/time.ts";
|
||||||
import * as datetime from "./mod.ts";
|
import * as datetime from "./mod.ts";
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
@ -64,6 +65,75 @@ Deno.test({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("[std/datetime] parse: The date is 2021-12-31", () => {
|
||||||
|
const time = new FakeTime("2021-12-31");
|
||||||
|
try {
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("01-01", "MM-dd"),
|
||||||
|
new Date(2021, 0, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("02-01", "MM-dd"),
|
||||||
|
new Date(2021, 1, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("03-01", "MM-dd"),
|
||||||
|
new Date(2021, 2, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("04-01", "MM-dd"),
|
||||||
|
new Date(2021, 3, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("05-01", "MM-dd"),
|
||||||
|
new Date(2021, 4, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("06-01", "MM-dd"),
|
||||||
|
new Date(2021, 5, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("07-01", "MM-dd"),
|
||||||
|
new Date(2021, 6, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("08-01", "MM-dd"),
|
||||||
|
new Date(2021, 7, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("09-01", "MM-dd"),
|
||||||
|
new Date(2021, 8, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("10-01", "MM-dd"),
|
||||||
|
new Date(2021, 9, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("11-01", "MM-dd"),
|
||||||
|
new Date(2021, 10, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("12-01", "MM-dd"),
|
||||||
|
new Date(2021, 11, 1),
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("01", "dd"),
|
||||||
|
new Date(2021, 11, 1),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("15", "dd"),
|
||||||
|
new Date(2021, 11, 15),
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
datetime.parse("31", "dd"),
|
||||||
|
new Date(2021, 11, 31),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
time.restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "[std/datetime] invalidParseDateTimeFormatThrows",
|
name: "[std/datetime] invalidParseDateTimeFormatThrows",
|
||||||
fn: () => {
|
fn: () => {
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
||||||
// This file contains test cases which needs to be run at the specific date (2021-12-31)
|
|
||||||
import { assertEquals } from "../testing/asserts.ts";
|
|
||||||
import { parse } from "./mod.ts";
|
|
||||||
|
|
||||||
Deno.test("The date is 2021-12-31", () => {
|
|
||||||
const d = new Date();
|
|
||||||
d.setHours(0, 0, 0, 0);
|
|
||||||
assertEquals(d, new Date(2021, 11, 31));
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("[std/datetime] parse: The date is 2021-12-31", () => {
|
|
||||||
assertEquals(
|
|
||||||
parse("01-01", "MM-dd"),
|
|
||||||
new Date(2021, 0, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("02-01", "MM-dd"),
|
|
||||||
new Date(2021, 1, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("03-01", "MM-dd"),
|
|
||||||
new Date(2021, 2, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("04-01", "MM-dd"),
|
|
||||||
new Date(2021, 3, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("05-01", "MM-dd"),
|
|
||||||
new Date(2021, 4, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("06-01", "MM-dd"),
|
|
||||||
new Date(2021, 5, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("07-01", "MM-dd"),
|
|
||||||
new Date(2021, 6, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("08-01", "MM-dd"),
|
|
||||||
new Date(2021, 7, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("09-01", "MM-dd"),
|
|
||||||
new Date(2021, 8, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("10-01", "MM-dd"),
|
|
||||||
new Date(2021, 9, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("11-01", "MM-dd"),
|
|
||||||
new Date(2021, 10, 1),
|
|
||||||
);
|
|
||||||
assertEquals(
|
|
||||||
parse("12-01", "MM-dd"),
|
|
||||||
new Date(2021, 11, 1),
|
|
||||||
);
|
|
||||||
});
|
|
@ -35,7 +35,6 @@
|
|||||||
},
|
},
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"test": "deno test --doc --unstable --allow-all --coverage=./cov --ignore=node/",
|
"test": "deno test --doc --unstable --allow-all --coverage=./cov --ignore=node/",
|
||||||
"test:faketime": "faketime '2021-12-31' deno test --unstable --allow-all datetime/test_2021_12_31.ts",
|
|
||||||
"test:browser": "git grep --name-only \"// This module is browser compatible.\" | grep -v \".github/workflows\" | xargs deno cache --config browser-compat.tsconfig.json",
|
"test:browser": "git grep --name-only \"// This module is browser compatible.\" | grep -v \".github/workflows\" | xargs deno cache --config browser-compat.tsconfig.json",
|
||||||
"node:unit": "DENO_FUTURE_CHECK=1 deno test --unstable --allow-all node/ --ignore=node/_tools/test.ts,node/_tools/versions/",
|
"node:unit": "DENO_FUTURE_CHECK=1 deno test --unstable --allow-all node/ --ignore=node/_tools/test.ts,node/_tools/versions/",
|
||||||
"node:test": "deno test --unstable --allow-all node/_tools/test.ts",
|
"node:test": "deno test --unstable --allow-all node/_tools/test.ts",
|
||||||
|
Loading…
Reference in New Issue
Block a user