diff --git a/app/data/ct.libs/assert/AssertYield.png b/app/data/ct.libs/assert/AssertYield.png new file mode 100644 index 000000000..6cde8baab Binary files /dev/null and b/app/data/ct.libs/assert/AssertYield.png differ diff --git a/app/data/ct.libs/assert/README.md b/app/data/ct.libs/assert/README.md new file mode 100644 index 000000000..c62fbafad --- /dev/null +++ b/app/data/ct.libs/assert/README.md @@ -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');`. \ No newline at end of file diff --git a/app/data/ct.libs/assert/index.js b/app/data/ct.libs/assert/index.js new file mode 100644 index 000000000..682d4eb9f --- /dev/null +++ b/app/data/ct.libs/assert/index.js @@ -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; + }; +} diff --git a/app/data/ct.libs/assert/module.json b/app/data/ct.libs/assert/module.json new file mode 100644 index 000000000..9f088cfc1 --- /dev/null +++ b/app/data/ct.libs/assert/module.json @@ -0,0 +1,11 @@ +{ + "main": { + "name": "ct.assert", + "version": "1.0.0", + "packageName": "assert", + "authors": [{ + "name": "Comigo", + "mail": "admin@nersta.ru" + }] + } +} diff --git a/app/data/ct.libs/assert/types.d.ts b/app/data/ct.libs/assert/types.d.ts new file mode 100644 index 000000000..704bc4126 --- /dev/null +++ b/app/data/ct.libs/assert/types.d.ts @@ -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; + } +} \ No newline at end of file