renix1

AutomatedTests

May 5th, 2020 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function TestAssertException (message) {
  2.     this.name = "TestAssertException";
  3.     this.message = message;
  4.     this.stack = (new Error()).stack;
  5. }
  6.  
  7. TestAssertException.prototype = new Error; // should be instanceof Error now
  8.  
  9. function TestLog(message, style) {
  10.     if (!style) {
  11.         style = "background: black; color: white; padding: 10px;";
  12.     }
  13.  
  14.     const resetStyle = "background: transparent; color: black;";
  15.     console.log(`%c${message}%c`, style, resetStyle);
  16. }
  17.  
  18. class Test {
  19.     describe(description, foo) {
  20.         self = this;
  21.         console.log(description.charAt(0).toUpperCase() + description.slice(1));
  22.         foo(self);
  23.     }
  24.  
  25.     it(description, foo) {
  26.         this.describe(description, foo);
  27.     }
  28.  
  29.     expect (expected) {
  30.         return {
  31.             toBe: function (assertion) {
  32.                 if (expected === assertion) {
  33.                     TestLog("Teste [OK]", "background: palegreen; color: black; padding: 5px;");
  34.                 } else {
  35.                     TestLog("Teste [NOK]", "background: lightcoral; color: white; padding: 5px;");
  36.                     throw new TestAssertException();
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
  42.  
  43. class UnitTests extends Test {
  44.     retornar2 () {
  45.         this.it("deveria retornar 2", function (test) {
  46.             test.expect(2).toBe(2);
  47.         })
  48.     }
  49. }
  50.  
  51. class TestRunner {
  52.     iniRun = 0;
  53.     endRun = 0;
  54.     excCount = 0;
  55.  
  56.     runAll () {
  57.         var unitTests = new UnitTests();
  58.         var methods = Reflect.ownKeys(Reflect.getPrototypeOf(unitTests)).filter(methodName => methodName != "constructor");
  59.         for (var k in methods) {
  60.             try {
  61.                 var method = methods[k];
  62.                 TestLog("Rodando teste [" + method + "]");
  63.                 this.iniRun++;
  64.                 unitTests[method]();
  65.                 this.endRun++;
  66.             } catch (e) {
  67.                 this.excCount++;
  68.             }
  69.         }
  70.  
  71.         TestLog(`${this.endRun} testes passaram`, "background: seagreen; color: white; padding: 5px;");
  72.         TestLog(`${this.excCount} testes falharam`, "background: red; color: white; padding: 5px;");
  73.     }
  74. }
  75.  
  76. var testRunner = new TestRunner();
  77. testRunner.runAll();
Add Comment
Please, Sign In to add comment