diff --git a/lib/dust.js b/lib/dust.js index 6b66566c..e95c0735 100644 --- a/lib/dust.js +++ b/lib/dust.js @@ -200,7 +200,7 @@ } for (;fromIndex < length; fromIndex++) { - if (this[fromIndex] === item) { + if (arr[fromIndex] === item) { return fromIndex; } } diff --git a/test/core.js b/test/core.js index 488519a0..9c280c43 100644 --- a/test/core.js +++ b/test/core.js @@ -118,6 +118,26 @@ exports.coreSetup = function(suite, auto) { unit.fail(err); }); }); + + suite.test("indexInArray", function() { + var unit = this, + arr = ["hello", "world"], + nativeIndexOf = Array.prototype.indexOf, + indexOf; + indexOf = dust.indexInArray(arr, "world"); + unit.equals(indexOf, 1); + indexOf = dust.indexInArray(arr, "foo"); + unit.equals(indexOf, -1); + Array.prototype.indexOf = undefined; + // test indexOf when the array indexOf function is undefined (IE lte 8) + indexOf = dust.indexInArray(arr, "world"); + unit.equals(indexOf, 1); + indexOf = dust.indexInArray(arr, "foo"); + unit.equals(indexOf, -1); + Array.prototype.indexOf = nativeIndexOf; + unit.notEquals(Array.prototype.indexOf, undefined); + unit.pass(); + }); } function testRender(unit, source, context, expected, options, baseContext, error, logMessage) { diff --git a/test/uutest.js b/test/uutest.js index 97415e46..1d744aa9 100644 --- a/test/uutest.js +++ b/test/uutest.js @@ -27,6 +27,14 @@ Test.prototype.equals = function(actual, expected, message) { } } +Test.prototype.notEquals = function(actual, expected, message) { + if (actual === expected) { + var err = new Error(); + if (message) err.message = message; + throw wrapAssertionError(err, actual, expected, "==="); + } +} + Test.prototype.ifError = function(err) { if (err) throw err; }