diff options
| author | mo k <mo@mokhan.ca> | 2012-05-21 12:04:07 -0600 |
|---|---|---|
| committer | mo k <mo@mokhan.ca> | 2012-05-21 12:04:07 -0600 |
| commit | 82fce20cd0f18829c3df13a7d62cacd75c09b9bf (patch) | |
| tree | 2bca2faa678d2d6dbf61eb17629c802b0f4e0b2f | |
| parent | 7e15845e5089850d2882850d288bf6dd298e59e7 (diff) | |
match on multiple arguments
| -rw-r--r-- | spec/javascripts/FakeSpec.js | 80 |
1 files changed, 70 insertions, 10 deletions
diff --git a/spec/javascripts/FakeSpec.js b/spec/javascripts/FakeSpec.js index 0bf6c1b..37ec967 100644 --- a/spec/javascripts/FakeSpec.js +++ b/spec/javascripts/FakeSpec.js @@ -6,32 +6,92 @@ describe ("Fake", function() { describe ("when stubbing out return values", function() { describe ("when there is no args", function() { it ("should return the correct value", function() { - expect(result).toEqual("hello mo"); + expect(result).toEqual("hello"); }); beforeEach (function() { - sut.stub('greet').andReturn('hello mo'); + sut.stub('greet').andReturn('hello'); result = sut.greet(); }); var result; }); + describe ("when there are arguments to match", function() { + describe ("when there is a single input argument", function() { + it ("should return the value the corresponds to the input arguments", function() { + expect(sut.greet('mo')).toEqual('hello mo'); + }); + it ("should return the correct value that corresponds to other args", function() { + //expect(sut.greet('jo')).toEqual('hello jo'); + }); + beforeEach (function() { + sut.stub('greet').with('mo').andReturn('hello mo'); + sut.stub('greet').with('jo').andReturn('hello jo'); + }); + }); + }); }); }); var Fake = (function(){ var fake = function(){ - methods = []; + _stubs = []; this.stub = function(method){ - methods.push(method); - return this; + if(_stubs[method] === undefined) { + _stubs[method] = new Stub(this, method); + } + return _stubs[method]; + }; + }; + return function(){ + return new fake(); + }; +})(); + +var Stub = (function(){ + var stub = function(target, method){ + this.arguments = []; + this.with = function(){ + var args = new Arguments(this, target, method, arguments); + this.arguments.push(args); + return args; }; this.andReturn = function(return_value){ - var method = methods[methods.length - 1]; - console.log(method); - this[method] = function(){ + target[method] = function(){ return return_value; }; }; + this.find_match_for = function(args){ + for (var i = 0; i < this.arguments.length; i += 1) { + var matcher = this.arguments[i]; + if(matcher.matches(args)){ + return matcher; + } + } + throw "Matcher not found."; + }; + }; + return function(target, method){ + return new stub(target, method); + }; +})(); +var Arguments = (function(){ + var Arguments = function(stub, target, method, expected_args){ + this.andReturn = function(return_value){ + this.return_value = return_value; + + target[method] = function(){ + return stub.find_match_for(arguments).return_value; + }; + }; + this.matches = function(args){ + var result = expected_args === args; + for (var i = 0; i < expected_args.length; i += 1) { + if(args[i] !== expected_args[i]){ + return false; + } + } + return true; + }; }; - return function(){ - return new fake(); + return function(stub, target, method, expected_args){ + return new Arguments(stub, target, method, expected_args); }; })(); |
