diff options
author | Geoff Gustafson <geoff@linux.intel.com> | 2017-01-25 11:53:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-25 11:53:09 -0800 |
commit | fefab5fb881f2df69e2497fe3a4bc7faf21f44bf (patch) | |
tree | efe60533c3505a7376852847bcf3ece353fb14ea /modules | |
parent | a5fda85d632a29db052348b12bb029d070367bd4 (diff) |
[assert] Make the QA Assert module match Node APIs better (#650)
This improves the basic syntax to:
var assert = require('Assert');
assert(true, "successful assertion");
assert(false, "failed assertion");
Also allow any "truthy" value to pass. If you explicitly want to check for
exact equality with true, then do:
assert(operation() === true, "perform operation");
Relax about argument types; equal can check non-object arguments. The
description argument can be a non-string or doesn't have to be given.
Swapped argument order in assert.throws() to match Node API.
Signed-off-by: Geoff Gustafson <geoff@linux.intel.com>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/Assert.js | 53 |
1 files changed, 14 insertions, 39 deletions
diff --git a/modules/Assert.js b/modules/Assert.js index 945d67f..d0f2856 100644 --- a/modules/Assert.js +++ b/modules/Assert.js @@ -3,54 +3,27 @@ // JavaScript library for the tests case function Assert() { - // API object - var assert = {}; - - assert.total = 0; - assert.pass = 0; - - assert.true = function (actual, description) { - if (typeof actual !== "boolean" || - typeof description !== "string") { - console.log("AssertionError: invalid input type given"); - throw new Error(); - return; - } - - this.total++; + var assert = function (actual, description) { + var thisfunc = arguments.callee; + thisfunc.total++; var label = "\033[1m\033[31mFAIL\033[0m"; - if (actual === true) { - this.pass++; + if (actual) { + thisfunc.pass++; label = "\033[1m\033[32mPASS\033[0m"; } + if (description === undefined) + description = 'no description provided'; console.log(label + " - " + description); } assert.equal = function (argA, argB, description) { - if (typeof argA !== "object" || - typeof argA === "undefined" || - typeof argB !== "object" || - typeof argB === "undefined" || - typeof description !== "string") { - console.log("AssertionError: invalid input type given"); - throw new Error(); - return; - } - - this.true(argA === argB, description); + return this(argA === argB, description); } - assert.throws = function (description, func) { - if (typeof description !== "string" || - typeof func !== "function") { - console.log("AssertionError: invalid input type given"); - throw new Error(); - return; - } - + assert.throws = function (func, description) { var booleanValue = false; try { @@ -60,14 +33,16 @@ function Assert() { booleanValue = true; } - this.true(booleanValue, description); + return this(booleanValue, description); } assert.result = function () { - console.log("TOTAL: " + this.pass + " of " - + this.total + " passed"); + console.log("TOTAL: " + this.pass + " of " + this.total + " passed"); } + assert.total = 0; + assert.pass = 0; + return assert; }; |