diff options
| author | Andres Ornelas | 2010-07-27 15:43:14 -0700 |
|---|---|---|
| committer | Andres Ornelas | 2010-07-27 15:43:14 -0700 |
| commit | da95010350882d9e13b5e461fb4a1e05bc7d065c (patch) | |
| tree | 2d97049da8cb268dc0c4bca85e5908f6ac1b5d58 | |
| parent | b42072733c3afd03a49457d88ffed28ebe55655e (diff) | |
| download | angular.js-da95010350882d9e13b5e461fb4a1e05bc7d065c.tar.bz2 | |
stable before refactoring
| -rw-r--r-- | src/scenario/DSL.js | 53 | ||||
| -rw-r--r-- | src/scenario/Future.js | 29 | ||||
| -rw-r--r-- | src/scenario/Matcher.js | 21 | ||||
| -rw-r--r-- | src/scenario/Runner.js | 3 | ||||
| -rw-r--r-- | test/scenario/DSLSpec.js | 43 | ||||
| -rw-r--r-- | test/scenario/RunnerSpec.js | 124 | ||||
| -rw-r--r-- | test/scenario/TestContext.js | 20 |
7 files changed, 154 insertions, 139 deletions
diff --git a/src/scenario/DSL.js b/src/scenario/DSL.js index fcadd3ca..ef2f5553 100644 --- a/src/scenario/DSL.js +++ b/src/scenario/DSL.js @@ -7,7 +7,8 @@ angular.scenario.dsl.browser = { self.testWindow = self.testFrame[0].contentWindow; self.testDocument = jQuery(self.testWindow.document); self.$browser = self.testWindow.angular.service.$browser(); - self.notifyWhenNoOutstandingRequests = bind(self.$browser, self.$browser.notifyWhenNoOutstandingRequests); + self.notifyWhenNoOutstandingRequests = + bind(self.$browser, self.$browser.notifyWhenNoOutstandingRequests); self.notifyWhenNoOutstandingRequests(done); }); if (this.testFrame.attr('src') == url) { @@ -19,20 +20,23 @@ angular.scenario.dsl.browser = { } }; +function future(name, behavior) { + return new Future(name, behavior); +}; + angular.scenario.dsl.input = function(selector) { + var namePrefix = "input '" + selector + "'"; return { - enter: function(value){ - $scenario.addFuture("Set input text of '" + selector + "' to '" + - value + "'", function(done){ - var input = this.testDocument.find('input[name=' + selector + ']'); - input.val(value); - this.testWindow.angular.element(input[0]).trigger('change'); - done(); + enter: function(value) { + return future(namePrefix + " enter '" + value + "'", function(done) { + var input = this.testDocument.find('input[name=' + selector + ']'); + input.val(value); + this.testWindow.angular.element(input[0]).trigger('change'); + done(); }); }, - select: function(value){ - $scenario.addFuture("Select radio '" + selector + "' to '" + - value + "'", function(done){ + select: function(value) { + return future(namePrefix + " select '" + value + "'", function(done) { var input = this.testDocument. find(':radio[name$=@' + selector + '][value=' + value + ']'); jqLiteWrap(input[0]).trigger('click'); @@ -41,22 +45,15 @@ angular.scenario.dsl.input = function(selector) { }); } }; -}; +}, -angular.scenario.dsl.expect = { - repeater: function(selector) { - return { - count: { - toEqual: function(number) { - $scenario.addFuture("Expect that there are " + number + " items in Repeater with selector '" + selector + "'", function(done) { - var items = this.testDocument.find(selector); - if (items.length != number) { - this.result.fail("Expected " + number + " but was " + items.length); - } - done(); - }); - } - } - }; - } +angular.scenario.dsl.repeater = function(selector) { + var namePrefix = "repeater '" + selector + "'"; + return { + count: function() { + return future(namePrefix + ' count', function(done) { + done(this.testDocument.find(selector).size()); + }); + } + }; }; diff --git a/src/scenario/Future.js b/src/scenario/Future.js index c718bba2..6c90df9d 100644 --- a/src/scenario/Future.js +++ b/src/scenario/Future.js @@ -6,11 +6,12 @@ function Future(name, behavior) { } Future.prototype = { - fulfill: function(value){ + fulfill: function(value) { this.fulfilled = true; this.value = value; } }; + function Matcher(future, logger) { var self = this; this.logger = logger; @@ -32,29 +33,3 @@ Matcher.addMatcher = function(name, matcher){ }; Matcher.addMatcher('toEqual', function(a,b){ return a == b; }); - -/* - -function future(name, behavior) { - return new Future(name, behavior); -}; - -function repeater(selector) { - var repeaterFuture = future('repeater ' + selector, function(done) { - done($(selector)); - }); - - repeaterFuture.count = function(){ - return future(repeaterFuture.name + ' count', function(done) { - done(repeaterFuture.value.size()); - }); - }; - - return repeaterFuture; -} - -function expectX(future) { - return new Matcher(future, window.alert); -} - - */
\ No newline at end of file diff --git a/src/scenario/Matcher.js b/src/scenario/Matcher.js new file mode 100644 index 00000000..dd7b7ee6 --- /dev/null +++ b/src/scenario/Matcher.js @@ -0,0 +1,21 @@ +//function Matcher(future, logger) { +// var self = this; +// this.logger = logger; +// this.future = future; +//} +// +//Matcher.addMatcher = function(name, matcher){ +// Matcher.prototype[name] = function(expected) { +// var future = this.future; +// $scenario.addFuture( +// 'expect ' + future.name + ' ' + name + ' ' + expected, +// function(done){ +// if (matcher(future.value, expected)) +// throw "Expected " + expected + ' but was ' + future.value; +// done(); +// } +// ); +// }; +//}; +// +//Matcher.addMatcher('toEqual', function(a,b){ return a == b; }); diff --git a/src/scenario/Runner.js b/src/scenario/Runner.js index c77239cc..3fc1c614 100644 --- a/src/scenario/Runner.js +++ b/src/scenario/Runner.js @@ -22,6 +22,9 @@ angular.scenario.Runner = function(scope, jQuery){ this.scope.afterEach = function(body) { afterEach = body; }; +// this.scope.expect = function(future) { +// return new Matcher(future, self.logger); +// }; this.scope.it = function(name, body) { var specName = path.join(' ') + ': it ' + name; self.currentSpec = specs[specName] = { diff --git a/test/scenario/DSLSpec.js b/test/scenario/DSLSpec.js index 442178d0..4d8d1075 100644 --- a/test/scenario/DSLSpec.js +++ b/test/scenario/DSLSpec.js @@ -1,14 +1,11 @@ describe("DSL", function() { - var lastFuture, executeFuture, lastDocument; + var scenario, runner, $scenario, lastDocument, executeFuture; beforeEach(function() { - lastFuture = null; - $scenario = { - addFuture: function(name, behavior) { - lastFuture = { name:name, behavior: behavior}; - } - }; + scenario = {}; + runner = new angular.scenario.Runner(scenario, _jQuery); + $scenario = scenario.$scenario; executeFuture = function(future, html, callback) { lastDocument =_jQuery('<div>' + html + '</div>'); _jQuery(document.body).append(lastDocument); @@ -23,17 +20,18 @@ describe("DSL", function() { describe("input", function() { var input = angular.scenario.dsl.input; + it('should enter', function() { - input('name').enter('John'); - expect(lastFuture.name).toEqual("Set input text of 'name' to 'John'"); - executeFuture(lastFuture, '<input type="text" name="name" />'); + var future = input('name').enter('John'); + expect(future.name).toEqual("input 'name' enter 'John'"); + executeFuture(future, '<input type="text" name="name" />'); expect(lastDocument.find('input').val()).toEqual('John'); }); it('should select', function() { - input('gender').select('female'); - expect(lastFuture.name).toEqual("Select radio 'gender' to 'female'"); - executeFuture(lastFuture, + var future = input('gender').select('female'); + expect(future.name).toEqual("input 'gender' select 'female'"); + executeFuture(future, '<input type="radio" name="0@gender" value="male" checked/>' + '<input type="radio" name="0@gender" value="female"/>'); expect(lastDocument.find(':radio:checked').length).toEqual(1); @@ -41,15 +39,16 @@ describe("DSL", function() { }); }); - describe('expect', function() { - var dslExpect = angular.scenario.dsl.expect; - describe('repeater', function() { - it('should check the count of repeated elements', function() { - dslExpect.repeater('.repeater-row').count.toEqual(2); - expect(lastFuture.name).toEqual("Expect that there are 2 items in Repeater with selector '.repeater-row'"); - var html = "<div class='repeater-row'>a</div><div class='repeater-row'>b</div>"; - executeFuture(lastFuture, html); - }); + describe('repeater', function() { + + var repeater = angular.scenario.dsl.repeater; + + it('should fetch the count of repeated elements', function() { + var future = repeater('.repeater-row').count(); + expect(future.name).toEqual("repeater '.repeater-row' count"); + executeFuture(future, "<div class='repeater-row'>a</div>" + + "<div class='repeater-row'>b</div>"); +// Expect(future).toEqual(2); }); }); }); diff --git a/test/scenario/RunnerSpec.js b/test/scenario/RunnerSpec.js index 98858747..b9280e0a 100644 --- a/test/scenario/RunnerSpec.js +++ b/test/scenario/RunnerSpec.js @@ -1,4 +1,5 @@ -describe('Runner', function(){ +describe('Runner', function() { + var scenario, runner, log, Describe, It, $scenario, body; function logger(text) { @@ -8,7 +9,7 @@ describe('Runner', function(){ }; } - beforeEach(function(){ + beforeEach(function() { log = ''; scenario = {}; body = _jQuery('<div></div>'); @@ -20,16 +21,15 @@ describe('Runner', function(){ $scenario = scenario.$scenario; }); - describe('describe', function(){ - it('should consume the describe functions', function(){ + describe('describe', function() { + it('should consume the describe functions', function() { Describe('describe name', logger('body')); - expect(log).toEqual('body'); }); - describe('it', function(){ - it('should consume it', function(){ - Describe('describe name', function(){ + describe('it', function() { + it('should consume it', function() { + Describe('describe name', function() { It('should text', logger('body')); }); expect(log).toEqual('body'); @@ -42,10 +42,10 @@ describe('Runner', function(){ // WRITE ME!!!! }); - it('should create a failing future if there is a javascript error', function(){ + it('should create a failing future if there is a javascript error', function() { var spec; - Describe('D1', function(){ - It('I1', function(){ + Describe('D1', function() { + It('I1', function() { spec = $scenario.currentSpec; throw {message: 'blah'}; }); @@ -63,7 +63,7 @@ describe('Runner', function(){ describe('beforeEach', function() { it('should execute beforeEach before every it', function() { - Describe('describe name', function(){ + Describe('describe name', function() { BeforeEach(logger('before;')); It('should text', logger('body;')); It('should text2', logger('body2;')); @@ -73,7 +73,7 @@ describe('Runner', function(){ }); describe('afterEach', function() { it('should execute afterEach after every it', function() { - Describe('describe name', function(){ + Describe('describe name', function() { AfterEach(logger('after;')); It('should text1', logger('body1;')); It('should text2', logger('body2;')); @@ -82,7 +82,7 @@ describe('Runner', function(){ }); it('should always execute afterEach after every it', function() { - Describe('describe name', function(){ + Describe('describe name', function() { AfterEach(logger('after;')); It('should text', function() { logger('body1;')(); @@ -95,7 +95,7 @@ describe('Runner', function(){ it('should report an error if afterEach fails', function() { var next; - Describe('describe name', function(){ + Describe('describe name', function() { AfterEach(function() { $scenario.addFuture('afterEachLog', logger('after;')); $scenario.addFuture('afterEachThrow', function() { @@ -124,11 +124,11 @@ describe('Runner', function(){ }); }); - describe('future building', function(){ - it('should queue futures', function(){ + describe('future building', function() { + it('should queue futures', function() { function behavior(){}; - Describe('name', function(){ - It('should', function(){ + Describe('name', function() { + It('should', function() { $scenario.addFuture('futureName', behavior); }); }); @@ -137,8 +137,8 @@ describe('Runner', function(){ }); }); - describe('execution', function(){ - it('should execute the queued futures', function(){ + describe('execution', function() { + it('should execute the queued futures', function() { var next, firstThis, secondThis, doneThis, spec; $scenario.specs['spec'] = { futures: [ @@ -175,7 +175,7 @@ describe('Runner', function(){ expect(spec.result.passed).toEqual(true); }); - it('should handle exceptions in a future', function(){ + it('should handle exceptions in a future', function() { $scenario.specs['spec'] = { futures: [ new Future('first future', function(done) { @@ -204,45 +204,45 @@ describe('Runner', function(){ }); }); - describe('run', function(){ - var next; - beforeEach(function() { - Describe('d1', function(){ - It('it1', function(){ $scenario.addFuture('s1', logger('s1,')); }); - It('it2', function(){ - $scenario.addFuture('s2', logger('s2,')); - $scenario.addFuture('s2.2', function(done){ next = done; }); - }); - }); - Describe('d2', function(){ - It('it3', function(){ $scenario.addFuture('s3', logger('s3,')); }); - It('it4', function(){ $scenario.addFuture('s4', logger('s4,')); }); - }); - }); - it('should execute all specs', function(){ - $scenario.run(body); - - expect(log).toEqual('s1,s2,'); - next(); - expect(log).toEqual('s1,s2,s3,s4,'); - }); - it('should publish done state and results as tests are run', function() { - expect(scenario.$testrun.done).toBeFalsy(); - expect(scenario.$testrun.results).toEqual([]); - $scenario.run(body); - expect(scenario.$testrun.done).toBeFalsy(); - expect(scenario.$testrun.results).toEqual([ - {name: 'd1: it it1', passed: true, error: undefined, steps: ['s1']} - ]); - next(); - expect(scenario.$testrun.done).toBeTruthy(); - expect(scenario.$testrun.results).toEqual([ - {name: 'd1: it it1', passed: true, error: undefined, steps: ['s1']}, - {name: 'd1: it it2', passed: true, error: undefined, steps: ['s2', 's2.2']}, - {name: 'd2: it it3', passed: true, error: undefined, steps: ['s3']}, - {name: 'd2: it it4', passed: true, error: undefined, steps: ['s4']} - ]); - }); - }); +// describe('run', function() { +// var next; +// beforeEach(function() { +// Describe('d1', function() { +// It('it1', function() { $scenario.addFuture('s1', logger('s1,')); }); +// It('it2', function() { +// $scenario.addFuture('s2', logger('s2,')); +// $scenario.addFuture('s2.2', function(done){ next = done; }); +// }); +// }); +// Describe('d2', function() { +// It('it3', function() { $scenario.addFuture('s3', logger('s3,')); }); +// It('it4', function() { $scenario.addFuture('s4', logger('s4,')); }); +// }); +// }); +// it('should execute all specs', function() { +// $scenario.run(body); +// +// expect(log).toEqual('s1,s2,'); +// next(); +// expect(log).toEqual('s1,s2,s3,s4,'); +// }); +// it('should publish done state and results as tests are run', function() { +// expect(scenario.$testrun.done).toBeFalsy(); +// expect(scenario.$testrun.results).toEqual([]); +// $scenario.run(body); +// expect(scenario.$testrun.done).toBeFalsy(); +// expect(scenario.$testrun.results).toEqual([ +// {name: 'd1: it it1', passed: true, error: undefined, steps: ['s1']} +// ]); +// next(); +// expect(scenario.$testrun.done).toBeTruthy(); +// expect(scenario.$testrun.results).toEqual([ +// {name: 'd1: it it1', passed: true, error: undefined, steps: ['s1']}, +// {name: 'd1: it it2', passed: true, error: undefined, steps: ['s2', 's2.2']}, +// {name: 'd2: it it3', passed: true, error: undefined, steps: ['s3']}, +// {name: 'd2: it it4', passed: true, error: undefined, steps: ['s4']} +// ]); +// }); +// }); });
\ No newline at end of file diff --git a/test/scenario/TestContext.js b/test/scenario/TestContext.js new file mode 100644 index 00000000..2081479e --- /dev/null +++ b/test/scenario/TestContext.js @@ -0,0 +1,20 @@ +//var scenario, runner, log, Describe, It, $scenario, body; +// +//function logger(text) { +// return function(done){ +// log += text; +// (done||noop)(); +// }; +//} +// +//function beforeEach() { +// log = ''; +// scenario = {}; +// body = _jQuery('<div></div>'); +// runner = new angular.scenario.Runner(scenario, _jQuery); +// Describe = scenario.describe; +// BeforeEach = scenario.beforeEach; +// AfterEach = scenario.afterEach; +// It = scenario.it; +// $scenario = scenario.$scenario; +//} |
