Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions packages/core/src/lib/NumberAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,15 @@ export class NumberAssertion extends Assertion<number> {
/**
* Check if the number is even. That is, when the number is divisible by 2.
*
* Note: Parity applies only to integers. Non-integers (e.g. 1.5), `NaN`,
* and `±Infinity` are not considered even. Both `0` and `-0` are
* considered even.
*
* @example
* ```
* expect(50).toBeEven();
* expect(2).toBeEven();
* expect(-4).toBeEven();
* expect(0).toBeEven();
* expect(-10).toBeEven();
* ```
*
* @returns the assertion instance
Expand All @@ -202,11 +206,11 @@ export class NumberAssertion extends Assertion<number> {
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the value NOT to be even",
message: `Expected <${this.actual}> NOT to be even`,
});

return this.execute({
assertWhen: this.actual % 2 === 0,
assertWhen: Number.isInteger(this.actual) && this.actual % 2 === 0,
error,
invertedError,
});
Expand All @@ -215,6 +219,9 @@ export class NumberAssertion extends Assertion<number> {
/**
* Check if the number is odd. That is, when the number is not divisible by 2.
*
* Note: Parity applies only to integers. Non-integers (e.g. 1.5), `NaN`,
* and `±Infinity` are not considered odd.
*
* @example
* ```
* expect(-13).toBeOdd();
Expand All @@ -230,11 +237,11 @@ export class NumberAssertion extends Assertion<number> {
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the value NOT to be odd",
message: `Expected <${this.actual}> NOT to be odd`,
});

return this.execute({
assertWhen: this.actual % 2 === 1,
assertWhen: Number.isInteger(this.actual) && Math.abs(this.actual % 2) === 1,
error,
invertedError,
});
Expand Down Expand Up @@ -282,10 +289,12 @@ export class NumberAssertion extends Assertion<number> {
const inclusiveError = new AssertionError({
actual: this.actual,
expected: options,
message: `Expected <${this.actual}> to be between ${rangeText} range` });
message: `Expected <${this.actual}> to be between ${rangeText} range`,
});
const inclusiveInvertedError = new AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> NOT to be between ${rangeText} range` });
message: `Expected <${this.actual}> NOT to be between ${rangeText} range`,
});

return this.execute({
assertWhen: options.inclusive
Expand Down
183 changes: 168 additions & 15 deletions packages/core/test/lib/NumberAssertion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,51 +136,204 @@ describe("[Unit] NumberAssertion.test.ts", () => {
describe(".toBeEven", () => {
context("when the value is even", () => {
it("returns the assertion instance", () => {
const test = new NumberAssertion(2);
const testPositive = new NumberAssertion(2);

assert.deepStrictEqual(testPositive.toBeEven(), testPositive);
assert.throws(() => testPositive.not.toBeEven(), {
message: "Expected <2> NOT to be even",
name: AssertionError.name,
});

const testNegative = new NumberAssertion(-2);

assert.deepStrictEqual(testNegative.toBeEven(), testNegative);
assert.throws(() => testNegative.not.toBeEven(), {
message: "Expected <-2> NOT to be even",
name: AssertionError.name,
});

const testZero = new NumberAssertion(0);

assert.deepStrictEqual(testZero.toBeEven(), testZero);
assert.throws(() => testZero.not.toBeEven(), {
message: "Expected <0> NOT to be even",
name: AssertionError.name,
});

const testNegativeZero = new NumberAssertion(-0);

assert.deepStrictEqual(test.toBeEven(), test);
assert.throws(() => test.not.toBeEven(), {
message: "Expected the value NOT to be even",
assert.deepStrictEqual(testNegativeZero.toBeEven(), testNegativeZero);
assert.throws(() => testNegativeZero.not.toBeEven(), {
message: "Expected <0> NOT to be even",
name: AssertionError.name,
});
});
});

context("when the value is NOT even", () => {
it("throws an assertion error", () => {
const test = new NumberAssertion(1);
const testOddPositive = new NumberAssertion(1);

assert.throws(() => test.toBeEven(), {
assert.throws(() => testOddPositive.toBeEven(), {
message: "Expected <1> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(test.not.toBeEven(), test);
assert.deepStrictEqual(testOddPositive.not.toBeEven(), testOddPositive);

const testOddNegative = new NumberAssertion(-1);

assert.throws(() => testOddNegative.toBeEven(), {
message: "Expected <-1> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(testOddNegative.not.toBeEven(), testOddNegative);

const testFloat = new NumberAssertion(1.5);

assert.throws(() => testFloat.toBeEven(), {
message: "Expected <1.5> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(testFloat.not.toBeEven(), testFloat);

const testNaN = new NumberAssertion(NaN);

assert.throws(() => testNaN.toBeEven(), {
message: "Expected <NaN> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(testNaN.not.toBeEven(), testNaN);

const testInfinity = new NumberAssertion(Infinity);

assert.throws(() => testInfinity.toBeEven(), {
message: "Expected <Infinity> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(testInfinity.not.toBeEven(), testInfinity);

const testNegativeInfinity = new NumberAssertion(-Infinity);

assert.throws(() => testNegativeInfinity.toBeEven(), {
message: "Expected <-Infinity> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(
testNegativeInfinity.not.toBeEven(),
testNegativeInfinity,
);

const testNegativeFloat = new NumberAssertion(-1.5);

assert.throws(() => testNegativeFloat.toBeEven(), {
message: "Expected <-1.5> to be even",
name: AssertionError.name,
});
assert.deepStrictEqual(
testNegativeFloat.not.toBeEven(),
testNegativeFloat,
);
});
});
});

describe(".toBeOdd", () => {
context("when the value is odd", () => {
it("returns the assertion instance", () => {
const test = new NumberAssertion(1);
const testPositive = new NumberAssertion(1);

assert.deepStrictEqual(testPositive.toBeOdd(), testPositive);
assert.throws(() => testPositive.not.toBeOdd(), {
message: "Expected <1> NOT to be odd",
name: AssertionError.name,
});

const testNegative = new NumberAssertion(-13);

assert.deepStrictEqual(test.toBeOdd(), test);
assert.throws(() => test.not.toBeOdd(), {
message: "Expected the value NOT to be odd",
assert.deepStrictEqual(testNegative.toBeOdd(), testNegative);
assert.throws(() => testNegative.not.toBeOdd(), {
message: "Expected <-13> NOT to be odd",
name: AssertionError.name,
});

const testNegativeOne = new NumberAssertion(-1);

assert.deepStrictEqual(testNegativeOne.toBeOdd(), testNegativeOne);
assert.throws(() => testNegativeOne.not.toBeOdd(), {
message: "Expected <-1> NOT to be odd",
name: AssertionError.name,
});
});
});

context("when the value is NOT odd", () => {
it("throws an assertion error", () => {
const test = new NumberAssertion(2);
const testEvenPositive = new NumberAssertion(2);

assert.throws(() => test.toBeOdd(), {
assert.throws(() => testEvenPositive.toBeOdd(), {
message: "Expected <2> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(test.not.toBeOdd(), test);
assert.deepStrictEqual(testEvenPositive.not.toBeOdd(), testEvenPositive);

const testEvenNegative = new NumberAssertion(-2);

assert.throws(() => testEvenNegative.toBeOdd(), {
message: "Expected <-2> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(testEvenNegative.not.toBeOdd(), testEvenNegative);

const testNegativeZero = new NumberAssertion(-0);

assert.throws(() => testNegativeZero.toBeOdd(), {
message: "Expected <0> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(testNegativeZero.not.toBeOdd(), testNegativeZero);

const testFloat = new NumberAssertion(1.5);

assert.throws(() => testFloat.toBeOdd(), {
message: "Expected <1.5> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(testFloat.not.toBeOdd(), testFloat);

const testFloatNegative = new NumberAssertion(-1.5);

assert.throws(() => testFloatNegative.toBeOdd(), {
message: "Expected <-1.5> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(testFloatNegative.not.toBeOdd(), testFloatNegative);

const testNaN = new NumberAssertion(NaN);

assert.throws(() => testNaN.toBeOdd(), {
message: "Expected <NaN> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(testNaN.not.toBeOdd(), testNaN);

const testInfinity = new NumberAssertion(Infinity);

assert.throws(() => testInfinity.toBeOdd(), {
message: "Expected <Infinity> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(testInfinity.not.toBeOdd(), testInfinity);

const testNegativeInfinity = new NumberAssertion(-Infinity);

assert.throws(() => testNegativeInfinity.toBeOdd(), {
message: "Expected <-Infinity> to be odd",
name: AssertionError.name,
});
assert.deepStrictEqual(
testNegativeInfinity.not.toBeOdd(),
testNegativeInfinity,
);
});
});
});
Expand Down Expand Up @@ -467,7 +620,7 @@ describe("[Unit] NumberAssertion.test.ts", () => {
assert.deepStrictEqual(test.toBeBetween({ highInclusive: true, range: [10, 12] }), test);
assert.throws(() => test.not.toBeBetween({ highInclusive: true, range: [10, 12] }), {
message:
"Expected <12> NOT to be between (10, 12] range",
"Expected <12> NOT to be between (10, 12] range",
name: AssertionError.name,
},
);
Expand Down
Loading