From 3332f93a2407993cd2be4d0d8b4f89bfadb99440 Mon Sep 17 00:00:00 2001 From: Greg Cobb Date: Fri, 5 Sep 2014 12:25:28 -0700 Subject: [PATCH] Only run focused runnables inside focused suites - Focused runnables now walk up the tree to unfocus the first focused ancestor. Because of the way the tree is constructed, this makes sure that each focused runnable has no focused ancestors. [#78289686] --- lib/jasmine-core/jasmine.js | 31 +++++++++----- spec/core/integration/SpecRunningSpec.js | 51 ++++++++++++++++++++++-- src/core/Env.js | 33 ++++++++++----- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 61f83f34..e33c4a67 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -646,11 +646,11 @@ getJasmineRequireObj().Env = function(j$) { this.fdescribe = function(description, specDefinitions) { var suite = suiteFactory(description); suite.isFocused = true; + + focusedRunnables.push(suite.id); + unfocusAncestors(); addSpecsToSuite(suite, specDefinitions); - if (!hasFocusedAncestor(suite.parentSuite)) { - focusedRunnables.push(suite.id); - } return suite; }; @@ -675,15 +675,27 @@ getJasmineRequireObj().Env = function(j$) { currentDeclarationSuite = parentSuite; } - function hasFocusedAncestor(suite) { + function findFocusedAncestor(suite) { while (suite) { if (suite.isFocused) { - return true; + return suite.id; } suite = suite.parentSuite; } - return false; + return null; + } + + function unfocusAncestors() { + var focusedAncestor = findFocusedAncestor(currentDeclarationSuite); + if (focusedAncestor) { + for (var i = 0; i < focusedRunnables.length; i++) { + if (focusedRunnables[i] === focusedAncestor) { + focusedRunnables.splice(i, 1); + break; + } + } + } } var runnablesExplictlySet = false; @@ -744,13 +756,12 @@ getJasmineRequireObj().Env = function(j$) { }; var focusedRunnables = []; + this.fit = function(description, fn ){ var spec = this.it(description, fn); - if (!hasFocusedAncestor(currentDeclarationSuite)) { - focusedRunnables.push(spec.id); - } - + focusedRunnables.push(spec.id); + unfocusAncestors(); return spec; }; diff --git a/spec/core/integration/SpecRunningSpec.js b/spec/core/integration/SpecRunningSpec.js index 6ed4604a..e1675d00 100644 --- a/spec/core/integration/SpecRunningSpec.js +++ b/spec/core/integration/SpecRunningSpec.js @@ -408,23 +408,68 @@ describe("jasmine spec running", function () { env.execute(); }); - it('runs fits and fdescribes in fdescribes only once', function(done){ + it('focused specs in focused suites cause non-focused siblings to not run', function(done){ var actions = []; env.fdescribe('focused suite', function() { + env.it('unfocused spec', function() { + actions.push('unfocused spec') + }); env.fit('focused spec', function() { actions.push('focused spec') }); + }); + var assertions = function() { + var expected = ['focused spec']; + expect(actions).toEqual(expected); + done(); + }; + + env.addReporter({jasmineDone: assertions}); + env.execute(); + }); + + it('focused suites in focused suites cause non-focused siblings to not run', function(done){ + var actions = []; + + env.fdescribe('focused suite', function() { + env.it('unfocused spec', function() { + actions.push('unfocused spec') + }); env.fdescribe('inner focused suite', function() { env.it('inner spec', function() { - actions.push('unfocused spec'); + actions.push('inner spec'); }); }); }); var assertions = function() { - var expected = ['focused spec', 'unfocused spec']; + var expected = ['inner spec']; + expect(actions).toEqual(expected); + done(); + }; + + env.addReporter({jasmineDone: assertions}); + env.execute(); + }); + + it('focused runnables unfocus ancestor focused suites', function() { + var actions = []; + + env.fdescribe('focused suite', function() { + env.it('unfocused spec', function() { + actions.push('unfocused spec') + }); + env.describe('inner focused suite', function() { + env.fit('focused spec', function() { + actions.push('focused spec'); + }); + }); + }); + + var assertions = function() { + var expected = ['focused spec']; expect(actions).toEqual(expected); done(); }; diff --git a/src/core/Env.js b/src/core/Env.js index 7e676d4d..c7a1c175 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -270,14 +270,16 @@ getJasmineRequireObj().Env = function(j$) { return suite; }; + var focusedRunnables = []; + this.fdescribe = function(description, specDefinitions) { var suite = suiteFactory(description); suite.isFocused = true; + + focusedRunnables.push(suite.id); + unfocusAncestor(); addSpecsToSuite(suite, specDefinitions); - if (!hasFocusedAncestor(suite.parentSuite)) { - focusedRunnables.push(suite.id); - } return suite; }; @@ -302,15 +304,27 @@ getJasmineRequireObj().Env = function(j$) { currentDeclarationSuite = parentSuite; } - function hasFocusedAncestor(suite) { + function findFocusedAncestor(suite) { while (suite) { if (suite.isFocused) { - return true; + return suite.id; } suite = suite.parentSuite; } - return false; + return null; + } + + function unfocusAncestor() { + var focusedAncestor = findFocusedAncestor(currentDeclarationSuite); + if (focusedAncestor) { + for (var i = 0; i < focusedRunnables.length; i++) { + if (focusedRunnables[i] === focusedAncestor) { + focusedRunnables.splice(i, 1); + break; + } + } + } } var runnablesExplictlySet = false; @@ -370,14 +384,11 @@ getJasmineRequireObj().Env = function(j$) { return spec; }; - var focusedRunnables = []; this.fit = function(description, fn ){ var spec = this.it(description, fn); - if (!hasFocusedAncestor(currentDeclarationSuite)) { - focusedRunnables.push(spec.id); - } - + focusedRunnables.push(spec.id); + unfocusAncestor(); return spec; };