import ngmin tests

This commit is contained in:
Olov Lassus
2014-05-09 13:01:46 +02:00
parent 886eab1ce6
commit 63db245dbe
8 changed files with 731 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
Tests imported 2014-05-09 from https://github.com/btford/ngmin.git
(revision be63c57076dc8fca81347260ca1e5923ccdbf8ec)
ngmin is MIT licensed, author Brian Ford
../ngmin_original.js and ../ngmin_with_annotations.js are reduced versions
of the original test files.
+135
View File
@@ -0,0 +1,135 @@
/*
* Test chained declarations
* angular.module('myMod', []).
* controller( ... ).
* controller( ... );
*/
var assert = require('should');
// so we don't have to put the stuff we're testing into a string
var stringifyFunctionBody = require('./util').stringifyFunctionBody;
var annotate = function (arg) {
return require('../main').annotate(
stringifyFunctionBody(arg));
};
describe('annotate', function () {
it('should annotate chained declarations', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
service('myService', function (dep) {}).
service('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
service('myService', ['dep', function (dep) {}]).
service('MyCtrl', ['$scope', function ($scope) {}]);
}));
});
it('should annotate multiple chained declarations', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
service('myService', function (dep) {}).
service('myService2', function (dep) {}).
service('myService3', function (dep) {}).
service('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
service('myService', ['dep', function (dep) {}]).
service('myService2', ['dep', function (dep) {}]).
service('myService3', ['dep', function (dep) {}]).
service('MyCtrl', ['$scope', function ($scope) {}]);
}));
});
it('should annotate multiple chained declarations on constants', function() {
var annotated = annotate(function () {
angular.module('myMod', []).
constant('myConstant', 'someConstant').
constant('otherConstant', 'otherConstant').
service('myService1', function (dep) {}).
service('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
constant('myConstant', 'someConstant').
constant('otherConstant', 'otherConstant').
service('myService1', ['dep', function (dep) {}]).
service('MyCtrl', ['$scope', function ($scope) {}]);
}));
});
it('should annotate multiple chained declarations on values', function() {
var annotated = annotate(function () {
angular.module('myMod', []).
value('myConstant', 'someConstant').
value('otherConstant', 'otherConstant').
service('myService1', function (dep) {}).
service('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
value('myConstant', 'someConstant').
value('otherConstant', 'otherConstant').
service('myService1', ['dep', function (dep) {}]).
service('MyCtrl', ['$scope', function ($scope) {}]);
}));
});
it('should annotate multiple chained declarations on constants and value regardless of order', function() {
var annotated = annotate(function () {
angular.module('myMod', []).
value('myConstant', 'someConstant').
service('myService1', function (dep) {}).
constant('otherConstant', 'otherConstant').
service('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
value('myConstant', 'someConstant').
service('myService1', ['dep', function (dep) {}]).
constant('otherConstant', 'otherConstant').
service('MyCtrl', ['$scope', function ($scope) {}]);
}));
});
it('should annotate refs that have been chained', function () {
var annotated = annotate(function () {
var mod = angular.module('chain', []);
mod.factory('a', function ($scope){}).
factory('b', function ($scope){});
});
annotated.should.equal(stringifyFunctionBody(function () {
var mod = angular.module('chain', []);
mod.factory('a', ['$scope', function($scope){}]).
factory('b', ['$scope', function($scope){}]);
}));
});
it('should annotate refs to chains', function () {
var annotated = annotate(function () {
var mod = angular.module('chain', []).
factory('a', function ($scope){});
mod.factory('b', function ($scope){});
});
annotated.should.equal(stringifyFunctionBody(function () {
var mod = angular.module('chain', []).
factory('a', ['$scope', function($scope){}]);
mod.factory('b', ['$scope', function($scope){}]);
}));
});
});
+70
View File
@@ -0,0 +1,70 @@
/*
* Test annotations within the Directive Definition Object (DDO):
*
* angular.module('myMod', []).directive('whatever', function () {
* return {
* controller: function ($scope) { ... } // <--- this needs annotations
* };
* })
*
*/
var assert = require('should');
// so we don't have to put the stuff we're testing into a string
var stringifyFunctionBody = require('./util').stringifyFunctionBody;
var annotate = function (arg) {
return require('../main').annotate(
stringifyFunctionBody(arg));
};
describe('annotate', function () {
it('should annotate directive controllers', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
directive('myDir', function () {
return {
controller: function ($scope) { $scope.test = true; }
};
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
directive('myDir', function () {
return {
controller: [
'$scope',
function ($scope) { $scope.test = true; }
]
};
});
}));
});
it('should annotate directive controllers of annotated directives', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
directive('myDir', function ($window) {
return {
controller: function ($scope) { $scope.test = true; }
};
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
directive('myDir', ['$window', function ($window) {
return {
controller: [
'$scope',
function ($scope) { $scope.test = true; }
]
};
}]);
}));
});
});
+56
View File
@@ -0,0 +1,56 @@
/*
* Test for angular modules that are wrapped by goofy
* 3rd party loaders like Require.js
*/
var assert = require('should');
// so we don't have to put the stuff we're testing into a string
var stringifyFunctionBody = require('./util').stringifyFunctionBody;
var annotate = function (arg) {
return require('../main').annotate(
stringifyFunctionBody(arg));
};
describe('annotate', function () {
it('should annotate modules inside of loaders', function () {
var annotated = annotate(function () {
define(["./thing"], function(thing) {
angular.module('myMod', []).
controller('MyCtrl', function ($scope) {});
});
});
annotated.should.equal(stringifyFunctionBody(function () {
define(["./thing"], function(thing) {
angular.module('myMod', []).
controller('MyCtrl', ['$scope', function ($scope) {}]);
});
}));
});
it('should annotate module refs inside of loaders', function () {
var annotated = annotate(function () {
define(["./thing"], function(thing) {
var myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', function ($scope) {});
return myMod;
});
});
annotated.should.equal(stringifyFunctionBody(function () {
define(["./thing"], function(thing) {
var myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', ['$scope', function ($scope) {}]);
return myMod;
});
}));
});
});
+120
View File
@@ -0,0 +1,120 @@
/*
* Test cases where there's a reference to a module
*
* var myMod = angular.module('myMod', []);
* myMod.controller( ... )
*
*/
var assert = require('should');
// so we don't have to put the stuff we're testing into a string
var stringifyFunctionBody = require('./util').stringifyFunctionBody;
var annotate = function (arg) {
return require('../main').annotate(
stringifyFunctionBody(arg));
};
describe('annotate', function () {
it('should annotate declarations on referenced modules', function () {
var annotated = annotate(function () {
var myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
var myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', [
'$scope',
function ($scope) {
}
]);
}));
});
it('should annotate declarations on referenced modules when reference is declared then initialized', function () {
var annotated = annotate(function () {
var myMod;
myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
var myMod;
myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', [
'$scope',
function ($scope) {
}
]);
}));
});
it('should annotate object-defined providers on referenced modules', function () {
var annotated = annotate(function () {
var myMod;
myMod = angular.module('myMod', []);
myMod.provider('MyService', { $get: function(service) {} });
});
annotated.should.equal(stringifyFunctionBody(function () {
var myMod;
myMod = angular.module('myMod', []);
myMod.provider('MyService', {
$get: ['service', function(service) {}]
});
}));
});
// TODO: lol commenting out test cases
/*
it('should annotate declarations on referenced modules ad infinitum', function () {
var annotated = annotate(function () {
var myMod = angular.module('myMod', []);
var myMod2 = myMod, myMod3;
myMod3 = myMod2;
myMod3.controller('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
var myMod = angular.module('myMod', []);
var myMod2 = myMod, myMod3;
myMod3 = myMod2;
myMod3.controller('MyCtrl', ['$scope', function ($scope) {}]);
}));
});
*/
// TODO: it should annotate silly assignment chains
it('should not annotate declarations on non-module objects', function () {
var fn = function () {
var myMod, myOtherMod;
myMod = angular.module('myMod', []);
myOtherMod.controller('MyCtrl', function ($scope) {});
};
var annotated = annotate(fn);
annotated.should.equal(stringifyFunctionBody(fn));
});
it('should keep comments', function() {
var annotated = annotate(function () {
var myMod = angular.module('myMod', []);
/*! license */
myMod.controller('MyCtrl', function ($scope) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
var myMod = angular.module('myMod', []);
/*! license */
myMod.controller('MyCtrl', [
'$scope',
function ($scope) {
}
]);
}));
});
});
+86
View File
@@ -0,0 +1,86 @@
/*
* Test annotations within the Directive Definition Object (DDO):
*
* angular.module('myMod', []).directive('whatever', function () {
* return {
* controller: function ($scope) { ... } // <--- this needs annotations
* };
* })
*
*/
var assert = require('should');
// so we don't have to put the stuff we're testing into a string
var stringifyFunctionBody = require('./util').stringifyFunctionBody;
var annotate = function (arg) {
return require('../main').annotate(
stringifyFunctionBody(arg));
};
describe('annotate', function () {
it('should annotate $routeProvider.when()', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
config(function ($routeProvider) {
$routeProvider.when('path', {
controller: function ($scope) {
$scope.works = true;
}
});
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('path', {
controller: ['$scope', function ($scope) {
$scope.works = true;
}]
});
}]);
}));
});
it('should annotate chained $routeProvider.when()', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
config(function ($routeProvider) {
$routeProvider.
when('path', {
controller: function ($scope) {
$scope.works = true;
}
}).
when('other/path', {
controller: function ($http) {
$http.get();
}
});
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('path', {
controller: ['$scope', function ($scope) {
$scope.works = true;
}]
}).
when('other/path', {
controller: ['$http', function ($http) {
$http.get();
}]
});
}]);
}));
});
});
+244
View File
@@ -0,0 +1,244 @@
/*
* Test simple cases
*
* angular.module('myMod', []).controller( ... );
*
*/
var assert = require('should');
// so we don't have to put the stuff we're testing into a string
var stringifyFunctionBody = require('./util').stringifyFunctionBody;
var annotate = function (arg) {
return require('../main').annotate(
stringifyFunctionBody(arg));
};
describe('annotate', function () {
it('should annotate controllers', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
controller('MyCtrl', function ($scope) {
$scope.foo = 'bar';
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).controller('MyCtrl', [
'$scope',
function ($scope) {
$scope.foo = 'bar';
}
]);
}));
});
it('should annotate directives', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
directive('myDirective', function ($rootScope) {
return {
restrict: 'E',
template: 'sup'
};
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).directive('myDirective', [
'$rootScope',
function ($rootScope) {
return {
restrict: 'E',
template: 'sup'
};
}
]);
}));
});
it('should annotate filters', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
filter('myFilter', function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).filter('myFilter', [
'dep',
function (dep) {
}
]);
}));
});
it('should annotate services', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
service('myService', function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).service('myService', [
'dep',
function (dep) {
}
]);
}));
});
it('should annotate factories', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
controller('factory', function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).controller('factory', [
'dep',
function (dep) {
}
]);
}));
});
it('should annotate decorators', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
decorator('myService', function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).decorator('myService', [
'dep',
function (dep) {
}
]);
}));
});
it('should annotate config', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
config(function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).config([
'dep',
function (dep) {
}
]);
}));
});
it('should annotate run', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
run(function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).run([
'dep',
function (dep) {
}
]);
}));
});
it('should annotate providers defined by functions', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
provider('myService', function (dep) {
this.$get = function(otherDep) {};
});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).provider('myService', [
'dep',
function (dep) {
this.$get = ['otherDep', function(otherDep) {}];
}
]);
}));
});
it('should annotate providers defined by objects', function () {
var annotated = annotate(function () {
angular.module('myMod', []).
provider('myService', {
$get: function(otherDep) {}
})
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
provider('myService', {
$get: ['otherDep', function(otherDep) {}]
});
}));
});
it('should annotate declarations on modules being referenced', function () {
var annotated = annotate(function () {
angular.module('myMod', []);
angular.module('myMod').
provider('myService', function (dep) {});
});
annotated.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []);
angular.module('myMod').provider('myService', [
'dep',
function (dep) {
}
]);
}));
});
it('should not annotate declarations with no dependencies', function () {
var fn = function () {
angular.module('myMod', []).
provider('myService', function () {});
};
var annotated = annotate(fn);
annotated.should.equal(stringifyFunctionBody(fn));
});
it('should not annotate constants', function () {
var fn = function () {
angular.module('myMod', []).constant('fortyTwo', 42);
};
var annotated = annotate(fn);
annotated.should.equal(stringifyFunctionBody(fn));
});
it('should not annotate values', function () {
var fn = function () {
angular.module('myMod', []).value('fortyTwo', 42);
};
var annotated = annotate(fn);
annotated.should.equal(stringifyFunctionBody(fn));
});
});
+14
View File
@@ -0,0 +1,14 @@
/*
* Test utils
*/
var body = require('fn-body'),
normalize = require('normalize-fn');
// given a function, return its body as a normalized string.
// makes tests look a bit cleaner
exports.stringifyFunctionBody = function (fn) {
return normalize(body(fn));
};