diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index eb9eb5b5..8bd2f92a 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -16,7 +16,8 @@ Please submit pull requests via feature branches using the semi-standard workflo git clone git@github.com:yourUserName/jasmine.git # Clone your fork cd jasmine # Change directory git remote add upstream https://github.com/jasmine/jasmine.git # Assign original repository to a remote named 'upstream' -git fetch upstream # Pull in changes not present in your local repository +git fetch upstream # Fetch changes not present in your local repository +git merge upstream/master # Sync local master with upstream repository git checkout -b my-new-feature # Create your feature branch git commit -am 'Add some feature' # Commit your changes git push origin my-new-feature # Push to the branch diff --git a/.travis.yml b/.travis.yml index 41d697fa..622273cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,16 +24,16 @@ matrix: include: - env: - USE_SAUCE=false - - NODE_VERSION="4.8.6" - - TEST_COMMAND="bash travis-node-script.sh" + - TEST_COMMAND="bash travis-node-script.sh v0.12.18" - env: - USE_SAUCE=false - - NODE_VERSION="8.9.1" - - TEST_COMMAND="bash travis-node-script.sh" + - TEST_COMMAND="bash travis-node-script.sh v4" - env: - USE_SAUCE=false - - NODE_VERSION="9.1.0" - - TEST_COMMAND="bash travis-node-script.sh" + - TEST_COMMAND="bash travis-node-script.sh v8" + - env: + - USE_SAUCE=false + - TEST_COMMAND="bash travis-node-script.sh v9" - env: - JASMINE_BROWSER="safari" - SAUCE_OS="OS X 10.11" @@ -58,6 +58,14 @@ matrix: - JASMINE_BROWSER="internet explorer" - SAUCE_OS="Windows 8" - SAUCE_BROWSER_VERSION=10 + - env: + - JASMINE_BROWSER="internet explorer" + - SAUCE_OS="Windows 7" + - SAUCE_BROWSER_VERSION=9 + - env: + - JASMINE_BROWSER="internet explorer" + - SAUCE_OS="Windows 7" + - SAUCE_BROWSER_VERSION=8 - env: - JASMINE_BROWSER="chrome" - SAUCE_OS="Linux" diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 4d4bdb52..19208da3 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -217,6 +217,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { return obj.nodeType > 0; }; + j$.isPromise = function(obj) { + return typeof jasmineGlobal.Promise !== 'undefined' && obj.constructor === jasmineGlobal.Promise; + }; + j$.fnNameFor = function(func) { if (func.name) { return func.name; @@ -2660,6 +2664,12 @@ getJasmineRequireObj().matchersUtil = function(j$) { diffBuilder.record(a, b); return false; } + + var aIsPromise = j$.isPromise(a); + var bIsPromise = j$.isPromise(b); + if (aIsPromise && bIsPromise) { + return a === b; + } // Assume equality for cyclic structures. The algorithm for detecting cyclic // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`. @@ -2743,24 +2753,36 @@ getJasmineRequireObj().matchersUtil = function(j$) { // For both sets, check they are all contained in the other set var setPairs = [[a, b], [b, a]]; - var baseIter, baseValueIt, baseValue; - var otherSet, otherIter, otherValueIt, otherValue, found; + var stackPairs = [[aStack, bStack], [bStack, aStack]]; + var baseIter, baseValueIt, baseValue, baseStack; + var otherSet, otherIter, otherValueIt, otherValue, otherStack; + var found; + var prevStackSize; for (i = 0; result && i < setPairs.length; i++) { baseIter = setPairs[i][0].values(); otherSet = setPairs[i][1]; + baseStack = stackPairs[i][0]; + otherStack = stackPairs[i][1]; // For each value in the base set... baseValueIt = baseIter.next(); while (result && !baseValueIt.done) { baseValue = baseValueIt.value; // ... test that it is present in the other set - otherIter = otherSet.values(); - otherValueIt = otherIter.next(); // Optimisation: start looking for value by object identity found = otherSet.has(baseValue); + if (!found) { + otherIter = otherSet.values(); + otherValueIt = otherIter.next(); + } // If not found, compare by value equality while (!found && !otherValueIt.done) { otherValue = otherValueIt.value; - found = eq(baseValue, otherValue, aStack, bStack, customTesters, j$.NullDiffBuilder()); + prevStackSize = baseStack.length; + found = eq(baseValue, otherValue, baseStack, otherStack, customTesters, j$.NullDiffBuilder()); + if (!found && prevStackSize !== baseStack.length) { + baseStack.splice(prevStackSize); + otherStack.splice(prevStackSize); + } otherValueIt = otherIter.next(); } result = result && found; @@ -4990,7 +5012,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) { * @param {Function} fn The function to invoke with the passed parameters. */ this.callFake = function(fn) { - if(!j$.isFunction_(fn)) { + if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) { throw new Error('Argument passed to callFake should be a function, got ' + fn); } plan = fn; diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js index 0965c04c..16f1af18 100644 --- a/spec/core/SpyStrategySpec.js +++ b/spec/core/SpyStrategySpec.js @@ -92,12 +92,35 @@ describe("SpyStrategy", function() { expect(returnValue).toEqual(67); }); + it("allows a fake async function to be called instead", function(done) { + jasmine.getEnv().requireAsyncAwait(); + var originalFn = jasmine.createSpy("original"), + fakeFn = jasmine.createSpy("fake").and.callFake(eval("async () => { return 67; }")), + spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}), + returnValue; + + spyStrategy.callFake(fakeFn); + spyStrategy.exec().then(function (returnValue) { + expect(originalFn).not.toHaveBeenCalled(); + expect(fakeFn).toHaveBeenCalled(); + expect(returnValue).toEqual(67); + done(); + }).catch(function (err) { + done.fail(err); + }) + }); + it('throws an error when a non-function is passed to callFake strategy', function() { var originalFn = jasmine.createSpy('original'), spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}), invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/]; spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false); + spyOn(jasmineUnderTest, 'isAsyncFunction_').and.returnValue(false); + + expect(function () { + spyStrategy.callFake(function() {}); + }).toThrowError(/^Argument passed to callFake should be a function, got/); expect(function () { spyStrategy.callFake(function() {}); diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index de2dae30..b0297ca7 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -164,6 +164,16 @@ describe("matchersUtil", function() { expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true); }); + + it("passes for equivalent Promises (GitHub issue #1314)", function() { + if (typeof Promise === 'undefined') { return; } + + var p1 = new Promise(function () {}), + p2 = new Promise(function () {}); + + expect(jasmineUnderTest.matchersUtil.equals(p1, p1)).toBe(true); + expect(jasmineUnderTest.matchersUtil.equals(p1, p2)).toBe(false); + }); describe("when running in a browser", function() { function isNotRunningInBrowser() { @@ -383,13 +393,27 @@ describe("matchersUtil", function() { expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); }); - it("passes when comparing identical sets with different insertion order", function() { + it("passes when comparing identical sets with different insertion order and simple elements", function() { jasmine.getEnv().requireFunctioningSets(); var setA = new Set([3, 6]); var setB = new Set([6, 3]); expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); }); + it("passes when comparing identical sets with different insertion order and complex elements 1", function() { + jasmine.getEnv().requireFunctioningMaps(); + var setA = new Set([new Set([['a', 3], [6, 1]]), new Set([['y', 3], [6, 1]])]); + var setB = new Set([new Set([[6, 1], ['a', 3]]), new Set([[6, 1], ['y', 3]])]); + expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); + }); + + it("passes when comparing identical sets with different insertion order and complex elements 2", function() { + jasmine.getEnv().requireFunctioningMaps(); + var setA = new Set([[[1,2], [3,4]], [[5,6], [7,8]]]); + var setB = new Set([[[5,6], [7,8]], [[1,2], [3,4]]]); + expect(jasmineUnderTest.matchersUtil.equals(setA, setB)).toBe(true); + }); + it("fails for sets with different elements", function() { jasmine.getEnv().requireFunctioningSets(); var setA = new Set([6, 3, 5]); diff --git a/src/core/SpyStrategy.js b/src/core/SpyStrategy.js index 5af7cbee..b0c716f7 100644 --- a/src/core/SpyStrategy.js +++ b/src/core/SpyStrategy.js @@ -88,7 +88,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) { * @param {Function} fn The function to invoke with the passed parameters. */ this.callFake = function(fn) { - if(!j$.isFunction_(fn)) { + if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) { throw new Error('Argument passed to callFake should be a function, got ' + fn); } plan = fn; diff --git a/src/core/base.js b/src/core/base.js index bfd9958f..ea09cb34 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -87,6 +87,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { return obj.nodeType > 0; }; + j$.isPromise = function(obj) { + return typeof jasmineGlobal.Promise !== 'undefined' && obj.constructor === jasmineGlobal.Promise; + }; + j$.fnNameFor = function(func) { if (func.name) { return func.name; diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index c05f2a83..d01cc868 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -192,6 +192,12 @@ getJasmineRequireObj().matchersUtil = function(j$) { diffBuilder.record(a, b); return false; } + + var aIsPromise = j$.isPromise(a); + var bIsPromise = j$.isPromise(b); + if (aIsPromise && bIsPromise) { + return a === b; + } // Assume equality for cyclic structures. The algorithm for detecting cyclic // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`. @@ -275,24 +281,36 @@ getJasmineRequireObj().matchersUtil = function(j$) { // For both sets, check they are all contained in the other set var setPairs = [[a, b], [b, a]]; - var baseIter, baseValueIt, baseValue; - var otherSet, otherIter, otherValueIt, otherValue, found; + var stackPairs = [[aStack, bStack], [bStack, aStack]]; + var baseIter, baseValueIt, baseValue, baseStack; + var otherSet, otherIter, otherValueIt, otherValue, otherStack; + var found; + var prevStackSize; for (i = 0; result && i < setPairs.length; i++) { baseIter = setPairs[i][0].values(); otherSet = setPairs[i][1]; + baseStack = stackPairs[i][0]; + otherStack = stackPairs[i][1]; // For each value in the base set... baseValueIt = baseIter.next(); while (result && !baseValueIt.done) { baseValue = baseValueIt.value; // ... test that it is present in the other set - otherIter = otherSet.values(); - otherValueIt = otherIter.next(); // Optimisation: start looking for value by object identity found = otherSet.has(baseValue); + if (!found) { + otherIter = otherSet.values(); + otherValueIt = otherIter.next(); + } // If not found, compare by value equality while (!found && !otherValueIt.done) { otherValue = otherValueIt.value; - found = eq(baseValue, otherValue, aStack, bStack, customTesters, j$.NullDiffBuilder()); + prevStackSize = baseStack.length; + found = eq(baseValue, otherValue, baseStack, otherStack, customTesters, j$.NullDiffBuilder()); + if (!found && prevStackSize !== baseStack.length) { + baseStack.splice(prevStackSize); + otherStack.splice(prevStackSize); + } otherValueIt = otherIter.next(); } result = result && found; diff --git a/travis-node-script.sh b/travis-node-script.sh index a5897a16..fe3859b7 100644 --- a/travis-node-script.sh +++ b/travis-node-script.sh @@ -4,7 +4,7 @@ rm -rf ~/.nvm git clone https://github.com/creationix/nvm.git ~/.nvm (cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) source ~/.nvm/nvm.sh -nvm install $NODE_VERSION +nvm install ${1:-"v0.12.18"} npm install npm test