Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow maps to be passed to the empty expectation #463

Merged
merged 4 commits into from Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions core/matchers/empty-matcher.ts
Expand Up @@ -18,19 +18,26 @@ export class EmptyMatcher<T> extends Matcher<T> {
if (
typeof this.actualValue !== "string" &&
!Array.isArray(this.actualValue) &&
this.actualValue.constructor !== Object
this.actualValue.constructor !== Object &&
!(this.actualValue instanceof Map)
) {
throw new TypeError(
"toBeEmpty requires value passed in to Expect to be an array, string or object literal"
"toBeEmpty requires value passed in to Expect to be an array, string, object literal or map"
);
}

const contents = (this.actualValue as any).length
? this.actualValue
: Object.keys(this.actualValue);
if (this.actualValue instanceof Map) {
if ((this.actualValue.size === 0) !== this.shouldMatch) {
throw new EmptyMatchError(this.actualValue, this.shouldMatch);
}
} else {
const contents = (this.actualValue as any).length
? this.actualValue
: Object.keys(this.actualValue);

if (((contents as any).length === 0) !== this.shouldMatch) {
throw new EmptyMatchError(this.actualValue, this.shouldMatch);
if (((contents as any).length === 0) !== this.shouldMatch) {
throw new EmptyMatchError(this.actualValue, this.shouldMatch);
}
}
}
}
4 changes: 4 additions & 0 deletions core/stringification/stringify.ts
Expand Up @@ -5,6 +5,10 @@ export function stringify(data: any): string {
return stringifyArray(data);
}

if (data instanceof Map) {
return `Map<${data.size}>`;
}

if (data === Any) {
return "Anything";
}
Expand Down
47 changes: 46 additions & 1 deletion test/unit-tests/expect-tests/to-be-empty.spec.ts
Expand Up @@ -6,7 +6,7 @@ class DummyClass {}
export class ToBeEmptyTests {
private readonly _typeErrorMessage: string =
"toBeEmpty requires value passed in to Expect to be " +
"an array, string or object literal";
"an array, string, object literal or map";

@TestCase([])
@TestCase([1])
Expand Down Expand Up @@ -42,6 +42,17 @@ export class ToBeEmptyTests {
);
}

@TestCase(new Map())
@TestCase(new Map([["keyOne", "valueOne"]]))
public emptyShouldNotThrowTypeErrorForMaps(value: Map<any, any>) {
const expect = Expect(value);

Expect(() => expect.toBeEmpty()).not.toThrowError(
TypeError,
this._typeErrorMessage
);
}

@TestCase(null)
@TestCase(undefined)
public emptyShouldThrowTypeErrorForNullTypes(value: any) {
Expand Down Expand Up @@ -172,4 +183,38 @@ export class ToBeEmptyTests {

Expect(() => expect.not.toBeEmpty()).not.toThrow();
}

@Test()
public emptyShouldNotThrowErrorForEmptyMap() {
const expect = Expect(new Map());

Expect(() => expect.toBeEmpty()).not.toThrow();
}

@Test()
public emptyShouldThrowErrorForNonEmptyMap() {
const expect = Expect(new Map([["key", "value"]]));

Expect(() => expect.toBeEmpty()).toThrowError(
EmptyMatchError,
'Expected "Map<1>" to be empty.'
);
}

@Test()
public notEmptyShouldThrowErrorForEmptyMap() {
const expect = Expect(new Map());

Expect(() => expect.not.toBeEmpty()).toThrowError(
EmptyMatchError,
'Expected "Map<0>" not to be empty.'
);
}

@Test()
public notEmptyShouldNotThrowErrorForNonEmptyMap() {
const expect = Expect(new Map([["key", "value"]]));

Expect(() => expect.not.toBeEmpty()).not.toThrow();
}
}