ng-test-utils
Advanced tools
Comparing version 0.0.16 to 0.0.17
{ | ||
"name": "ng-test-utils", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "angular test utilities", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -506,2 +506,9 @@ ng-test-utils | ||
`@ngController` can also be used to annotate variable declarations (the declaration must have an initialization value). | ||
If the initialization value is a function, it will work exactly as above; the function will be instrumented to | ||
push each new controller instance on to the array. If the initialization is not a function declaration | ||
(a call to `sinon.spy()` perhaps), things will still work, but you do not get an auto populating array. | ||
This is not a problem for spies as you generally have another means of reaching those value. | ||
source-maps | ||
@@ -508,0 +515,0 @@ ----------- |
@@ -16,41 +16,31 @@ module.exports.replace = create; | ||
types.visit(node, { | ||
visitVariableDeclaration: function(path) { | ||
var node = path.node; | ||
if (hasAnnotation(node)) { | ||
installControllers(path, node.declarations.map(function(decl) { | ||
n.VariableDeclarator.assert(decl); | ||
var expr = decl.init; | ||
assert(expr, 'must have an init'); | ||
var init = null; | ||
if (n.Function.check(expr)) { | ||
instrumentControllerFunction(expr, decl.id); | ||
init = b.arrayExpression([]); | ||
} else { | ||
expr = b.assignmentExpression('=', decl.id, expr); | ||
} | ||
return {id: decl.id, expr: expr, init:init}; | ||
})); | ||
return false; | ||
} | ||
this.traverse(path); | ||
}, | ||
visitFunctionDeclaration: function(path) { | ||
var node = path.node; | ||
var id = node.id; | ||
if (hasAnnotation(node)) { | ||
instrumentControllerFunction(node, node.id); | ||
// inject any | ||
types.visit(node.body, { | ||
visitFunction: function() { | ||
return false; | ||
}, | ||
visitReturnStatement: function(path) { | ||
pushReturnStatement(path, id); | ||
return false; | ||
} | ||
}); | ||
var stmts = node.body.body; | ||
if (!n.ReturnStatement.check(stmts[stmts.length - 1])) { | ||
stmts.push(pushThisStatement(id)); | ||
} | ||
// strip the name from the function declaration so it does not shadow the variable; | ||
var controllerExpr = b.functionExpression(null, node.params, node.body); | ||
path.replace( | ||
// var a = | ||
b.variableDeclaration('var', [b.variableDeclarator(id, b.arrayExpression([]))]), | ||
callStmt( | ||
i.beforeEach, | ||
[b.callExpression( | ||
mAngularMockModule, | ||
[callback( | ||
[i.$controllerProvider], | ||
[callStmt(m$controllerProviderRegister, [b.literal(id.name), controllerExpr])] | ||
)] | ||
)] | ||
) | ||
); | ||
installControllers(path, [{id: node.id, expr:controllerExpr, init:b.arrayExpression([])}]); | ||
return false; | ||
@@ -64,2 +54,41 @@ } | ||
function instrumentControllerFunction(node, id) { | ||
n.Function.assert(node); | ||
types.visit(node.body, { | ||
visitFunction: function() { | ||
return false; | ||
}, | ||
visitReturnStatement: function(path) { | ||
pushReturnStatement(path, id); | ||
return false; | ||
} | ||
}); | ||
var stmts = node.body.body; | ||
if (!n.ReturnStatement.check(stmts[stmts.length - 1])) { | ||
stmts.push(pushThisStatement(id)); | ||
} | ||
} | ||
function installControllers(path, exprs) { | ||
path.replace( | ||
b.variableDeclaration('var', exprs.map(function(val) { | ||
return b.variableDeclarator(val.id, val.init || null); | ||
})), | ||
callStmt( | ||
i.beforeEach, | ||
[b.callExpression( | ||
mAngularMockModule, | ||
[callback( | ||
[i.$controllerProvider], | ||
exprs.map(function(val) { | ||
return callStmt(m$controllerProviderRegister, [b.literal(val.id.name), val.expr]); | ||
}) | ||
)] | ||
)] | ||
) | ||
); | ||
} | ||
// `return <arg>;` | ||
@@ -66,0 +95,0 @@ // becomes |
55188
1285
560