lib_test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var grunt = require('grunt');
  2. var helper = require('../lib/contrib.js').init(grunt);
  3. exports.lib = {
  4. getNamespaceDeclaration: function(test) {
  5. 'use strict';
  6. test.expect(10);
  7. // Both test should result in this[JST]
  8. var expected = {
  9. namespace: 'this["JST"]',
  10. declaration: 'this["JST"] = this["JST"] || {};'
  11. };
  12. var actual = helper.getNamespaceDeclaration("this.JST");
  13. test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
  14. test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
  15. actual = helper.getNamespaceDeclaration("JST");
  16. test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
  17. test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
  18. // Templates should be declared globally if this provided
  19. expected = {
  20. namespace: "this",
  21. declaration: ""
  22. };
  23. actual = helper.getNamespaceDeclaration("this");
  24. test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
  25. test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
  26. // Nested namespace declaration
  27. expected = {
  28. namespace: 'this["GUI"]["Templates"]["Main"]',
  29. declaration: 'this["GUI"] = this["GUI"] || {};\n' +
  30. 'this["GUI"]["Templates"] = this["GUI"]["Templates"] || {};\n' +
  31. 'this["GUI"]["Templates"]["Main"] = this["GUI"]["Templates"]["Main"] || {};'
  32. };
  33. actual = helper.getNamespaceDeclaration("GUI.Templates.Main");
  34. test.equal(expected.namespace, actual.namespace, 'namespace incorrect');
  35. test.equal(expected.declaration, actual.declaration, 'namespace declaration incorrect');
  36. // Namespace that contains square brackets
  37. expected = {
  38. namespace: 'this["main"]["[test]"]["[test2]"]',
  39. declaration: 'this["main"] = this["main"] || {};\n' +
  40. 'this["main"]["[test]"] = this["main"]["[test]"] || {};\n' +
  41. 'this["main"]["[test]"]["[test2]"] = this["main"]["[test]"]["[test2]"] || {};'
  42. };
  43. actual = helper.getNamespaceDeclaration("main.[test].[test2]");
  44. test.equal(expected.namespace, actual.namespace, 'namespace with square brackets incorrect');
  45. test.equal(expected.declaration, actual.declaration, 'namespace declaration with square brackets incorrect');
  46. test.done();
  47. },
  48. optsToArgs: function(test) {
  49. 'use strict';
  50. test.expect(1);
  51. var fixture = {
  52. key: 'a',
  53. key2: 1,
  54. key3: true,
  55. key4: false,
  56. key5: ['a', 'b']
  57. };
  58. var expected = ['--key', 'a', '--key2', '1', '--key3', '--key5', 'a', '--key5', 'b' ].toString();
  59. var actual = helper.optsToArgs(fixture).toString();
  60. test.equal(expected, actual, 'should convert object to array of CLI arguments');
  61. test.done();
  62. },
  63. stripPath: function(test) {
  64. 'use strict';
  65. var path = require('path');
  66. test.expect(4);
  67. var actual = helper.stripPath('path1/path2', 'path1');
  68. var expected = 'path2';
  69. test.equal(expected, actual, 'should strip path from a directory path and trim it.');
  70. actual = helper.stripPath('path1/path2/path3/path4', 'path1/path2');
  71. expected = path.normalize('path3/path4');
  72. test.equal(expected, actual, 'should strip path from a directory path and trim it. (deep)');
  73. actual = helper.stripPath('path1/file.ext', 'path1');
  74. expected = 'file.ext';
  75. test.equal(expected, actual, 'should strip path from a file path and trim it.');
  76. actual = helper.stripPath('path1/path2/path3/path4/file.ext', 'path1/path2');
  77. expected = path.normalize('path3/path4/file.ext');
  78. test.equal(expected, actual, 'should strip path from a file path and trim it. (deep)');
  79. test.done();
  80. },
  81. minMaxInfo: function(test) {
  82. 'use strict';
  83. test.expect(3);
  84. var max = new Array(100).join('blah ');
  85. var min = max.replace(/\s+/g, '');
  86. var actual;
  87. var expected;
  88. grunt.util.hooker.hook(grunt.log, 'writeln', {
  89. pre: function(result) {
  90. actual += grunt.log.uncolor(result) + grunt.util.linefeed;
  91. return grunt.util.hooker.preempt();
  92. }
  93. });
  94. grunt.util.hooker.hook(grunt.log, 'write', {
  95. pre: function(result) {
  96. actual += grunt.log.uncolor(result);
  97. return grunt.util.hooker.preempt();
  98. }
  99. });
  100. // No reporting option
  101. actual = '';
  102. expected = '';
  103. helper.minMaxInfo(min, max);
  104. test.equal(expected, actual, 'should not have reported min and max info.');
  105. // Report minification results
  106. actual = '';
  107. expected = [
  108. 'Original: 495 bytes.',
  109. 'Minified: 396 bytes.'
  110. ].join(grunt.util.linefeed) + grunt.util.linefeed;
  111. helper.minMaxInfo(min, max, 'min');
  112. test.equal(expected, actual, 'should have logged min and max info.');
  113. // Report minification and gzip results
  114. actual = '';
  115. expected = [
  116. 'Original: 495 bytes.',
  117. 'Minified: 396 bytes.',
  118. 'Gzipped: 36 bytes.'
  119. ].join(grunt.util.linefeed) + grunt.util.linefeed;
  120. helper.minMaxInfo(min, max, 'gzip');
  121. test.equal(expected, actual, 'should have logged min, max, gzip info.');
  122. grunt.util.hooker.unhook(grunt.log, 'writeln');
  123. grunt.util.hooker.unhook(grunt.log, 'write');
  124. test.done();
  125. },
  126. formatToType: {
  127. amd: function(test) {
  128. 'use strict';
  129. test.expect(2);
  130. var string = function () { };
  131. var actual = helper.formatForType(string, 'amd', 'JST', 'test');
  132. var expected = 'JST["test"] = function () { };';
  133. test.equal(expected, actual, 'should format string to amd with namespace');
  134. actual = helper.formatForType(string, 'amd');
  135. expected = "return function () { }";
  136. test.equal(expected, actual, 'should format string to amd');
  137. test.done();
  138. },
  139. commonjs: function(test) {
  140. 'use strict';
  141. test.expect(2);
  142. var string = function () { };
  143. var actual = helper.formatForType(string, 'commonjs', 'JST', 'test');
  144. var expected = 'JST["test"] = function () { };';
  145. test.equal(expected, actual, 'should format string to commonjs with namespace');
  146. actual = helper.formatForType(string, 'commonjs');
  147. expected = "module.exports = function () { }";
  148. test.equal(expected, actual, 'should format string to commonjs');
  149. test.done();
  150. },
  151. js: function(test) {
  152. 'use strict';
  153. test.expect(2);
  154. var string = function () { };
  155. var actual = helper.formatForType(string, 'js', 'JST', 'test');
  156. var expected = 'JST["test"] = function () { };';
  157. test.equal(expected, actual, 'should format string to js with namespace');
  158. actual = helper.formatForType(string, 'js');
  159. expected = 'function () { }';
  160. test.equal(expected, actual, 'should format string to js');
  161. test.done();
  162. },
  163. html: function(test) {
  164. 'use strict';
  165. test.expect(2);
  166. var string = function () { };
  167. var actual = helper.formatForType(string, 'html', 'JST', 'test');
  168. var expected = 'function () { }';
  169. test.equal(expected, actual, 'should format string to html with namespace');
  170. actual = helper.formatForType(string, 'html');
  171. expected = 'function () { }';
  172. test.equal(expected, actual, 'should format string to html');
  173. test.done();
  174. }
  175. }
  176. };