Skip to content

Commit

Permalink
Merge pull request #6 from RobDWaller/0.5.0
Browse files Browse the repository at this point in the history
0.5.0
  • Loading branch information
RobDWaller authored Aug 7, 2020
2 parents 654fb7c + 2f575fe commit cf9cc38
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 126 deletions.
70 changes: 40 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ import {
assertDate,
assertDateTime,
assertFloat,
assertNotThrows,
assertCount,
assertEmpty
Round
} from "https://deno.land/x/explicitly@0.4.0/mod.ts";
} from "https://deno.land/x/explicitly@0.5.0/mod.ts";
```

## Basic Usage

This assertion library makes 14 assertion methods available:
This assertion library makes 15 assertion methods available:

- `assertTrue(actual: unknown): void`
- `assertFalse(actual: unknown): void`
Expand All @@ -50,6 +53,7 @@ This assertion library makes 14 assertion methods available:
- `assertFloat(actual: number, expected: number, decimals?: number, round?: Round): void`
- `assertNotThrows(actual: Function): void`
- `assertCount<T>(actual: Array<T>, expected: number): void`
- `assertEmpty(actual: unknown): void`

Each of these assertions aims to test a single thing. This means unit tests are explicit and clearer to read.

Expand Down Expand Up @@ -83,7 +87,7 @@ Deno.test("Assert Same Example", () => {

### Assert Greater or Less Example

Assert whether a value is greater, less, greater or equal, less or equal than another value.
Assert whether a value is greater than, less than, greater or equal than, or less or equal than another value.

```js
Deno.test("Assert Greater or Less Example", () => {
Expand Down Expand Up @@ -117,7 +121,7 @@ Deno.test("Assert Date Time Example", () => {
});
```

### Assert Float
### Assert Float Example

Assert whether two floats match. You can optionally define how many decimal places the assertion should be made to, along with defining if the check should be to the floor or ceiling. This is done by passing in the `Round` enum, either `Round.Floor` or `Round.Ceiling`. The assertion defaults to floor.

Expand All @@ -131,7 +135,7 @@ Deno.test("Assert Float Example", () => {
});
```

### Assert Count
### Assert Count Example

Assert whether an array has an expected number of elements. Will only count top level elements
will count nested elements as a single element.
Expand All @@ -148,7 +152,21 @@ Deno.test("Assert Count Example", () => {
});
```

### Assert Not Throws
## Assert Empty Example

Assert whether a string array or object literal are empty.

```js
Deno.test("Assert Empty Example", () => {
assertEmpty("");

assertEmpty([]);

assertEmpty({});
});
```

### Assert Not Throws Example

Assert a function does not throw an Error. This assertion may be of use when testing complicated legacy code when you want to ensure no errors occur.

Expand Down Expand Up @@ -195,39 +213,31 @@ Deno.test("Assert Instance Of Example", () => {
}

class Adult implements Person {
name: string;
age: number;
location: string;

constructor(name: string, age: number, location: string) {
this.name = name;
this.age = age;
this.location = location;
}
constructor(
public name: string,
public age: number,
public location: string,
) {}
}

class Child implements Person {
name: string;
age: number;
location: string;

constructor(name: string, age: number, location: string) {
this.name = name;
this.age = age;
this.location = location;
}
constructor(
public name: string,
public age: number,
public location: string,
) {}
}

function createPerson(name: string, age: number, location: string): Person {
if (age < 18) {
return new Child(name, age, location);
}

return new Adult(name, age, location);
return age < 18
? new Child(name, age, location)
: new Adult(name, age, location);
}

const jenny = createPerson("Jenny Brown", 12, "US");
const jenny = createPerson("Jenny", 12, "US");
const devika = createPerson("Devika ", 28, "FR");

assertInstanceOf(jenny, Child);
assertInstanceOf(devika, Adult);
});
```
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "explicitly",
"description": "Additional assertions for Deno so developers can write more explicit unit tests.",
"stable": false,
"version": "0.4.0",
"version": "0.5.0",
"entry": "./mod.ts",
"repository": "https://github.com/robdwaller/explicitly",
"files": [
Expand Down
59 changes: 31 additions & 28 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
instanceOf,
typeOf,
count,
empty,
} from "./src/equality.ts";
import {
dateTime,
Expand All @@ -24,9 +25,9 @@ export { Round } from "./src/float.ts";
import { Result, AssertionError } from "./deps.ts";

/**
* Deno test tools require an AssertionError to be thrown on error. If the
* result of the equality check is false throw an error with the unwrapped error
* message included.
* Deno test tools require an AssertionError to be thrown on error. If the
* result of the equality check is error throw an error with the unwrapped error
* message included.
*/
function handleError(result: Result<string>): void {
if (result.isError()) {
Expand All @@ -35,77 +36,73 @@ function handleError(result: Result<string>): void {
}

/**
* Assert the provided value is equal to true.
* Assert a value is equal to true.
*/
export function assertTrue(actual: unknown): void {
handleError(equals(actual, true));
}

/**
* Assert the provided value is equal to false.
* Assert a value is equal to false.
*/
export function assertFalse(actual: unknown): void {
handleError(equals(actual, false));
}

/**
* Assert the provided values have the same value and type.
* Assert a value has the same value and type as the expected value.
*/
export function assertSame(actual: unknown, expected: unknown): void {
handleError(equals(actual, expected));
}

/**
* Assert the actual value is greater in value or length than the
* expected value.
* Assert a value is greater in value or length than the expected value.
*/
export function assertGreater(actual: unknown, expected: unknown): void {
handleError(greater(actual, expected));
}

/**
* Assert the actual value is greater or equal in value or length to the
* expected value.
* Assert a value is greater or equal in value or length to the expected value.
*/
export function assertGreaterOrEqual(actual: unknown, expected: unknown): void {
handleError(greaterOrEqual(actual, expected));
}

/**
* Assert the actual value is less in value or length than the
* expected value.
* Assert a value is less in value or length than the expected value.
*/
export function assertLess(actual: unknown, expected: unknown): void {
handleError(less(actual, expected));
}

/**
* Assert the actual value is less or equal in value or length to the
* expected value.
* Assert a value is less or equal in value or length to the expected value.
*/
export function assertLessOrEqual(actual: unknown, expected: unknown): void {
handleError(lessOrEqual(actual, expected));
}

/**
* Assert the actual value is an instance of the expected type. Useful when
* checking polymorphic relationships.
* Assert a value is an instance of the expected type. Useful when testing
* polymorphic relationships.
*/
export function assertInstanceOf(actual: unknown, expected: any): void {
handleError(instanceOf(actual, expected));
}

/**
* Assert the actual value is of a specific type. Useful when a method returns
* a value of any or unknown.
* Assert the a value is of the expected type string. Useful when a method
* returns a value of any or unknown.
*/
export function assertTypeOf(actual: unknown, expected: string): void {
handleError(typeOf(actual, expected));
}

/**
* Assert a date object has the same date time as another date object or a
* date time string.
* Assert a date object has the same date time as another date object or date
* time string.
*/
export function assertDateTime(actual: Date, expected: Date | string): void {
if (typeof expected === "string") {
Expand All @@ -116,8 +113,8 @@ export function assertDateTime(actual: Date, expected: Date | string): void {
}

/**
* Assert a date object has the same date as another date object or a
* date string.
* Assert a date object has the same date as the expected date object or
* date string.
*/
export function assertDate(actual: Date, expected: Date | string): void {
if (typeof expected === "string") {
Expand All @@ -128,9 +125,9 @@ export function assertDate(actual: Date, expected: Date | string): void {
}

/**
* Assert whether an actual float equals an expected float. Can define the
* equality accuracy to a number of decimal places and whether it should be
* based on rounding to the floor or ceiling.
* Assert whether a float equals the expected float. Can define the equality
* accuracy to a number of decimal places and whether it should be based on
* rounding to the floor or ceiling.
*/
export function assertFloat(
actual: number,
Expand All @@ -148,16 +145,22 @@ export function assertFloat(
}

/**
* Assert whether a function does not throw an error. May be a useful when
* refactoring a codebase.
* Assert whether a function does not throw an error.
*/
export function assertNotThrows(actual: Function): void {
handleError(notThrows(actual));
}

/**
* Assert an array has a expected number of elements.
* Assert an array has an expected number of elements.
*/
export function assertCount<T>(actual: Array<T>, expected: number): void {
handleError(count(actual, expected));
}

/**
* Assert a string, array or object is empty.
*/
export function assertEmpty(actual: unknown): void {
handleError(empty(actual));
}
14 changes: 9 additions & 5 deletions src/date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Result, ok, err } from "../deps.ts";
import { errorSimple } from "./message.ts";
import { errorActualExpected } from "./message.ts";

function toDateString(date: Date): string {
return date.getFullYear() + "-" +
Expand All @@ -16,7 +16,7 @@ export function date(
}

return err(
errorSimple(
errorActualExpected(
toDateString(actual),
toDateString(expected),
"does not match expected date",
Expand All @@ -37,7 +37,7 @@ export function dateString(
}

return err(
errorSimple(
errorActualExpected(
toDateString(actual),
toDateString(expectedDate),
"does not match expected date",
Expand All @@ -54,7 +54,7 @@ export function dateTime(
}

return err(
errorSimple(
errorActualExpected(
actual.toISOString(),
expected.toISOString(),
"does not match expected date",
Expand All @@ -73,6 +73,10 @@ export function dateTimeString(
}

return err(
errorSimple(actual.toISOString(), expected, "does not match expected date"),
errorActualExpected(
actual.toISOString(),
expected,
"does not match expected date",
),
);
}
Loading

0 comments on commit cf9cc38

Please sign in to comment.