123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- var grunt = require('grunt');
- var helper = require('../lib/contrib.js').init(grunt);
- exports.lib = {
- getNamespaceDeclaration: function(test) {
- 'use strict';
- test.expect(10);
- // Both test should result in this[JST]
- var expected = {
- namespace: 'this["JST"]',
- declaration: 'this["JST"] = this["JST"] || {};'
- };
- var actual = helper.getNamespaceDeclaration("this.JST");
- test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
- test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
- actual = helper.getNamespaceDeclaration("JST");
- test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
- test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
- // Templates should be declared globally if this provided
- expected = {
- namespace: "this",
- declaration: ""
- };
- actual = helper.getNamespaceDeclaration("this");
- test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
- test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
- // Nested namespace declaration
- expected = {
- namespace: 'this["GUI"]["Templates"]["Main"]',
- declaration: 'this["GUI"] = this["GUI"] || {};\n' +
- 'this["GUI"]["Templates"] = this["GUI"]["Templates"] || {};\n' +
- 'this["GUI"]["Templates"]["Main"] = this["GUI"]["Templates"]["Main"] || {};'
- };
- actual = helper.getNamespaceDeclaration("GUI.Templates.Main");
- test.equal(expected.namespace, actual.namespace, 'namespace incorrect');
- test.equal(expected.declaration, actual.declaration, 'namespace declaration incorrect');
- // Namespace that contains square brackets
- expected = {
- namespace: 'this["main"]["[test]"]["[test2]"]',
- declaration: 'this["main"] = this["main"] || {};\n' +
- 'this["main"]["[test]"] = this["main"]["[test]"] || {};\n' +
- 'this["main"]["[test]"]["[test2]"] = this["main"]["[test]"]["[test2]"] || {};'
- };
- actual = helper.getNamespaceDeclaration("main.[test].[test2]");
- test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
- test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
- test.done();
- },
- optsToArgs: function(test) {
- 'use strict';
- test.expect(1);
- var fixture = {
- key: 'a',
- key2: 1,
- key3: true,
- key4: false,
- key5: ['a', 'b']
- };
- var expected = ['--key', 'a', '--key2', '1', '--key3', '--key5', 'a', '--key5', 'b' ].toString();
- var actual = helper.optsToArgs(fixture).toString();
- test.equal(expected, actual, 'should convert object to array of CLI arguments');
- test.done();
- },
- stripPath: function(test) {
- 'use strict';
- var path = require('path');
- test.expect(4);
- var actual = helper.stripPath('path1/path2', 'path1');
- var expected = 'path2';
- test.equal(expected, actual, 'should strip path from a directory path and trim it.');
- actual = helper.stripPath('path1/path2/path3/path4', 'path1/path2');
- expected = path.normalize('path3/path4');
- test.equal(expected, actual, 'should strip path from a directory path and trim it. (deep)');
- actual = helper.stripPath('path1/file.ext', 'path1');
- expected = 'file.ext';
- test.equal(expected, actual, 'should strip path from a file path and trim it.');
- actual = helper.stripPath('path1/path2/path3/path4/file.ext', 'path1/path2');
- expected = path.normalize('path3/path4/file.ext');
- test.equal(expected, actual, 'should strip path from a file path and trim it. (deep)');
- test.done();
- },
- minMaxInfo: function(test) {
- 'use strict';
- test.expect(3);
- var max = new Array(100).join('blah ');
- var min = max.replace(/\s+/g, '');
- var actual;
- var expected;
- grunt.util.hooker.hook(grunt.log, 'writeln', {
- pre: function(result) {
- actual += grunt.log.uncolor(result) + grunt.util.linefeed;
- return grunt.util.hooker.preempt();
- }
- });
- grunt.util.hooker.hook(grunt.log, 'write', {
- pre: function(result) {
- actual += grunt.log.uncolor(result);
- return grunt.util.hooker.preempt();
- }
- });
- // No reporting option
- actual = '';
- expected = '';
- helper.minMaxInfo(min, max);
- test.equal(expected, actual, 'should not have reported min and max info.');
- // Report minification results
- actual = '';
- expected = [
- 'Original: 495 bytes.',
- 'Minified: 396 bytes.'
- ].join(grunt.util.linefeed) + grunt.util.linefeed;
- helper.minMaxInfo(min, max, 'min');
- test.equal(expected, actual, 'should have logged min and max info.');
- // Report minification and gzip results
- actual = '';
- expected = [
- 'Original: 495 bytes.',
- 'Minified: 396 bytes.',
- 'Gzipped: 36 bytes.'
- ].join(grunt.util.linefeed) + grunt.util.linefeed;
- helper.minMaxInfo(min, max, 'gzip');
- test.equal(expected, actual, 'should have logged min, max, gzip info.');
- grunt.util.hooker.unhook(grunt.log, 'writeln');
- grunt.util.hooker.unhook(grunt.log, 'write');
- test.done();
- },
- formatToType: {
- amd: function(test) {
- 'use strict';
- test.expect(2);
- var string = function () { };
- var actual = helper.formatForType(string, 'amd', 'JST', 'test');
- var expected = 'JST["test"] = function () { };';
- test.equal(expected, actual, 'should format string to amd with namespace');
- actual = helper.formatForType(string, 'amd');
- expected = "return function () { }";
- test.equal(expected, actual, 'should format string to amd');
- test.done();
- },
- commonjs: function(test) {
- 'use strict';
- test.expect(2);
- var string = function () { };
- var actual = helper.formatForType(string, 'commonjs', 'JST', 'test');
- var expected = 'JST["test"] = function () { };';
- test.equal(expected, actual, 'should format string to commonjs with namespace');
- actual = helper.formatForType(string, 'commonjs');
- expected = "module.exports = function () { }";
- test.equal(expected, actual, 'should format string to commonjs');
- test.done();
- },
- js: function(test) {
- 'use strict';
- test.expect(2);
- var string = function () { };
- var actual = helper.formatForType(string, 'js', 'JST', 'test');
- var expected = 'JST["test"] = function () { };';
- test.equal(expected, actual, 'should format string to js with namespace');
- actual = helper.formatForType(string, 'js');
- expected = 'function () { }';
- test.equal(expected, actual, 'should format string to js');
- test.done();
- },
- html: function(test) {
- 'use strict';
- test.expect(2);
- var string = function () { };
- var actual = helper.formatForType(string, 'html', 'JST', 'test');
- var expected = 'function () { }';
- test.equal(expected, actual, 'should format string to html with namespace');
- actual = helper.formatForType(string, 'html');
- expected = 'function () { }';
- test.equal(expected, actual, 'should format string to html');
- test.done();
- }
- }
- };
|