Skip to content

Commit

Permalink
Added @after behaviour and test
Browse files Browse the repository at this point in the history
  • Loading branch information
yukatan committed Mar 15, 2015
1 parent 44d1fe4 commit 165e030
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/commangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
var matcherString = interceptorExtractor.exec(result[2])[1];
var matcher = new RegExp("^%" + matcherString + "%\{(.*)\}$","mg");
var aspectOrder = order || (order = 0);
if(!/(\bBefore\b|\bAfterExecution\b|\bAfter\b|\bAfterThrowing\b)/.test(poincut))
if(!/(\bBefore\b|\bAfter\b|\bAfterThrowing\b)/.test(poincut))
throw new Error('aspect descriptor ' + aspectDescriptor + ' contains errors');
eventAspects.push({poincut:poincut,
matcher:matcher,
Expand Down Expand Up @@ -285,7 +285,10 @@
})
.then(function(){
result = self.exeOnResult(self.contextData.lastResult);
self.processResults(result,descriptor.command.config);
return self.processResults(result,descriptor.command.config);
})
.then(function(){
return self.intercept('After',descriptor.command.interceptors);
},function(error) {
var deferred = $q.defer();
if(self.canceled){
Expand Down Expand Up @@ -375,7 +378,7 @@
return defer.promise;
}
var promise = $q.when(result).then(function(data) {

self.contextData.lastResult = data;
if (config && config.resultKey) {
self.contextData[config.resultKey] = data;
Expand Down
39 changes: 12 additions & 27 deletions test/command-aspects/after-interception-testing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

describe("Aspect @AfterExecution execution testing", function() {
describe("Aspect @After execution testing", function() {

var provider;
var scope;
Expand All @@ -11,49 +11,34 @@ describe("Aspect @AfterExecution execution testing", function() {

commangular.reset();

commangular.aspect('@AfterExecution(/com\.test1.*/)', function(){
commangular.aspect('@After(/com\.test1.*/)', function(){

return {

execute : function () {

execute : function (lastResult) {

expect(lastResult).toBeDefined();
expect(lastResult).toBe("monkey2");
interceptorExecutedAfter = true;
}
}

});

commangular.aspect('@AfterExecution(/com\.test2.*/)', function(lastResult){

return {

execute : function() {

expect(lastResult).toBeDefined();
expect(lastResult).toBe('monkey');
}
}

});

commangular.create('com.test1.Command1',function(){

return {

execute : function() {

commandExecuted = true;
}
};
});

commangular.create('com.test2.Command2',function(){
commandExecuted = true;
return "monkey";

return {
},
onResult: function(lastResult){

execute : function() {

return "monkey";
expect(lastResult).toBe("monkey");
return "monkey2";
}
};
});
Expand Down
81 changes: 81 additions & 0 deletions test/command-aspects/afterexecution-interception-testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"use strict";

describe("Aspect @AfterExecution execution testing", function() {

var provider;
var scope;
var interceptorExecutedAfter = false;
var commandExecuted = false;

beforeEach(function() {

commangular.reset();

commangular.aspect('@AfterExecution(/com\.test1.*/)', function(){

return {

execute : function () {

interceptorExecutedAfter = true;
}
}

});

commangular.aspect('@AfterExecution(/com\.test2.*/)', function(lastResult){

return {

execute : function() {

expect(lastResult).toBeDefined();
expect(lastResult).toBe('monkey');
}
}

});

commangular.create('com.test1.Command1',function(){

return {

execute : function() {

commandExecuted = true;
}
};
});

commangular.create('com.test2.Command2',function(){

return {

execute : function() {

return "monkey";
}
};
});

});

beforeEach(function() {

module('commangular', function($commangularProvider) {
provider = $commangularProvider;
});
inject();
});

it("should execute the interceptor after the command", function() {

var complete = false;
provider.mapTo('AfterTestEvent').asSequence().add('com.test1.Command1');
dispatch({event:'AfterTestEvent'},function(){

expect(interceptorExecutedAfter).toBe(true);
expect(commandExecuted).toBe(true);
});
});
});
30 changes: 30 additions & 0 deletions test/command-aspects/aspect-execution-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ describe("Aspect execution testing", function() {
var interceptorExecutedBefore = false;
var commandExecutedAfter = false;
var afterInterceptorExecutedAfterCommand = false;
var afterInterceptorExecutedAfterOnResult = false;
var afterThrowingInterceptorExecutedAfterCommand = false;
var onResultMethodExecuted = false;

beforeEach(function() {

Expand All @@ -34,6 +36,17 @@ describe("Aspect execution testing", function() {
}
}
});
commangular.aspect('@After(/Command[1-9]/)', function(){

return {

execute : function() {

if(commandExecutedAfter && afterInterceptorExecutedAfterCommand)
afterInterceptorExecutedAfterOnResult = true;
}
}
});

commangular.aspect('@AfterThrowing(/Command[1-9]/)', function(){

Expand Down Expand Up @@ -81,6 +94,10 @@ describe("Aspect execution testing", function() {
commandExecutedAfter = true;
}
return "return From command1";
},
onResult: function(){

onResultMethodExecuted = true;
}
};
});
Expand Down Expand Up @@ -132,6 +149,17 @@ describe("Aspect execution testing", function() {
});
});

it("should execute the interceptor afterexecution the command", function() {

provider.mapTo('AspectTestEvent').asSequence().add('Command1');
dispatch({event:'AspectTestEvent'},function() {

expect(commandExecutedAfter).toBe(true);
expect(interceptorExecutedBefore).toBe(true);
expect(afterInterceptorExecutedAfterCommand).toBe(true);
});
});

it("should execute the interceptor after the command", function() {

provider.mapTo('AspectTestEvent').asSequence().add('Command1');
Expand All @@ -140,6 +168,8 @@ describe("Aspect execution testing", function() {
expect(commandExecutedAfter).toBe(true);
expect(interceptorExecutedBefore).toBe(true);
expect(afterInterceptorExecutedAfterCommand).toBe(true);
expect(onResultMethodExecuted).toBe(true);
expect(afterInterceptorExecutedAfterOnResult).toBe(true);
});
});

Expand Down

0 comments on commit 165e030

Please sign in to comment.