Skip to content

Commit

Permalink
fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
k1r0s committed Dec 15, 2016
1 parent 2fef29c commit 04b82f5
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 76 deletions.
36 changes: 24 additions & 12 deletions src/annotations.js
Expand Up @@ -24,11 +24,19 @@ module.exports = annotations = {
}
},
Store: function(opts) {
this.isBefore = true;
var befores = [];
var afters = [];
this.before = function(fn) {
befores.push(fn);
};
this.position = function(fn) {
if(this.isBefore){
befores.push(fn);
}else{
afters.push(fn);
}
},
this.after = function(fn) {
afters.push(fn);
};
Expand All @@ -48,8 +56,9 @@ module.exports = annotations = {
};
},
fireMethodAnnotations: function(annotations, storeInstance, locals) {
for (var i = 0; i < annotations.length; i++) {

for (var i = 0; i < annotations.length; i++) {
if(typeof annotations[i] === "function"){ storeInstance.isBefore = false; continue; }
var preparedAnnotation = annotations[i].split(":");
var annotationFn = this.getAnnotation(preparedAnnotation[0]);
var annotationArguments = preparedAnnotation[1];
Expand All @@ -64,22 +73,27 @@ module.exports = annotations = {
}
},
getMethodAnnotations: function(array) {
return array.filter(function(e, index, arr) {
return index !== arr.length - 1;
return array.filter(function(item) {
return typeof item !== "function";
});
},
getAnnotatedMethod: function(array){
return array.find(function(item) {
return typeof item === "function";
});
},
isValidStructure: function(array){
return array instanceof Array && array.some(function(item){ return typeof item === "function"; });
},
isValidAnnotationArray: function(array) {
return this.getMethodAnnotations(array)
.map(function(item) {
return item.split(":").shift();
})
.map(function(item) { return item.split(":").shift(); })
.every(this.getAnnotation, this);
},
compile: function(superClass, propertyName, propertyValue) {
if (!(
propertyValue &&
typeof propertyValue.length === "number" &&
typeof propertyValue[propertyValue.length - 1] === "function" &&
this.isValidStructure(propertyValue) &&
this.isValidAnnotationArray(propertyValue)
)) {
return propertyValue;
Expand All @@ -92,7 +106,7 @@ module.exports = annotations = {
var opts = {
scope: this,
parentScope: superClass.prototype,
method: propertyValue[propertyValue.length - 1],
method: selfAnnotations.getAnnotatedMethod(propertyValue),
methodName: propertyName,
args: Array.prototype.slice.call(arguments),
result: undefined,
Expand All @@ -101,9 +115,7 @@ module.exports = annotations = {

var store = new selfAnnotations.Store(opts);

var methodAnnotations = selfAnnotations.getMethodAnnotations(propertyValue);

selfAnnotations.fireMethodAnnotations(methodAnnotations, store, selfAnnotations.locals);
selfAnnotations.fireMethodAnnotations(propertyValue, store, selfAnnotations.locals);

store.next();

Expand Down
43 changes: 43 additions & 0 deletions test/functional.js
Expand Up @@ -200,3 +200,46 @@ describe("extending JS native types", function() {
assert(!listInstance.has(454));
});
});

describe("annotations could be placed anywhere in the array definition", function(){
var Service;
before(function(){
annotations.locals.aux = [];
annotations.add(function $log() {
this.position(function(opts, next) {
aux.push("logged");
next();
});
});

Service = Class.static({
operation1: ["$log", function(){
annotations.locals.aux.push("operation1");
}],
operation2: [function(){
annotations.locals.aux.push("operation2");
}, "$log"]
});
});

it("should check if annotations are executed given own position", function(){

Service.operation1();
Service.operation2();

assert.strictEqual(annotations.locals.aux.join(","), "logged,operation1,operation2,logged");
});
});

describe.skip("multiple inherits", function(){
var Human, Ape, Mamal;
before(function(){
Mamal = Class({
isWarmblooded: true
})

Ape = Class({

})
});
});
134 changes: 70 additions & 64 deletions test/unit.js
Expand Up @@ -3,81 +3,87 @@ var main = require("../index")
var extend = main.extend
var annotations = main.annotations

describe("unit test", function(){
describe("annotations::compile()", function(){
describe("annotations::compile()", function(){

it("propertyValue are falsy values, so we must receive it as normal", function(){
assert.strictEqual(null, annotations.compile(undefined, undefined, null))
assert.strictEqual(undefined, annotations.compile(undefined, undefined, undefined))
assert.strictEqual(0, annotations.compile(undefined, undefined, 0))
assert.strictEqual(false, annotations.compile(undefined, undefined, false))
})
it("propertyValue are falsy values, so we must receive it as normal", function(){
assert.strictEqual(null, annotations.compile(undefined, undefined, null))
assert.strictEqual(undefined, annotations.compile(undefined, undefined, undefined))
assert.strictEqual(0, annotations.compile(undefined, undefined, 0))
assert.strictEqual(false, annotations.compile(undefined, undefined, false))
})

it("propertyValue are numbers, should be retrieved without modifications", function(){
assert.strictEqual(1, annotations.compile(undefined, undefined, 1))
assert.strictEqual(-2, annotations.compile(undefined, undefined, -2))
assert.strictEqual(313, annotations.compile(undefined, undefined, 313))
assert.strictEqual(437, annotations.compile(undefined, undefined, 437))
assert.strictEqual(437.333, annotations.compile(undefined, undefined, 437.333))
})
it("propertyValue are numbers, should be retrieved without modifications", function(){
assert.strictEqual(1, annotations.compile(undefined, undefined, 1))
assert.strictEqual(-2, annotations.compile(undefined, undefined, -2))
assert.strictEqual(313, annotations.compile(undefined, undefined, 313))
assert.strictEqual(437, annotations.compile(undefined, undefined, 437))
assert.strictEqual(437.333, annotations.compile(undefined, undefined, 437.333))
})

it("propertyValue are complex types as strings, objects or functions", function(){
assert(typeof annotations.compile(undefined, undefined, function(){}) === "function")
assert.deepEqual(["prop1", "prop2", "prop3"], annotations.compile(undefined, undefined, ["prop1", "prop2", "prop3"]))
assert.deepEqual({}, annotations.compile(undefined, undefined, {}))
assert.strictEqual("something", annotations.compile(undefined, undefined, "something"))
})
it("propertyValue are complex types as strings, objects or functions", function(){
assert(typeof annotations.compile(undefined, undefined, function(){}) === "function")
assert.deepEqual(["prop1", "prop2", "prop3"], annotations.compile(undefined, undefined, ["prop1", "prop2", "prop3"]))
assert.deepEqual({}, annotations.compile(undefined, undefined, {}))
assert.strictEqual("something", annotations.compile(undefined, undefined, "something"))
})

it("propertyValue is an array that contains a function in the last slot, it should return the annotatedFunction", function(){
assert(typeof annotations.compile(undefined, undefined, ["$override", function(){}]) === "function")
})
it("propertyValue is an array that contains a function in the last slot, it should return the annotatedFunction", function(){
assert(typeof annotations.compile(undefined, undefined, ["$override", function(){}]) === "function")
})

it("if contains any annotation that was not added yet to the annotations pool, it should return the array instead", function(){
assert(typeof annotations.compile(undefined, undefined, ["$annotationThatNotExists", function(){}]) !== "function")
})
it("propertyValue is an array that contains a function in the last slot, it should return the annotatedFunction", function(){
assert(typeof annotations.compile(undefined, undefined, [function(){}, "$override"]) === "function")
})

describe("annotations::add, ::names, ::getAnnotation", function(){
it("if contains any annotation that was not added yet to the annotations pool, it should return the array instead", function(){
assert(typeof annotations.compile(undefined, undefined, ["$annotationThatNotExists", function(){}]) !== "function")
})
})

before(function(){
annotations.add(function $prop1(){})
annotations.add(function $prop2(){})
annotations.add(function $prop3(){})
annotations.add(function $prop4(){})
})
describe("annotations::add, ::names, ::getAnnotation", function(){

it("annotations::names() should return all the annotation names", function(){
var annotationsArr = annotations.names()
assert(annotationsArr.indexOf("$prop2") > -1)
assert(annotationsArr.indexOf("$prop1") > -1)
assert(annotationsArr.indexOf("$prop3") > -1)
assert(annotationsArr.indexOf("$prop4") > -1)
})
it("annotations::getAnnotation() should return the annotation function definition", function(){
assert.strictEqual("$prop3", annotations.getAnnotation("$prop3").name)
})
before(function(){
annotations.add(function $prop1(){})
annotations.add(function $prop2(){})
annotations.add(function $prop3(){})
annotations.add(function $prop4(){})
})

describe("annotations::getMethodAnnotations()", function(){
it("should return the complete list of defined annotations", function(){
assert.deepEqual(["$httpGet: 'Person'", "$json"],
annotations.getMethodAnnotations(["$httpGet: 'Person'", "$json", function(){ }]))
assert.deepEqual(["$override"],
annotations.getMethodAnnotations(["$override", function(){ }]))
})
it("annotations::names() should return all the annotation names", function(){
var annotationsArr = annotations.names()
assert(annotationsArr.indexOf("$prop2") > -1)
assert(annotationsArr.indexOf("$prop1") > -1)
assert(annotationsArr.indexOf("$prop3") > -1)
assert(annotationsArr.indexOf("$prop4") > -1)
})
it("annotations::getAnnotation() should return the annotation function definition", function(){
assert.strictEqual("$prop3", annotations.getAnnotation("$prop3").name)
})
})

describe("annotations::getMethodAnnotations()", function(){
it("should return the complete list of defined annotations", function(){
assert.deepEqual(["$httpGet: 'Person'", "$json"],
annotations.getMethodAnnotations(["$httpGet: 'Person'", "$json", function(){ }]))
assert.deepEqual(["$httpGet: 'Person'", "$json"],
annotations.getMethodAnnotations(["$httpGet: 'Person'", function(){ }, "$json"]))
assert.deepEqual(["$httpGet: 'Person'", "$json"],
annotations.getMethodAnnotations([function(){ }, "$httpGet: 'Person'", "$json"]))
assert.deepEqual(["$override"],
annotations.getMethodAnnotations(["$override", function(){ }]))
})
})
describe("annotations::isValidAnnotationArray()", function(){
it("should check if the given annotations are declared", function(){
assert(annotations.isValidAnnotationArray(["$override", function(){ }]))
})
it("should return false if the annotations are not declared", function(){
assert(!annotations.isValidAnnotationArray(["$httpGet: 'Person'", "$json", function(){ }]))
})
describe("annotations::isValidAnnotationArray()", function(){
it("should check if the given annotations are declared", function(){
assert(annotations.isValidAnnotationArray(["$override", function(){ }]))
})
it("should return false if the annotations are not declared", function(){
assert(!annotations.isValidAnnotationArray(["$httpGet: 'Person'", "$json", function(){ }]))
})
it("if the annotations are declared it must return true", function(){
annotations.add(function $httpGet(){})
annotations.add(function $json(){})
assert(annotations.isValidAnnotationArray(["$httpGet: 'Person'", "$json", function(){ }]))
assert(!annotations.isValidAnnotationArray(["ajdhkasjadh: 'Person'", "$json", function(){ }]))
})
it("if the annotations are declared it must return true", function(){
annotations.add(function $httpGet(){})
annotations.add(function $json(){})
assert(annotations.isValidAnnotationArray(["$httpGet: 'Person'", "$json", function(){ }]))
assert(!annotations.isValidAnnotationArray(["ajdhkasjadh: 'Person'", "$json", function(){ }]))
})
})

0 comments on commit 04b82f5

Please sign in to comment.