rename.spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*global describe, context, it, beforeEach, helper, helperError */
  2. "use strict";
  3. require("./spec-helper");
  4. describe("gulp-rename", function () {
  5. context("with string parameter", function () {
  6. context("when src pattern does not contain directory glob", function () {
  7. it("sets filename to value", function (done) {
  8. var srcPattern = "test/fixtures/hello.txt";
  9. var obj = "hola.md";
  10. var expectedPath = "test/fixtures/hola.md";
  11. helper(srcPattern, obj, expectedPath, done);
  12. });
  13. });
  14. context("when src pattern contains directory glob", function () {
  15. it("sets relative path to value", function (done) {
  16. var srcPattern = "test/**/hello.txt";
  17. var obj = "fixtures/hola.md";
  18. var expectedPath = "test/fixtures/hola.md";
  19. helper(srcPattern, obj, expectedPath, done);
  20. });
  21. });
  22. });
  23. context("with object parameter", function () {
  24. var srcPattern;
  25. beforeEach(function () {
  26. srcPattern = "test/**/hello.txt";
  27. });
  28. context("with empty object", function () {
  29. it("has no effect", function (done) {
  30. var obj = {};
  31. var expectedPath = "test/fixtures/hello.txt";
  32. helper(srcPattern, obj, expectedPath, done);
  33. });
  34. });
  35. context("with dirname value", function () {
  36. it("replaces dirname with value", function (done) {
  37. var obj = {
  38. dirname: "elsewhere",
  39. };
  40. var expectedPath = "test/elsewhere/hello.txt";
  41. helper(srcPattern, obj, expectedPath, done);
  42. });
  43. it("removes dirname with './'", function (done) {
  44. var obj = {
  45. dirname: "./",
  46. };
  47. var expectedPath = "test/hello.txt";
  48. helper(srcPattern, obj, expectedPath, done);
  49. });
  50. it("removes dirname with empty string", function (done) {
  51. var obj = {
  52. dirname: "",
  53. };
  54. var expectedPath = "test/hello.txt";
  55. helper(srcPattern, obj, expectedPath, done);
  56. });
  57. });
  58. context("with prefix value", function () {
  59. it("prepends value to basename", function (done) {
  60. var obj = {
  61. prefix: "bonjour-",
  62. };
  63. var expectedPath = "test/fixtures/bonjour-hello.txt";
  64. helper(srcPattern, obj, expectedPath, done);
  65. });
  66. });
  67. context("with basename value", function () {
  68. it("replaces basename with value", function (done) {
  69. var obj = {
  70. basename: "aloha",
  71. };
  72. var expectedPath = "test/fixtures/aloha.txt";
  73. helper(srcPattern, obj, expectedPath, done);
  74. });
  75. it("removes basename with empty string (for consistency)", function (done) {
  76. var obj = {
  77. prefix: "aloha",
  78. basename: "",
  79. };
  80. var expectedPath = "test/fixtures/aloha.txt";
  81. helper(srcPattern, obj, expectedPath, done);
  82. });
  83. });
  84. context("with suffix value", function () {
  85. it("appends value to basename", function (done) {
  86. var obj = {
  87. suffix: "-hola",
  88. };
  89. var expectedPath = "test/fixtures/hello-hola.txt";
  90. helper(srcPattern, obj, expectedPath, done);
  91. });
  92. });
  93. context("with extname value", function () {
  94. it("replaces extname with value", function (done) {
  95. var obj = {
  96. extname: ".md",
  97. };
  98. var expectedPath = "test/fixtures/hello.md";
  99. helper(srcPattern, obj, expectedPath, done);
  100. });
  101. it("removes extname with empty string", function (done) {
  102. var obj = {
  103. extname: "",
  104. };
  105. var expectedPath = "test/fixtures/hello";
  106. helper(srcPattern, obj, expectedPath, done);
  107. });
  108. });
  109. });
  110. context("with function parameter", function () {
  111. var srcPattern;
  112. beforeEach(function () {
  113. srcPattern = "test/**/hello.txt";
  114. });
  115. it("receives object with dirname", function (done) {
  116. var obj = function (path) {
  117. path.dirname.should.equal("fixtures");
  118. path.dirname = "elsewhere";
  119. };
  120. var expectedPath = "test/elsewhere/hello.txt";
  121. helper(srcPattern, obj, expectedPath, done);
  122. });
  123. it("receives object with basename", function (done) {
  124. var obj = function (path) {
  125. path.basename.should.equal("hello");
  126. path.basename = "aloha";
  127. };
  128. var expectedPath = "test/fixtures/aloha.txt";
  129. helper(srcPattern, obj, expectedPath, done);
  130. });
  131. it("receives object with extname", function (done) {
  132. var obj = function (path) {
  133. path.extname.should.equal(".txt");
  134. path.extname = ".md";
  135. };
  136. var expectedPath = "test/fixtures/hello.md";
  137. helper(srcPattern, obj, expectedPath, done);
  138. });
  139. it("can return a whole new object", function (done) {
  140. var obj = function (/*path*/) {
  141. return {
  142. dirname: "elsewhere",
  143. basename: "aloha",
  144. extname: ".md"
  145. };
  146. };
  147. var expectedPath = "test/elsewhere/aloha.md";
  148. helper(srcPattern, obj, expectedPath, done);
  149. });
  150. });
  151. context("throws unsupported parameter type", function () {
  152. var srcPattern;
  153. beforeEach(function () {
  154. srcPattern = "test/**/hello.txt";
  155. });
  156. var UNSUPPORTED_PARAMATER = "Unsupported renaming parameter type supplied";
  157. it("with undefined object", function (done) {
  158. var obj;
  159. var expectedError = UNSUPPORTED_PARAMATER;
  160. helperError(srcPattern, obj, expectedError, done);
  161. });
  162. it("with null object", function (done) {
  163. var obj = null;
  164. var expectedError = UNSUPPORTED_PARAMATER;
  165. helperError(srcPattern, obj, expectedError, done);
  166. });
  167. it("with empty string", function (done) {
  168. var obj = "";
  169. var expectedError = UNSUPPORTED_PARAMATER;
  170. helperError(srcPattern, obj, expectedError, done);
  171. });
  172. it("with boolean value", function (done) {
  173. var obj = true;
  174. var expectedError = UNSUPPORTED_PARAMATER;
  175. helperError(srcPattern, obj, expectedError, done);
  176. });
  177. it("with numeric value", function (done) {
  178. var obj = 1;
  179. var expectedError = UNSUPPORTED_PARAMATER;
  180. helperError(srcPattern, obj, expectedError, done);
  181. });
  182. });
  183. });