Skip to content

Commit

Permalink
Convert asserts to expects
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Mar 14, 2024
1 parent de6356d commit cd013f8
Show file tree
Hide file tree
Showing 247 changed files with 3,157 additions and 5,115 deletions.
13 changes: 5 additions & 8 deletions src/_lib/addLeadingZeros/test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/* eslint-env mocha */

import assert from "node:assert";
import { describe, it, expect } from "vitest";
import { describe, expect, it } from "vitest";
import { addLeadingZeros } from "./index.js";

describe("addLeadingZeros", () => {
it("adds leading zeros when number has fewer digits than target length", () => {
expect(addLeadingZeros(7, 3)).toBe("007");
assert.strictEqual(addLeadingZeros(7, 2), "07");
assert.strictEqual(addLeadingZeros(7, 1), "7");
assert.strictEqual(addLeadingZeros(7, 0), "7");
assert.strictEqual(addLeadingZeros(7, -1), "7");
expect(addLeadingZeros(7, 2)).toBe("07");
expect(addLeadingZeros(7, 1)).toBe("7");
expect(addLeadingZeros(7, 0)).toBe("7");
expect(addLeadingZeros(7, -1)).toBe("7");
});
});
51 changes: 25 additions & 26 deletions src/_lib/getRoundingMethod/test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import { describe, it } from "vitest";
import assert from "node:assert";
import { describe, expect, it } from "vitest";
import { getRoundingMethod } from ".";

describe("getRoundingMethod", () => {
it("rounds with truncate by default", () => {
assert(getRoundingMethod(undefined)(2.9) === 2);
assert(getRoundingMethod(undefined)(1.5) === 1);
assert(getRoundingMethod(undefined)(-3.9) === -3);
assert(getRoundingMethod(undefined)(-14.1) === -14);
expect(getRoundingMethod(undefined)(2.9)).toBe(2);
expect(getRoundingMethod(undefined)(1.5)).toBe(1);
expect(getRoundingMethod(undefined)(-3.9)).toBe(-3);
expect(getRoundingMethod(undefined)(-14.1)).toBe(-14);
});

it('allows to specify "trunc" rounding method', () => {
assert(getRoundingMethod("trunc")(2.9) === 2);
assert(getRoundingMethod("trunc")(1.5) === 1);
assert(getRoundingMethod("trunc")(-3.9) === -3);
assert(getRoundingMethod("trunc")(-14.1) === -14);
expect(getRoundingMethod("trunc")(2.9)).toBe(2);
expect(getRoundingMethod("trunc")(1.5)).toBe(1);
expect(getRoundingMethod("trunc")(-3.9)).toBe(-3);
expect(getRoundingMethod("trunc")(-14.1)).toBe(-14);
});

it('allows to specify "ceil" rounding method', () => {
assert(getRoundingMethod("ceil")(2.9) === 3);
assert(getRoundingMethod("ceil")(1.5) === 2);
assert(getRoundingMethod("ceil")(-3.9) === -3);
assert(getRoundingMethod("ceil")(-14.1) === -14);
expect(getRoundingMethod("ceil")(2.9)).toBe(3);
expect(getRoundingMethod("ceil")(1.5)).toBe(2);
expect(getRoundingMethod("ceil")(-3.9)).toBe(-3);
expect(getRoundingMethod("ceil")(-14.1)).toBe(-14);
});

it('allows to specify "floor" rounding method', () => {
assert(getRoundingMethod("floor")(2.9) === 2);
assert(getRoundingMethod("floor")(1.5) === 1);
assert(getRoundingMethod("floor")(-3.9) === -4);
assert(getRoundingMethod("floor")(-14.1) === -15);
expect(getRoundingMethod("floor")(2.9)).toBe(2);
expect(getRoundingMethod("floor")(1.5)).toBe(1);
expect(getRoundingMethod("floor")(-3.9)).toBe(-4);
expect(getRoundingMethod("floor")(-14.1)).toBe(-15);
});

it('allows to specify "round" rounding method', () => {
assert(getRoundingMethod("round")(2.9) === 3);
assert(getRoundingMethod("round")(1.5) === 2);
assert(getRoundingMethod("round")(-3.9) === -4);
assert(getRoundingMethod("round")(-14.1) === -14);
expect(getRoundingMethod("round")(2.9)).toBe(3);
expect(getRoundingMethod("round")(1.5)).toBe(2);
expect(getRoundingMethod("round")(-3.9)).toBe(-4);
expect(getRoundingMethod("round")(-14.1)).toBe(-14);
});

it("prevents negative zero", () => {
assert(!isNegativeZero(getRoundingMethod("trunc")(-0.01)));
assert(!isNegativeZero(getRoundingMethod("floor")(-0.0)));
assert(!isNegativeZero(getRoundingMethod("ceil")(-0.9)));
assert(!isNegativeZero(getRoundingMethod("round")(-0.1)));
expect(!isNegativeZero(getRoundingMethod("trunc")(-0.01))).toBe(true);
expect(!isNegativeZero(getRoundingMethod("floor")(-0.0))).toBe(true);
expect(!isNegativeZero(getRoundingMethod("ceil")(-0.9))).toBe(true);
expect(!isNegativeZero(getRoundingMethod("round")(-0.1))).toBe(true);
});
});

