Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TestAssertException (message) {
- this.name = "TestAssertException";
- this.message = message;
- this.stack = (new Error()).stack;
- }
- TestAssertException.prototype = new Error; // should be instanceof Error now
- function TestLog(message, style) {
- if (!style) {
- style = "background: black; color: white; padding: 10px;";
- }
- const resetStyle = "background: transparent; color: black;";
- console.log(`%c${message}%c`, style, resetStyle);
- }
- class Test {
- describe(description, foo) {
- self = this;
- console.log(description.charAt(0).toUpperCase() + description.slice(1));
- foo(self);
- }
- it(description, foo) {
- this.describe(description, foo);
- }
- expect (expected) {
- return {
- toBe: function (assertion) {
- if (expected === assertion) {
- TestLog("Teste [OK]", "background: palegreen; color: black; padding: 5px;");
- } else {
- TestLog("Teste [NOK]", "background: lightcoral; color: white; padding: 5px;");
- throw new TestAssertException();
- }
- }
- }
- }
- }
- class UnitTests extends Test {
- retornar2 () {
- this.it("deveria retornar 2", function (test) {
- test.expect(2).toBe(2);
- })
- }
- }
- class TestRunner {
- iniRun = 0;
- endRun = 0;
- excCount = 0;
- runAll () {
- var unitTests = new UnitTests();
- var methods = Reflect.ownKeys(Reflect.getPrototypeOf(unitTests)).filter(methodName => methodName != "constructor");
- for (var k in methods) {
- try {
- var method = methods[k];
- TestLog("Rodando teste [" + method + "]");
- this.iniRun++;
- unitTests[method]();
- this.endRun++;
- } catch (e) {
- this.excCount++;
- }
- }
- TestLog(`${this.endRun} testes passaram`, "background: seagreen; color: white; padding: 5px;");
- TestLog(`${this.excCount} testes falharam`, "background: red; color: white; padding: 5px;");
- }
- }
- var testRunner = new TestRunner();
- testRunner.runAll();
Add Comment
Please, Sign In to add comment