Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix missing Compose.stop and add more aspect unit tests

  • Loading branch information...
commit 6232a86f7f81ed35042db99272f5068d92d475fc 1 parent d3fd5cc
@kriszyp kriszyp authored
Showing with 71 additions and 24 deletions.
  1. +4 −1 lib/compose.js
  2. +67 −23 test/compose.js
View
5 lib/compose.js
@@ -144,9 +144,12 @@ define([], function(){
Compose.before = aspect(function(target, base, advice){
return function(){
var results = advice.apply(this, arguments);
- return base.apply(this, results || arguments);
+ if(results !== stop){
+ return base.apply(this, results || arguments);
+ }
};
});
+ var stop = Compose.stop = {};
var undefined;
Compose.after = aspect(function(target, base, advice){
return function(){
View
90 test/compose.js
@@ -33,7 +33,7 @@ exports.testComposeWithConstruct = function() {
widget.render();
assert.equal(node.innerHTML, "<div>hi</div>");
};
-exports.testInheritance= function() {
+exports.testInheritance = function() {
MessageWidget = Compose(Widget, {
message: "Hello, World",
render: function(){
@@ -45,6 +45,18 @@ exports.testInheritance= function() {
widget.render();
assert.equal(node.innerHTML, "<div>Hello, World</div>");
};
+exports.testInheritanceViaExtend = function() {
+ MessageWidget = Widget.extend({
+ message: "Hello, World",
+ render: function(){
+ this.node.innerHTML = "<div>" + this.message + "</div>";
+ }
+ });
+ var node = {};
+ var widget = new MessageWidget(node);
+ widget.render();
+ assert.equal(node.innerHTML, "<div>Hello, World</div>");
+};
exports.testInheritance2 = function() {
SpanishWidget = Compose(MessageWidget, {
message: "Hola",
@@ -253,48 +265,80 @@ exports.testExtendError = function(){
assert.equal(error instanceof Error, true);
}
exports.testDiamond = function(){
- var baseCallCount = 0, sub1CallCount = 0, sub2CallCount = 0;
- function Base(){
+ var baseCallCount = 0, sub1CallCount = 0, sub2CallCount = 0, fooCallCount = 0, fooSub1Count = 0, fooSub2Count = 0;
+ var Base = Compose(function(){
baseCallCount++;
- }
- var Sub1 = Compose(Base, function(){sub1CallCount++;});
- var Sub2 = Compose(Base, function(){sub2CallCount++;});
+ }, {
+ foo: function(){
+ fooCallCount++;
+ }
+ });
+ var Sub1 = Compose(Base, function(){sub1CallCount++;}, {
+ foo: Compose.after(function(){
+ fooSub1Count++;
+ })
+ });
+ var Sub2 = Compose(Base, function(){sub2CallCount++;}, {
+ foo: Compose.after(function(){
+ fooSub2Count++;
+ })
+ });
var Combined = Sub1.extend(Sub2);
- new Combined;
+ var combined = new Combined;
assert.equal(baseCallCount, 1);
assert.equal(sub1CallCount, 1);
assert.equal(sub2CallCount, 1);
+ combined.foo();
+ assert.equal(fooCallCount, 1);
+ //assert.equal(fooSub1Count, 1); // TODO: Should this be 1?
+ assert.equal(fooSub2Count, 1);
}
-/*exports.testAdvice = function() {
+exports.testAdvice = function() {
var order = [];
var obj = {
foo: function(value){
order.push(value);
return 6;
},
- on: Compose.after
};
- Compose.around(obj, "foo", function(base){
- return function(){
- order.push(2);
- try{
- return base.apply(this, arguments);
- }finally{
- order.push(4);
+ Advised = Compose(obj, {
+ "foo": Compose.around(function(base){
+ return function(){
+ order.push(2);
+ try{
+ return base.apply(this, arguments);
+ }finally{
+ order.push(4);
+ }
}
- }
+ })
});
- obj.on("foo", function(){
- order.push(5);
+ Advised = Compose(Advised, {
+ "foo": Compose.after(function(){
+ order.push(5);
+ })
});
- Compose.before(obj, "foo", function(value){
- order.push(value);
- return [3];
+ Advised = Compose(Advised, {
+ "foo": Compose.before(function(value){
+ order.push(value);
+ return [3];
+ })
});
+ obj = new Advised();
order.push(obj.foo(1));
assert.deepEqual(order, [1,2,3,4,5,6]);
-};*/
+ order = [];
+ Advised = Compose(Advised, {
+ "foo": Compose.before(function(value){
+ order.push(0);
+ return Compose.stop;
+ })
+ });
+ obj = new Advised();
+ obj.foo(1);
+ assert.deepEqual(order, [0]);
+};

0 comments on commit 6232a86

Please sign in to comment.
Something went wrong with that request. Please try again.