Expand Down
11 changes: 4 additions & 7 deletions src/_lib/getTimezoneOffsetInMilliseconds/test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
/* eslint-env mocha */

import assert from "node:assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { getTimezoneOffsetInMilliseconds } from "./index.js";

describe("getTimezoneOffsetInMilliseconds", () => {
it("works for a modern date", () => {
const date = new Date(2018, 0 /* Jan */, 1, 12, 34, 56, 789);
const result = date.getTime() - getTimezoneOffsetInMilliseconds(date);
const expectedResult = Date.UTC(2018, 0 /* Jan */, 1, 12, 34, 56, 789);
assert(result === expectedResult);
expect(result).toBe(expectedResult);
});

it("works for a date before standardized timezones", () => {
const date = new Date(1800, 0 /* Jan */, 1, 12, 34, 56, 789);
const result = date.getTime() - getTimezoneOffsetInMilliseconds(date);
const expectedResult = Date.UTC(1800, 0 /* Jan */, 1, 12, 34, 56, 789);
assert(result === expectedResult);
expect(result).toBe(expectedResult);
});

it("works for a date BC", () => {
const date = new Date(-500, 0 /* Jan */, 1, 12, 34, 56, 789);
const result = date.getTime() - getTimezoneOffsetInMilliseconds(date);
const expectedResult = Date.UTC(-500, 0 /* Jan */, 1, 12, 34, 56, 789);
assert(result === expectedResult);
expect(result).toBe(expectedResult);
});
});
31 changes: 11 additions & 20 deletions src/add/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-env mocha */

import assert from "node:assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { add } from "./index.js";
import { getDstTransitions } from "../../test/dst/tzOffsetTransitions.js";

Expand All @@ -16,10 +13,7 @@ describe("add", () => {
minutes: 9,
seconds: 30,
});
assert.deepStrictEqual(
result,
new Date(2017, 5 /* June */, 15, 15, 29, 20),
);
expect(result).toEqual(new Date(2017, 5 /* June */, 15, 15, 29, 20));
});

it("supports an undefined value in the duration object", () => {
Expand All @@ -32,10 +26,7 @@ describe("add", () => {
minutes: 9,
seconds: 30,
});
assert.deepStrictEqual(
result,
new Date(2015, 5 /* June */, 15, 15, 29, 20),
);
expect(result).toEqual(new Date(2015, 5 /* June */, 15, 15, 29, 20));
});

it("returns same date object when passed empty duration values", () => {
Expand All @@ -48,31 +39,31 @@ describe("add", () => {
minutes: undefined,
seconds: undefined,
});
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 10));
expect(result).toEqual(new Date(2014, 8 /* Sep */, 1, 10));
});

it("returns same date object when passed undefined duration values", () => {
const result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), {});
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 10));
expect(result).toEqual(new Date(2014, 8 /* Sep */, 1, 10));
});

it("accepts a timestamp", () => {
const result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), {
hours: 4,
});
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1, 14));
expect(result).toEqual(new Date(2014, 8 /* Sep */, 1, 14));
});

it("does not mutate the original date", () => {
const date = new Date(2014, 8 /* Sep */, 1, 10);
add(date, { hours: 4 });
assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 1, 10));
expect(date).toEqual(new Date(2014, 8 /* Sep */, 1, 10));
});

it("works well if the desired month has fewer days and the provided date is in the last day of a month", () => {
const date = new Date(2014, 11 /* Dec */, 31);
const result = add(date, { months: 9 });
assert.deepStrictEqual(result, new Date(2015, 8 /* Sep */, 30));
expect(result).toEqual(new Date(2015, 8 /* Sep */, 30));
});

