-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨
ct.assert
module for readable checks in ct.js projects
- Loading branch information
1 parent
b630b7d
commit 585b1ee
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
A tiny module that provides a method `ct.assert(condition, message)` to help making readable tests in ct.js projects. | ||
|
||
The `condition` may be either boolean or a function — other values (numbers, strings) will fail. Functions are executed first and then tested against their returned result. | ||
|
||
There is also a `ct.assert.summary();` call, that shows counted amount of passed and failed tests. | ||
|
||
Usage example: | ||
|
||
```js | ||
ct.assert( | ||
ct.inherit.isParent('AbstractMonster', 'Monster_Red_Squished'), | ||
'ct.inherit.isParent works with two types (two strings)' | ||
); | ||
ct.assert( | ||
ct.inherit.isChild('Monster_Green', 'AbstractMonster'), | ||
'ct.inherit.isChild works with two types (two strings)' | ||
); | ||
ct.assert( | ||
ct.inherit.list('AbstractMonster').length === 2, | ||
'ct.inherit.list gets all the monsters' | ||
); | ||
ct.assert( | ||
ct.inherit.isChild(ct.types.list['Monster_Green'][0], 'AbstractMonster'), | ||
'ct.inherit.isChild works against a copy and a type' | ||
); | ||
ct.assert( | ||
ct.inherit.isChild(ct.types.list['Monster_Red_Squished'][0], ct.types.list['Monster_Red'][0]), | ||
'ct.inherit.isChild works against two copies' | ||
); | ||
ct.assert.summary(); | ||
``` | ||
|
||
This will yield: | ||
|
||
 | ||
|
||
## Grouping results and measuring time | ||
|
||
You still have all the powers of browser's asserting and logging tools. | ||
|
||
If you want to group test results, use `console.group('Label');` and `console.groupEnd();`. | ||
|
||
If you want to measure the amount of time somethign takes to compute, use `console.time('Label')`; and `console.timeEnd('Label');`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
let passed = 0, | ||
failed = 0; | ||
ct.assert = function assert(condition, message) { | ||
let result = condition; | ||
if (condition instanceof Function) { | ||
try { | ||
result = condition(); | ||
} catch (e) { | ||
console.error(`%c Got an execution error while evaluating%c${message ? ':\n' + message : ''}`, 'font-weigth: bold;', ''); | ||
failed++; | ||
throw e; | ||
} | ||
} | ||
if (typeof result !== 'boolean') { | ||
console.error(`%c Not a boolean%c${message ? ':\n' + message : ''}\nGot this value:`, 'font-weigth: bold;', ''); | ||
// eslint-disable-next-line no-console | ||
console.log(result); | ||
failed++; | ||
} | ||
if (result) { | ||
// eslint-disable-next-line no-console | ||
console.log(`%c✅ Passed%c${message ? ':\n' + message : ''}`, 'color: #3c3; font-weight: bold;', ''); | ||
passed++; | ||
} else { | ||
console.error(`%c Failed%c${message ? ':\n' + message : ''}`, 'font-weigth: bold;', ''); | ||
failed++; | ||
} | ||
}; | ||
ct.assert.summary = function summary() { | ||
if (failed > 0) { | ||
console.error(`%c Failed: ${failed}, passed: ${passed}.`, 'font-weight: bold;'); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.log(`%c✅ Failed: ${failed}, passed: ${passed}.`, 'color: #3c3; font-weight: bold;'); | ||
} | ||
failed = passed = 0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"main": { | ||
"name": "ct.assert", | ||
"version": "1.0.0", | ||
"packageName": "assert", | ||
"authors": [{ | ||
"name": "Comigo", | ||
"mail": "[email protected]" | ||
}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare namespace ct { | ||
/** | ||
* A tiny module to simplify tests in ct.js projects. | ||
* @param condition may be either boolean or a function — other values (numbers, strings) will fail. Functions are executed first and then tested against their returned result. | ||
* @param message The message to show in console. | ||
*/ | ||
function assert(condition: boolean | Function, message: string): void; | ||
|
||
namespace assert { | ||
/** | ||
* Displays a summary with a number of failed and passed tasks, and resets the counter of failed and passed tasks. | ||
*/ | ||
function summary(): void; | ||
} | ||
} |