main
1var Fake = (function(){
2 var fake = function(){
3 _stubs = [];
4 this.stub = function(method){
5 if(_stubs[method] === undefined) {
6 _stubs[method] = new Stub(this, method);
7 }
8 return _stubs[method];
9 };
10 };
11 return function(){
12 return new fake();
13 };
14})();
15
16var Stub = (function(){
17 var stub = function(target, method){
18 this.arguments = [];
19 this.with = function(){
20 var args = new Arguments(this, target, method, arguments);
21 this.arguments.push(args);
22 return args;
23 };
24 this.andReturn = function(return_value){
25 target[method] = function(){
26 return return_value;
27 };
28 };
29 this.find_match_for = function(args){
30 for (var i = 0; i < this.arguments.length; i += 1) {
31 var matcher = this.arguments[i];
32 if(matcher.matches(args)){
33 return matcher;
34 }
35 }
36 throw "Matcher not found.";
37 };
38 };
39 return function(target, method){
40 return new stub(target, method);
41 };
42})();
43var Arguments = (function(){
44 var Arguments = function(stub, target, method, expected_args){
45 this.andReturn = function(return_value){
46 this.return_value = return_value;
47
48 target[method] = function(){
49 return stub.find_match_for(arguments).return_value;
50 };
51 };
52 this.matches = function(args){
53 var result = expected_args === args;
54 for (var i = 0; i < expected_args.length; i += 1) {
55 if(args[i] !== expected_args[i]){
56 return false;
57 }
58 }
59 return true;
60 };
61 };
62 return function(stub, target, method, expected_args){
63 return new Arguments(stub, target, method, expected_args);
64 };
65})();