const dstTransitions = getDstTransitions(2017);
Expand All @@ -85,7 +76,7 @@ describe("add", () => {
() => {
const date = dstTransitions.end;
const result = add(date!, { hours: 1 });
assert.deepStrictEqual(result, new Date(date!.getTime() + HOUR));
expect(result).toEqual(new Date(date!.getTime() + HOUR));
},
);

Expand All @@ -97,11 +88,11 @@ describe("add", () => {
expectedResult.setFullYear(0, 1 /* Feb */, 29);
expectedResult.setHours(0, 0, 0, 0);
const result = add(initialDate, { months: 3 });
assert.deepStrictEqual(result, expectedResult);
expect(result).toEqual(expectedResult);
});

it("returns `Invalid Date` if the given date is invalid", () => {
const result = add(new Date(NaN), { hours: 5 });
assert(result instanceof Date && isNaN(result.getTime()));
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});
});
48 changes: 21 additions & 27 deletions src/addBusinessDays/test.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,75 @@
/* eslint-env mocha */

import assert from "node:assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { addBusinessDays } from "./index.js";

describe("addBusinessDays", () => {
it("adds the given number of business days", () => {
const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1), 10);
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 15));
expect(result).toEqual(new Date(2014, 8 /* Sep */, 15));
});

it("handles negative amount", () => {
const result = addBusinessDays(new Date(2014, 8 /* Sep */, 15), -10);
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 1));
expect(result).toEqual(new Date(2014, 8 /* Sep */, 1));
});

it("returns the Monday when 1 day is added on the Friday", () => {
assert.deepStrictEqual(
addBusinessDays(new Date(2020, 0 /* Jan */, 10), 1), // Friday
new Date(2020, 0 /* Jan */, 13), // Monday
);
expect(addBusinessDays(new Date(2020, 0 /* Jan */, 10), 1)).toEqual(// Friday
// Monday
new Date(2020, 0 /* Jan */, 13));
});

it("returns the Monday when 1 day is added on the Satuday", () => {
assert.deepStrictEqual(
addBusinessDays(new Date(2020, 0 /* Jan */, 11), 1), // Saturday
new Date(2020, 0 /* Jan */, 13), // Monday
);
expect(addBusinessDays(new Date(2020, 0 /* Jan */, 11), 1)).toEqual(// Saturday
// Monday
new Date(2020, 0 /* Jan */, 13));
});

it("returns the Monday when 1 day is added on the Sunday", () => {
assert.deepStrictEqual(
addBusinessDays(new Date(2020, 0 /* Jan */, 12), 1), // Sunday
new Date(2020, 0 /* Jan */, 13), // Monday
);
expect(addBusinessDays(new Date(2020, 0 /* Jan */, 12), 1)).toEqual(// Sunday
// Monday
new Date(2020, 0 /* Jan */, 13));
});

it("can handle a large number of business days", () => {
const result = addBusinessDays(new Date(2014, 0 /* Jan */, 1), 3387885);
assert.deepStrictEqual(result, new Date(15000, 0 /* Jan */, 1));
expect(result).toEqual(new Date(15000, 0 /* Jan */, 1));
});

it("accepts a timestamp", () => {
const result = addBusinessDays(
new Date(2014, 8 /* Sep */, 1).getTime(),
10,
);
assert.deepStrictEqual(result, new Date(2014, 8 /* Sep */, 15));
expect(result).toEqual(new Date(2014, 8 /* Sep */, 15));
});

it("does not mutate the original date", () => {
const date = new Date(2014, 8 /* Sep */, 1);
addBusinessDays(date, 11);
assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 1));
expect(date).toEqual(new Date(2014, 8 /* Sep */, 1));
});

it("returns `Invalid Date` if the given date is invalid", () => {
const result = addBusinessDays(new Date(NaN), 10);
assert(result instanceof Date && isNaN(result.getTime()));
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});

it("returns `Invalid Date` if the given amount is NaN", () => {
const result = addBusinessDays(new Date(2014, 8 /* Sep */, 1), NaN);
assert(result instanceof Date && isNaN(result.getTime()));
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});

it("starting from a weekend day should land on a weekday when reducing a divisible by 5", () => {
const substractResult = addBusinessDays(new Date(2019, 7, 18), -5);
assert.deepStrictEqual(substractResult, new Date(2019, 7, 12));
expect(substractResult).toEqual(new Date(2019, 7, 12));

const subtractResultWeekend = addBusinessDays(new Date(2019, 7, 17), -5);
assert.deepStrictEqual(subtractResultWeekend, new Date(2019, 7, 12));
expect(subtractResultWeekend).toEqual(new Date(2019, 7, 12));

const addResult = addBusinessDays(new Date(2019, 7, 18), 5);
assert.deepStrictEqual(addResult, new Date(2019, 7, 23));
expect(addResult).toEqual(new Date(2019, 7, 23));

const addResultWeekend = addBusinessDays(new Date(2019, 7, 17), 5);
assert.deepStrictEqual(addResultWeekend, new Date(2019, 7, 23));
expect(addResultWeekend).toEqual(new Date(2019, 7, 23));
});
});

0 comments on commit cd013f8

Please sign in to comment.