Skip to content

Commit

Permalink
Added count equality check method and added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDWaller committed Jul 1, 2020
1 parent a531e4e commit 5b99607
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/equality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ export function typeOf(

return err(errorSimple(actual, expected, "is not a type of"));
}

export function count<T>(actual: Array<T>, expected: number): Result<string> {
if (actual.length === expected) {
return ok(`${actual} has a count of ${expected}`);
}

return err(errorSimple(actual, expected, "does not have a count of"));
}
36 changes: 35 additions & 1 deletion tests/equality.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Result } from "../dev_deps.ts";
import { assertTrue } from "../mod.ts";
import { assertTrue, assertFalse } from "../mod.ts";
import {
equals,
greater,
Expand All @@ -8,6 +8,7 @@ import {
lessOrEqual,
instanceOf,
typeOf,
count
} from "../src/equality.ts";

Deno.test("Equals Boolean", () => {
Expand Down Expand Up @@ -106,3 +107,36 @@ Deno.test("Type Of Fail", () => {

assertTrue(result.isError());
});

Deno.test("Count", () => {
const result: Result<string> = count(["Hello", "World"], 2);

assertTrue(result.isOk());
});

Deno.test("Count Fail", () => {
const result: Result<string> = count(["Hello", "World", "Foo"], 2);

assertFalse(result.isOk());
});

Deno.test("Count Mixed Types", () => {
const result: Result<string> = count(["Hello", 4, "Foo"], 3);

assertTrue(result.isOk());
});

Deno.test("Count Nested Elements", () => {
const myArray = [
"Hello",
"World",
["Foo", "Bar"],
"How",
"Are",
"You"
]

const result: Result<string> = count(myArray, 6);

assertTrue(result.isOk());
});

0 comments on commit 5b99607

Please sign in to comment.