Skip to content

Commit

Permalink
Merge branch 'release/v0.4.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
myurasov committed Mar 17, 2016
2 parents de4a02f + dd6a851 commit c4620ed
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

Unit Testing Framework for EI/Squirrel.

_Version: 0.4.1_
_Version: 0.4.2_

## License

Expand Down
46 changes: 29 additions & 17 deletions dist/impUnit.nut
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* impUnit Test Framework
*
* @author Mikhail Yurasov <[email protected]>
* @version 0.4.1
* @version 0.4.2
* @package ImpUnit
*/

Expand Down Expand Up @@ -515,37 +515,36 @@ local ImpTestCase = class {

/**
* Perform a deep comparison of two values
* Useful for comparing arrays or tables
* @param {*} expected
* @param {*} actual
* @param {*} value1
* @param {*} value2
* @param {string} message
* @param {boolean} isForwardPass - on forward pass value1 is treated "expected", value2 as "actual" and vice-versa on backward pass
* @param {string} path - current slot path
* @param {int} level - current depth level
* @private
*/
function assertDeepEqual(expected, actual, message = "At [%s]: expected \"%s\", got \"%s\"", path = "", level = 0) {

if (0 == level) {
this.assertions++;
}

function _assertDeepEqual(value1, value2, message, isForwardPass, path = "", level = 0) {
local cleanPath = @(p) p.len() == 0 ? p : p.slice(1);

if (level > 32) {
throw "Possible cyclic reference at " + cleanPath(path);
}

switch (type(actual)) {
switch (type(value1)) {
case "table":
case "class":
case "array":

foreach (k, v in expected) {
foreach (k, v in value2) {

path += "." + k;

if (!(k in actual)) {
throw format("Missing slot [%s] in actual value", cleanPath(path));
if (!(k in value1)) {
throw format("%s slot [%s] in actual value",
isForwardPass ? "Missing" : "Extra", cleanPath(path));
}

this.assertDeepEqual(expected[k], actual[k], message, path, level + 1);
this._assertDeepEqual(value2[k], value1[k], message, isForwardPass, path, level + 1);
}

break;
Expand All @@ -554,14 +553,27 @@ local ImpTestCase = class {
break;

default:
if (expected != actual) {
throw format(message, cleanPath(path), expected + "", actual + "");
if (value2 != value1) {
throw format(message, cleanPath(path), value2 + "", value1 + "");
}

break;
}
}

/**
* Perform a deep comparison of two values
* Useful for comparing arrays or tables
* @param {*} expected
* @param {*} actual
* @param {string} message
*/
function assertDeepEqual(expected, actual, message = "At [%s]: expected \"%s\", got \"%s\"") {
this.assertions++;
this._assertDeepEqual(expected, actual, message, true); // forward pass
this._assertDeepEqual(actual, expected, message, false); // backwards pass
}

/**
* Assert that the value is between min amd max
* @param {number|*} actual
Expand Down
2 changes: 1 addition & 1 deletion index.nut
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* impUnit Test Framework
*
* @author Mikhail Yurasov <[email protected]>
* @version 0.4.1
* @version 0.4.2
* @package ImpUnit
*/

Expand Down
44 changes: 28 additions & 16 deletions src/TestCase.nut
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,36 @@ local ImpTestCase = class {

/**
* Perform a deep comparison of two values
* Useful for comparing arrays or tables
* @param {*} expected
* @param {*} actual
* @param {*} value1
* @param {*} value2
* @param {string} message
* @param {boolean} isForwardPass - on forward pass value1 is treated "expected", value2 as "actual" and vice-versa on backward pass
* @param {string} path - current slot path
* @param {int} level - current depth level
* @private
*/
function assertDeepEqual(expected, actual, message = "At [%s]: expected \"%s\", got \"%s\"", path = "", level = 0) {

if (0 == level) {
this.assertions++;
}

function _assertDeepEqual(value1, value2, message, isForwardPass, path = "", level = 0) {
local cleanPath = @(p) p.len() == 0 ? p : p.slice(1);

if (level > 32) {
throw "Possible cyclic reference at " + cleanPath(path);
}

switch (type(actual)) {
switch (type(value1)) {
case "table":
case "class":
case "array":

foreach (k, v in expected) {
foreach (k, v in value2) {

path += "." + k;

if (!(k in actual)) {
throw format("Missing slot [%s] in actual value", cleanPath(path));
if (!(k in value1)) {
throw format("%s slot [%s] in actual value",
isForwardPass ? "Missing" : "Extra", cleanPath(path));
}

this.assertDeepEqual(expected[k], actual[k], message, path, level + 1);
this._assertDeepEqual(value2[k], value1[k], message, isForwardPass, path, level + 1);
}

break;
Expand All @@ -123,14 +122,27 @@ local ImpTestCase = class {
break;

default:
if (expected != actual) {
throw format(message, cleanPath(path), expected + "", actual + "");
if (value2 != value1) {
throw format(message, cleanPath(path), value2 + "", value1 + "");
}

break;
}
}

/**
* Perform a deep comparison of two values
* Useful for comparing arrays or tables
* @param {*} expected
* @param {*} actual
* @param {string} message
*/
function assertDeepEqual(expected, actual, message = "At [%s]: expected \"%s\", got \"%s\"") {
this.assertions++;
this._assertDeepEqual(expected, actual, message, true); // forward pass
this._assertDeepEqual(actual, expected, message, false); // backwards pass
}

/**
* Assert that the value is between min amd max
* @param {number|*} actual
Expand Down

0 comments on commit c4620ed

Please sign in to comment.