Skip to content

Instantly share code, notes, and snippets.

@unders
Forked from freedmand/tester.js
Created November 3, 2018 18:43
Show Gist options
  • Select an option

  • Save unders/ee6a50eabce3dbea57ee4cb0c59e033a to your computer and use it in GitHub Desktop.

Select an option

Save unders/ee6a50eabce3dbea57ee4cb0c59e033a to your computer and use it in GitHub Desktop.

Revisions

  1. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tester.js
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ class Tester {
    }

    assert(condition) {
    this.assertEquals(condition, true);
    if (!condition) throw new Error(`Expected ${condition} to be truthy`);
    }
    }

  2. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion tester.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,6 @@ class Tester {


    /*
    Usage:
    const t = new Tester();
  3. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tester.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,9 @@ class Tester {
    }
    }


    /*
    Usage:
    const t = new Tester();
  4. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tester.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    const PASS = ['32']; // green
    const FAIL = ['31', '1']; // red, bold
    const PASS = ['32']; // green
    const FAIL = ['31', '1']; // red, bold

    function logStyle(ansiEscapeCodes, text) {
    console.log(`\x1b[${ansiEscapeCodes.join(';')}m${text}\x1b[0m`);
  5. @freedmand freedmand revised this gist Nov 3, 2018. No changes.
  6. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tester.js
    Original file line number Diff line number Diff line change
    @@ -32,9 +32,11 @@ class Tester {
    Usage:
    const t = new Tester();
    t.test('testName', () => {
    t.assertEquals('dog'.length, 3); // will pass
    });
    t.test('anotherTestName', () => {
    t.assert(false); // will fail
    });
  7. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions tester.js
    Original file line number Diff line number Diff line change
    @@ -29,12 +29,14 @@ class Tester {
    }

    /*
    Usage (try it in a web inspector console):
    Usage:
    const t = new Tester();
    t.test('testName', () => {
    t.assertEquals('dog'.length, 3);
    t.assert(false); // will fail
    t.assertEquals('dog'.length, 3); // will pass
    });
    t.test('anotherTestName', () => {
    t.assert(false); // will fail
    });
    */
  8. @freedmand freedmand revised this gist Nov 3, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions tester.js
    Original file line number Diff line number Diff line change
    @@ -19,13 +19,13 @@ class Tester {
    }
    }

    assert(condition) {
    if (!condition) throw new Error(`Expected ${condition} to be true`);
    }

    assertEquals(arg1, arg2) {
    if (arg1 != arg2) throw new Error(`Expected ${arg1} to equal ${arg2}`);
    }

    assert(condition) {
    this.assertEquals(condition, true);
    }
    }

    /*
  9. @freedmand freedmand created this gist Nov 3, 2018.
    40 changes: 40 additions & 0 deletions tester.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const PASS = ['32']; // green
    const FAIL = ['31', '1']; // red, bold

    function logStyle(ansiEscapeCodes, text) {
    console.log(`\x1b[${ansiEscapeCodes.join(';')}m${text}\x1b[0m`);
    }

    class Tester {
    constructor() {}

    test(name, fn) {
    try {
    fn.bind(this)();
    logStyle(PASS, `${name} PASSED`);
    }
    catch (e) {
    logStyle(FAIL, `${name} FAILED:
    ${e}`);
    }
    }

    assert(condition) {
    if (!condition) throw new Error(`Expected ${condition} to be true`);
    }

    assertEquals(arg1, arg2) {
    if (arg1 != arg2) throw new Error(`Expected ${arg1} to equal ${arg2}`);
    }
    }

    /*
    Usage (try it in a web inspector console):
    const t = new Tester();
    t.test('testName', () => {
    t.assertEquals('dog'.length, 3);
    t.assert(false); // will fail
    });
    */