forked from osteele/jsspec
/
JSSpec2.js
97 lines (83 loc) · 2.5 KB
/
JSSpec2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* JSSpec2
*
* Copyright 2007 Alan Kang
* - mailto:jania902@gmail.com
* - http://jania.pe.kr
*
* http://jania.pe.kr/aw/moin.cgi/JSSpec2
*
* Dependencies:
* - diff_match_patch.js ( http://code.google.com/p/google-diff-match-patch )
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* 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 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();
}
};
JSSpec2.RhinoRunner = function() {
this.scenarios = [];
this.addScenario = function(scenario) {
this.scenarios.push(scenario);
}
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();