Skip to content

Commit

Permalink
exray v1.1.0 (#108)
Browse files Browse the repository at this point in the history
X from class to namespace
  • Loading branch information
nicfv authored Apr 13, 2024
1 parent 6ca182b commit 19366e0
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 38 deletions.
7 changes: 7 additions & 0 deletions exray/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.1.0

- Change `X` from a class to a namespace
- **Breaking change**: `X.true()` and `X.false()` are now `X.isTrue()` and `X.isFalse()` (since `true` and `false` are reserved keywords)
- Add a few more test cases
- Minor changes in the main readme documentation

## 1.0.5

- Update build scripts
Expand Down
2 changes: 1 addition & 1 deletion exray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ npm i -D exray

exray contains a simple, lightweight assertion testing framework for JavaScript and TypeScript projects. If any one of the exray tests fail, it will throw an exception and halt program execution. Exceptions can be caught using the standard `try ... catch` block.

exray exports one class, `X`, that includes several different test types, however all of them can be simply derived from `X.true(...)`. Custom exception messages are always optional, but recommended for clarity in case a test fails.
exray exports one namespace, `X`, that includes several different test types, all of which can be simply derived from `X.isTrue(...)`. Each test type throws a unique exception message if failed. Custom exception messages are optional, but always recommended for clarity to explain why a test may have failed.
2 changes: 1 addition & 1 deletion exray/examples/1-Assert-Boolean-Values.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Normally, a unit test will return true or false to determine if it passes or fails. If this is the case, you almost certainly want to use the `X.true()` function. `X.false()` can be used if a test needs to return false in order to pass. Here are a few simple examples of tests that will always pass using these functions.
Normally, a unit test will return true or false to determine if it passes or fails. If this is the case, you almost certainly want to use the `X.isTrue()` function. `X.isFalse()` can be used if a test needs to return false in order to pass. Here are a few simple examples of tests that will always pass using these functions.
4 changes: 2 additions & 2 deletions exray/examples/1-Assert-Boolean-Values.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ function alwaysReturnsTrue() {
return true;
}
// This test should pass
X.true(alwaysReturnsTrue());
X.isTrue(alwaysReturnsTrue());

// This function always returns false.
function alwaysReturnsFalse() {
return false;
}
// This test should pass
X.false(alwaysReturnsFalse());
X.isFalse(alwaysReturnsFalse());

// Show that all tests passed.
console.log('All tests passed!');
11 changes: 7 additions & 4 deletions exray/examples/3-Math-Comparisons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { X } from 'exray';

// 5 is greater than 4
X.gt(5, 4);
X.true(5 > 4);
X.isTrue(5 > 4);

// 2 is less than or equal to 2
X.le(2, 2);
X.true(2 <= 2);
X.isTrue(2 <= 2);

// 3 is not equal to 0
X.ne(3, 0)
X.true(3 !== 0);
X.false(3 === 0);
X.isTrue(3 !== 0);
X.isFalse(3 === 0);

// Show that all tests passed.
console.log('All tests passed!');
2 changes: 1 addition & 1 deletion exray/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"clean": "rm -rf node_modules package-lock.json"
},
"dependencies": {
"exray": "file:exray-1.0.5.tgz"
"exray": "file:exray-1.1.0.tgz"
}
}
2 changes: 1 addition & 1 deletion exray/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exray",
"version": "1.0.5",
"version": "1.1.0",
"description": "Lightweight assertion testing framework",
"homepage": "https://npm.nicfv.com/exray",
"bin": "",
Expand Down
46 changes: 23 additions & 23 deletions exray/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
* Exceptions can be caught with
* standard `try ... catch` blocks.
*/
export abstract class X {
export namespace X {
/**
* Increments for every test run.
*/
private static i: number = 0;
let i: number = 0;
/**
* Expect a test to return **true**.
* @param test A test that returns a boolean result
* @param message The exception message to show if
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static true(test: boolean, message: string = 'The test returned ' + test + '.'): void {
this.i++;
export function isTrue(test: boolean, message: string = 'The test returned ' + test + '.'): void {
i++;
if (!test) {
throw new Exception(this.i, message);
throw new Exception(i, message);
}
}
/**
Expand All @@ -36,8 +36,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static false(test: boolean, message: string = 'The test returned ' + test + '.'): void {
this.true(!test, message);
export function isFalse(test: boolean, message: string = 'The test returned ' + test + '.'): void {
isTrue(!test, message);
}
/**
* Expect the test string to be **identical** to the expected string.
Expand All @@ -47,8 +47,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static is(test: string, expect: string, message: string = 'The test string "' + test + '" did not match the expected string "' + expect + '".'): void {
this.true(test === expect, message);
export function is(test: string, expect: string, message: string = 'The test string "' + test + '" did not match the expected string "' + expect + '".'): void {
isTrue(test === expect, message);
}
/**
* Expect the test string to be **different** than the expected string.
Expand All @@ -58,8 +58,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static not(test: string, expect: string, message: string = 'The test string "' + test + '" matched the expected string "' + expect + '".'): void {
this.true(test !== expect, message);
export function not(test: string, expect: string, message: string = 'The test string "' + test + '" matched the expected string "' + expect + '".'): void {
isTrue(test !== expect, message);
}
/**
* Expect the test to **be equal** to the expected value.
Expand All @@ -69,8 +69,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static eq(test: number, expect: number, message: string = 'The test value of ' + test + ' was not equal to the expected value of ' + expect + '.'): void {
this.true(test === expect, message);
export function eq(test: number, expect: number, message: string = 'The test value of ' + test + ' was not equal to the expected value of ' + expect + '.'): void {
isTrue(test === expect, message);
}
/**
* Expect the test to **not be equal** to the expected value.
Expand All @@ -80,8 +80,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static ne(test: number, expect: number, message: string = 'The test value of ' + test + ' was equal to the expected value of ' + expect + '.'): void {
this.true(test !== expect, message);
export function ne(test: number, expect: number, message: string = 'The test value of ' + test + ' was equal to the expected value of ' + expect + '.'): void {
isTrue(test !== expect, message);
}
/**
* Expect the test to be strictly **greater than** the expected value.
Expand All @@ -91,8 +91,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static gt(test: number, expect: number, message: string = 'The test value of ' + test + ' was not greater than the expected value of ' + expect + '.'): void {
this.true(test > expect, message);
export function gt(test: number, expect: number, message: string = 'The test value of ' + test + ' was not greater than the expected value of ' + expect + '.'): void {
isTrue(test > expect, message);
}
/**
* Expect the test to be strictly **less than** the expected value.
Expand All @@ -102,8 +102,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static lt(test: number, expect: number, message: string = 'The test value of ' + test + ' was not less than the expected value of ' + expect + '.'): void {
this.true(test < expect, message);
export function lt(test: number, expect: number, message: string = 'The test value of ' + test + ' was not less than the expected value of ' + expect + '.'): void {
isTrue(test < expect, message);
}
/**
* Expect the test to be **greater than or equal to** the expected value.
Expand All @@ -113,8 +113,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static ge(test: number, expect: number, message: string = 'The test value of ' + test + ' was not greater than nor equal to the expected value of ' + expect + '.'): void {
this.true(test >= expect, message);
export function ge(test: number, expect: number, message: string = 'The test value of ' + test + ' was not greater than nor equal to the expected value of ' + expect + '.'): void {
isTrue(test >= expect, message);
}
/**
* Expect the test to be **less than or equal to** the expected value.
Expand All @@ -124,8 +124,8 @@ export abstract class X {
* an unexpected result was found. If not set, will
* display a default message for this type of test.
*/
public static le(test: number, expect: number, message: string = 'The test value of ' + test + ' was not less than nor equal to the expected value of ' + expect + '.'): void {
this.true(test <= expect, message);
export function le(test: number, expect: number, message: string = 'The test value of ' + test + ' was not less than nor equal to the expected value of ' + expect + '.'): void {
isTrue(test <= expect, message);
}
}
/**
Expand Down
13 changes: 8 additions & 5 deletions exray/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { X } from './index';

X.true(true);
X.false(false);
X.isTrue(true);
X.isFalse(false);
X.eq(1, 1);
X.ge(1, 1);
X.ge(2, 1);
X.gt(2, 1);

try {
X.true(false);
X.isTrue(false);
} catch (e) {
X.is((e as Error).message, 'Exception found in test #4! The test returned false.');
X.is((e as Error).message, 'Exception found in test #7! The test returned false.');
}

try {
X.ne(1, 1, 'Custom');
} catch (e) {
X.is((e as Error).message, 'Exception found in test #6! Custom');
X.is((e as Error).message, 'Exception found in test #9! Custom');
}

0 comments on commit 19366e0

Please sign in to comment.