From 420c8f07c1017303f981b78b907c5c5b821b191d Mon Sep 17 00:00:00 2001 From: jania902 Date: Fri, 11 Jan 2008 09:24:07 +0000 Subject: [PATCH] git-svn-id: http://jsspec.googlecode.com/svn/trunk@96 92187c49-0d35-0410-b305-2f54cc65526e --- exp/build.xml | 2 +- exp/src/JSSpec2.js | 74 +++++++++++++++++++++++++++++++++++++---- exp/src/JSSpec2_spec.js | 47 ++++++++++++++++++++++++++ exp/src/RhinoRunner.js | 1 - 4 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 exp/src/JSSpec2_spec.js delete mode 100644 exp/src/RhinoRunner.js diff --git a/exp/build.xml b/exp/build.xml index 2e61397..15b4a8e 100644 --- a/exp/build.xml +++ b/exp/build.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/exp/src/JSSpec2.js b/exp/src/JSSpec2.js index 28f49de..48dfcf3 100644 --- a/exp/src/JSSpec2.js +++ b/exp/src/JSSpec2.js @@ -24,14 +24,74 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -var jsspec = {}; -jsspec.isBrowser = !!this[alert]; +var JSSpec2 = { + given: function(givens) { + // make new current scenario + this.current_scenario = new JSSpec2.Scenario(); + runner.addScenario(this.current_scenario); + + // set givens into current scenario + this.current_scenario.givens = givens; + }, + + when: function(events) { + // set events into current scenario + this.current_scenario.events = events; + }, + + then: function(outcomes) { + // set expected outcomes into current scenario + this.current_scenario.outcomes = outcomes; + }, + + value_of: function(value) { + return new JSSpec2.Expectation(value); + }, + + run: function() { + this.current_scenario.run(); + } +}; -jsspec.echo = function(message) { +JSSpec2.RhinoRunner = function() { + this.scenarios = []; + + this.addScenario = function(scenario) { + this.scenarios.push(scenario); + } - if(jsspec.isBrowser) { - alert(message); - } else { + this.run = function() { + for(var i = 0; i < this.scenarios.length; i++) { + this.scenarios[i].run(); + } + } +} + +JSSpec2.Scenario = function() { + this.run = function() { + this.context = {}; + + for(var key in this.givens) { + this.givens[key].apply(this.context); + } + + for(var key in this.events) { + this.events[key].apply(this.context); + } + for(var key in this.outcomes) { + this.outcomes[key].apply(this.context); + } } -} \ No newline at end of file +} + +JSSpec2.Expectation = function(actual_value) { + this.should_be = function(expected_value) { + if(expected_value != actual_value) { + print("[" + actual_value + "] should be [" + expected_value + "]"); + } + } +} + +// Main +var runner = new JSSpec2.RhinoRunner(); \ No newline at end of file diff --git a/exp/src/JSSpec2_spec.js b/exp/src/JSSpec2_spec.js new file mode 100644 index 0000000..67a80c4 --- /dev/null +++ b/exp/src/JSSpec2_spec.js @@ -0,0 +1,47 @@ +load("/prj/jsspec/exp/src/JSSpec2.js") + +with(JSSpec2) { + __log__ = []; + + given({"new scenario": function() { + this.scenario = new JSSpec2.Scenario("Scenario 1") + this.scenario.givens = { + "Given 1": function() { + __log__.push("given") + this.a = 1 + } + } + this.scenario.events = { + "When 1": function() { + __log__.push("when") + this.b = 2 + } + } + this.scenario.outcomes = { + "Then 1": function() { + __log__.push("then") + this.c = 3 + } + } + } + }) + + when({ + "the scenario runned": function() { + this.scenario.run() + } + }) + + then({ + "'given', 'when' and 'then' should be executed in exact order": function() { + value_of(__log__.join(",")).should_be("given,when,then") + }, + "context should be preserved": function() { + value_of(this.scenario.context.a).should_be(1); + value_of(this.scenario.context.b).should_be(2); + value_of(this.scenario.context.c).should_be(3); + } + }) +} + +runner.run() \ No newline at end of file diff --git a/exp/src/RhinoRunner.js b/exp/src/RhinoRunner.js deleted file mode 100644 index dfe80e8..0000000 --- a/exp/src/RhinoRunner.js +++ /dev/null @@ -1 +0,0 @@ -print("Hello World"); \ No newline at end of file