From 99ffa2aafcea475c2bbb3e3d97ac5426d88eed1c Mon Sep 17 00:00:00 2001 From: Victor Widell Date: Mon, 5 Jul 2021 18:15:53 +0200 Subject: [PATCH 1/3] Made array arguments `readonly`. The array arguments have no business being mutable. --- src/lib/assertion/expect.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/assertion/expect.ts b/src/lib/assertion/expect.ts index 9985531..8920ac8 100644 --- a/src/lib/assertion/expect.ts +++ b/src/lib/assertion/expect.ts @@ -43,7 +43,7 @@ class Expect { * @param expected Expected array. * @param message Custom assertion failure message. */ - public arraysToBeEqual(actual: T, expected: T, message?: string) { + public arraysToBeEqual(actual: T, expected: T, message?: string) { if (actual.length !== expected.length) throw new ExpectationError(message || `Expected sequences to be equal, but their lenghts were different.`); @@ -219,7 +219,7 @@ class Expect { * @param array Array in which the given item is expected to be. * @param message Custom assertion failure message. */ - public toBeIn(item: T, array: T[], message?: string) { + public toBeIn(item: T, array: readonly T[], message?: string) { const isIn = array.find((x) => x === item); if (this.notFlag ? isIn : !isIn) { throw new ExpectationError( From 2c0ad19b0ecfb83f9ff08be3e4bd7db252c4bdaf Mon Sep 17 00:00:00 2001 From: Victor Widell Date: Sun, 11 Jul 2021 19:30:15 +0200 Subject: [PATCH 2/3] Added test for ReadonlyArray. --- src/spec/assertion/arraysToBeEqual.spec.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/spec/assertion/arraysToBeEqual.spec.ts b/src/spec/assertion/arraysToBeEqual.spec.ts index 2801324..866a537 100644 --- a/src/spec/assertion/arraysToBeEqual.spec.ts +++ b/src/spec/assertion/arraysToBeEqual.spec.ts @@ -10,6 +10,13 @@ export class ExpectArraysToBeEqualTestSuite { private equal(actual, expected) { expect.arraysToBeEqual(actual, expected); } + + @Test('ReadonlyArrays to be equal') + private equal() { + const actual: ReaonlyArray = ['a', 'b', 'c']; + const expected: ReaonlyArray = ['a', 'b', 'c']; + expect.arraysToBeEqual(actual, expected); + } @Test('Arrays to be equal, should fail') @TestCase(`'a, b, c' to equal 'b, c, a'`, ['a', 'b', 'c'], ['b', 'c', 'a']) From 1899b243278b22f34b7dfd1392540b6f499f65db Mon Sep 17 00:00:00 2001 From: Victor Widell Date: Sun, 11 Jul 2021 19:33:12 +0200 Subject: [PATCH 3/3] Added test for ReadonlyArray. --- src/spec/assertion/toBeIn.spec.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/spec/assertion/toBeIn.spec.ts b/src/spec/assertion/toBeIn.spec.ts index 24c712b..8202198 100644 --- a/src/spec/assertion/toBeIn.spec.ts +++ b/src/spec/assertion/toBeIn.spec.ts @@ -7,6 +7,12 @@ export class ExpectToMatch { private aToBeInSuccess() { expect.toBeIn('a', ['a', 'b', 'c']); } + + @Test(`'a' to be in ReadonlyArray, should succeed`) + private aToBeInReadonlySuccess() { + const readonlyArray: ReaonlyArray = ['a', 'b', 'c']; + expect.toBeIn('a', readonlyArray); + } @Test(`'a' to be in, should fail`) private aToBeInFail() {