Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improved when() cases: can specify either a function or an explicit list

  • Loading branch information...
commit 579c7219eb5b5fee77ef27501a1cfacff26db2f5 1 parent 501ee73
Romain Prieto authored
View
50 fakejs.js
@@ -31,29 +31,51 @@ var Fake = function() {
function when() {
var me = {
name: 'when',
- returnDefault: undefined,
- returnValues: {},
- run: function(args) {
- if (me.returnValues.hasOwnProperty(args)) {
- return me.returnValues[args];
- } else {
- return me.returnDefault;
+ conditions: [],
+ values: [],
+ otherwise: undefined,
+ run: function() {
+ for (var i=0; i<me.conditions.length; ++i) {
+ if (me.conditions[i].apply(null, arguments)) {
+ return me.values[i];
+ }
}
+ return me.otherwise;
}
};
return me;
}
+ function compareArrays(a, b) {
+ if (a.length != b.length) { return false; }
+ a = a.sort(),
+ b = b.sort();
+ for (var i = 0; a[i]; i++) {
+ if (a[i] !== b[i]) {
+ return false;
+ }
+ }
+ return true;
+ };
+
+ function matchesExplicitArgs() {
+ var expectedArguments = Array.prototype.slice.call(arguments);
+ return function() {
+ return compareArrays(expectedArguments, Array.prototype.slice.call(arguments));
+ }
+ }
+
function method() {
//
// Definition
//
- var f = function(args) {
+ var f = function() {
+ var args = Array.prototype.slice.call(arguments);
f.called = true;
- f.calls.push(Array.prototype.slice.call(arguments));
- return f.type.run(args);
+ f.calls.push(args);
+ return f.type.run.apply(null, args);
};
//
@@ -83,9 +105,13 @@ var Fake = function() {
if (f.type.name !== 'when') {
f.type = when();
};
+ if (typeof condition != 'function') {
+ condition = matchesExplicitArgs.apply(null, Array.prototype.slice.call(arguments));
+ }
return {
returns: function(value) {
- f.type.returnValues[condition] = value;
+ f.type.conditions.push(condition);
+ f.type.values.push(value);
return f;
}
};
@@ -95,7 +121,7 @@ var Fake = function() {
if (f.type.name !== 'when') {
throw 'otherwise() only valid after when()';
}
- f.type.returnDefault = value;
+ f.type.otherwise = value;
return f;
};
View
4 tests/integration.js
@@ -6,7 +6,7 @@ describe('integration', function() {
fake.three.returns(3);
fake.four = Fake.method().returns(4);
- fake.isBob = Fake.method().when('Bob').returns(true);
+ fake.isBob = Fake.method().when('Bob', 'Kelso').returns(true);
expect(fake.dummy.called).toEqual(false);
expect(fake.dummy()).toBe(undefined);
@@ -14,7 +14,7 @@ describe('integration', function() {
expect(fake.four(1, 2, 3)).toEqual(4);
expect(fake.four.called).toEqual(true);
expect(fake.four.calls[0]).toEqual([1, 2, 3]);
- expect(fake.isBob('Bob')).toEqual(true);
+ expect(fake.isBob('Bob', 'Kelso')).toEqual(true);
});
});
View
33 tests/method-return-values.js
@@ -17,6 +17,11 @@ describe('method return values', function() {
expect(m()).toEqual(3);
expect(m(1)).toEqual(3);
});
+
+ it('can return a complex object', function() {
+ var m = Fake.method().returns({ foo: 'bar', value: 3, nested: { hello: 'world' } });
+ expect(m()).toEqual({ foo: 'bar', value: 3, nested: { hello: 'world' } });
+ });
});
@@ -31,33 +36,5 @@ describe('method return values', function() {
});
});
-
- describe('cases', function() {
-
- it('returns a value depending on its argument', function() {
- var m = Fake.method().when(1).returns(2);
- expect(m(1)).toEqual(2);
- });
-
- it('returns undefined for undefined cases', function() {
- var m = Fake.method().when(1).returns(2);
- expect(m(3)).toBe(undefined);
- });
-
- it('can chain conditions and return values', function() {
- var m = Fake.method().when(1).returns(2).when(3).returns(4);
- expect(m(1)).toEqual(2);
- expect(m(3)).toEqual(4);
- });
-
- it('can specify a default value when the arguments dont match any case', function() {
- var m = Fake.method().when('hi').returns(true).otherwise(false);
- expect(m('hi')).toEqual(true);
- expect(m()).toEqual(false);
- expect(m('hello')).toEqual(false);
- expect(m(1, 2, 3)).toEqual(false);
- });
-
- });
});
View
89 tests/method-when.js
@@ -0,0 +1,89 @@
+
+describe('method when', function() {
+
+ describe('undefined cases', function() {
+
+ it('returns undefined for undefined cases', function() {
+ var m = Fake.method().when(1).returns(true);
+ expect(m(2)).toBe(undefined);
+ });
+
+ it('can specify a default value when the arguments dont match any case', function() {
+ var m = Fake.method().when(1).returns(true).otherwise(false);
+ expect(m()).toEqual(false);
+ expect(m('hello')).toEqual(false);
+ expect(m(1, 2, 3)).toEqual(false);
+ });
+
+ });
+
+ describe('argument matchers', function() {
+
+ describe('explicit list', function() {
+
+ it('can specify a unique argument to be matched explicitely', function() {
+ var m = Fake.method().when(1).returns(true);
+ expect(m()).toBe(undefined);
+ expect(m(1)).toEqual(true);
+ expect(m(1, 2)).toBe(undefined);
+ expect(m('hi')).toBe(undefined);
+ });
+
+ it('can specify all arguments to be matched explicitely', function() {
+ var m = Fake.method().when(1, 2).returns(true);
+ expect(m()).toBe(undefined);
+ expect(m(1)).toBe(undefined);
+ expect(m(1, 2)).toEqual(true);
+ expect(m(1, 2, 3)).toBe(undefined);
+ });
+
+ });
+
+ describe('functions', function() {
+
+ it('can use a function to determine if the arguments match', function() {
+ var m = Fake.method()
+ .when(function(arg) { return arg == 1; })
+ .returns(true);
+ expect(m()).toBe(undefined);
+ expect(m(1)).toEqual(true);
+ expect(m(1, 2)).toEqual(true);
+ expect(m('hi')).toBe(undefined);
+ });
+
+ it('passes all the arguments to the when() condition function', function() {
+ var m = Fake.method()
+ .when(function(arg1, arg2) { return arg1 == 1 && arg2 == 2; })
+ .returns(true);
+ expect(m(1)).toEqual(undefined);
+ expect(m(1, 2)).toEqual(true);
+ expect(m(5, 6)).toEqual(undefined);
+ });
+
+ it('can check properties of an argument object (useful for jQuery)', function() {
+ var m = Fake.method()
+ .when(function(arg) { return arg.selector == '#test'; })
+ .returns(true);
+ var $ = function(s) { return { selector: s } };
+ expect(m($('#test'))).toEqual(true);
+ });
+
+ });
+
+ });
+
+ describe('several return values', function() {
+
+ it('can chain conditions and return values', function() {
+ var m = Fake.method()
+ .when(1).returns(10)
+ .when(2).returns(20)
+ .when(3).returns(30);
+ expect(m(1)).toEqual(10);
+ expect(m(2)).toEqual(20);
+ expect(m(3)).toEqual(30);
+ });
+
+ });
+
+});
View
29 tests/run-all-tests.html
@@ -2,22 +2,23 @@
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
- <title>Jasmine Test Runner</title>
- <link rel="stylesheet" type="text/css" href="3rdparty/jasmine-1.0.1/jasmine.css" />
+ <title>Jasmine Test Runner</title>
+ <link rel="stylesheet" type="text/css" href="3rdparty/jasmine-1.0.1/jasmine.css" />
- <!-- Jasmine -->
- <script type="text/javascript" src="3rdparty/jasmine-1.0.1/jasmine.js"></script>
- <script type="text/javascript" src="3rdparty/jasmine-1.0.1/jasmine-html.js"></script>
+ <!-- Jasmine -->
+ <script type="text/javascript" src="3rdparty/jasmine-1.0.1/jasmine.js"></script>
+ <script type="text/javascript" src="3rdparty/jasmine-1.0.1/jasmine-html.js"></script>
- <!-- Plugin code -->
- <script type="text/javascript" src="../fakejs.js"></script>
-
- <!-- Tests -->
- <script type="text/javascript" src="method-return-values.js"></script>
- <script type="text/javascript" src="several-methods.js"></script>
- <script type="text/javascript" src="recording.js"></script>
- <script type="text/javascript" src="basic-objects.js"></script>
- <script type="text/javascript" src="integration.js"></script>
+ <!-- Plugin code -->
+ <script type="text/javascript" src="../fakejs.js"></script>
+
+ <!-- Tests -->
+ <script type="text/javascript" src="method-return-values.js"></script>
+ <script type="text/javascript" src="several-methods.js"></script>
+ <script type="text/javascript" src="method-when.js"></script>
+ <script type="text/javascript" src="recording.js"></script>
+ <script type="text/javascript" src="basic-objects.js"></script>
+ <script type="text/javascript" src="integration.js"></script>
</head>
<body>
Please sign in to comment.
Something went wrong with that request. Please try again.