Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
git-svn-id: http://jsspec.googlecode.com/svn/trunk@96 92187c49-0d35-0…
…410-b305-2f54cc65526e
  • Loading branch information
jania902 committed Jan 11, 2008
1 parent 1e1f8c9 commit 420c8f0
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
2 changes: 1 addition & 1 deletion exp/build.xml
Expand Up @@ -3,7 +3,7 @@

<target name="test">
<java jar="lib/js.jar" fork="true">
<arg line="-f src/RhinoRunner.js" />
<arg line="-f src/JSSpec2_spec.js" />
</java>
</target>
</project>
74 changes: 67 additions & 7 deletions exp/src/JSSpec2.js
Expand Up @@ -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);
}
}
}
}

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();
47 changes: 47 additions & 0 deletions 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()
1 change: 0 additions & 1 deletion exp/src/RhinoRunner.js

This file was deleted.

0 comments on commit 420c8f0

Please sign in to comment.