Skip to content

Commit

Permalink
ct.assert module for readable checks in ct.js projects
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoMyzrailGorynych committed Jul 30, 2020
1 parent b630b7d commit 585b1ee
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
Binary file added app/data/ct.libs/assert/AssertYield.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions app/data/ct.libs/assert/README.md
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:

![](./data/ct.libs/assert/AssertYield.png)

## 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');`.
39 changes: 39 additions & 0 deletions app/data/ct.libs/assert/index.js
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;
};
}
11 changes: 11 additions & 0 deletions app/data/ct.libs/assert/module.json
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]"
}]
}
}
15 changes: 15 additions & 0 deletions app/data/ct.libs/assert/types.d.ts
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;
}
}

0 comments on commit 585b1ee

Please sign in to comment.