UNPKG

6.03 kBJavaScriptView Raw
1"use strict";
2
3var grunt = require('grunt');
4var fs = require('fs');
5
6exports['test-text-replace'] = {
7
8 'Test core replacement functionality': {
9 'Test string replacements': function (test) {
10 test.equal(grunt.helper('text-replace', 'Hello world', 'Hello', 'Goodbye'), 'Goodbye world');
11 test.notEqual(grunt.helper('text-replace', 'Hello w000rld', 'w0*rld', 'world'), 'Hello world');
12 test.equal(grunt.helper('text-replace', 'Hello (*foo.)', '(*foo.)', 'world'), 'Hello world');
13 test.equal(grunt.helper('text-replace', 'Hello \\foo', '\\', ''), 'Hello foo');
14 test.equal(grunt.helper('text-replace', 'Foo bar bar', 'bar', 'foo'), 'Foo foo foo');
15 test.equal(grunt.helper('text-replace', 'Foo bar bar', 'bar', 'Foo bar'), 'Foo Foo bar Foo bar');
16 test.done();
17 },
18
19 'Test regex replacements': function (test) {
20 test.equal(grunt.helper('text-replace', 'Hello world', /Hello/, 'Goodbye'), 'Goodbye world');
21 test.equal(grunt.helper('text-replace', 'Hello world', /(Hello) (world)/, '$2 $1'), 'world Hello');
22 test.equal(grunt.helper('text-replace', 'Foo bar bar', /bar/, 'foo'), 'Foo foo bar');
23 test.equal(grunt.helper('text-replace', 'Foo bar bar', /bar/g, 'foo'), 'Foo foo foo');
24 test.done();
25 },
26
27 'Test grunt.template replacements': function (test) {
28 test.equal(grunt.helper('text-replace', 'Hello world', 'world',
29 '<%= grunt.template.date("20 Nov 2012 11:30:00 GMT", "dd/mm/yy") %>'), 'Hello 20/11/12');
30 test.done();
31 },
32
33 'Test function replacements': function (test) {
34 test.equal(grunt.helper('text-replace', 'Hello world', 'world',
35 function (matchedWord, index, fullText, regexMatches) {
36 return new Array(4).join(matchedWord);
37 }), 'Hello worldworldworld');
38 test.equal(grunt.helper('text-replace', 'Hello world', 'world',
39 function (matchedWord, index, fullText, regexMatches) {
40 return index;
41 }), 'Hello 6');
42 test.equal(grunt.helper('text-replace', 'Hello world', 'Hello',
43 function (matchedWord, index, fullText, regexMatches) {
44 return index;
45 }), '0 world');
46 test.equal(grunt.helper('text-replace', 'Hello world', 'foo',
47 function (matchedWord, index, fullText, regexMatches) {
48 return index;
49 }), 'Hello world');
50 test.equal(grunt.helper('text-replace', 'Hello world', 'world',
51 function (matchedWord, index, fullText, regexMatches) {
52 return fullText;
53 }), 'Hello Hello world');
54 test.equal(grunt.helper('text-replace', 'Hello world', /(Hello) (world)/g,
55 function (matchedWord, index, fullText, regexMatches) {
56 return 'Place: ' + regexMatches[1] + ', Greeting: ' + regexMatches[0];
57 }), 'Place: world, Greeting: Hello');
58 test.equal(grunt.helper('text-replace', 'Hello world', /(Hello) (world)/g,
59 function (matchedWord, index, fullText, regexMatches) {
60 return regexMatches[0] + ' <%= grunt.template.date("20 Nov 2012 11:30:00 GMT", "dd/mm/yy") %>';
61 }), 'Hello 20/11/12');
62 test.done();
63 },
64
65 'Test multiple replacements': function (test) {
66 test.equal(grunt.helper('text-replace-multiple', 'Hello world',
67 [{
68 from: 'Hello',
69 to: 'Hi'
70 }, {
71 from: 'world',
72 to: 'planet'
73 }]), 'Hi planet');
74 test.done();
75 }
76 },
77
78 'Test file handling': {
79 setUp: function (done) {
80 grunt.file.copy('test/text_files/test.txt', 'test/temp/testA.txt');
81 grunt.file.copy('test/text_files/test.txt', 'test/temp/testB.txt');
82 done();
83 },
84
85 tearDown: function (done) {
86 fs.unlinkSync('test/temp/testA.txt');
87 fs.unlinkSync('test/temp/testB.txt');
88 fs.rmdirSync('test/temp');
89 done();
90 },
91
92 'Test change to file specifying destination file': function (test) {
93 var originalText, replacedText;
94 originalText = grunt.file.read('test/temp/testA.txt');
95 grunt.helper('text-replace-file', 'test/temp/testA.txt', 'test/temp/testA.txt', [{from: 'world', to: 'planet'}]);
96 replacedText = grunt.file.read('test/temp/testA.txt');
97 test.equal(originalText, 'Hello world');
98 test.equal(replacedText, 'Hello planet');
99 test.done();
100 },
101
102 'Test change to file specifying destination directory': function (test) {
103 var originalText, replacedText;
104 originalText = grunt.file.read('test/temp/testA.txt');
105 grunt.helper('text-replace-file', 'test/temp/testA.txt', 'test/temp/', [{from: 'world', to: 'planet'}]);
106 replacedText = grunt.file.read('test/temp/testA.txt');
107 test.equal(originalText, 'Hello world');
108 test.equal(replacedText, 'Hello planet');
109 test.done();
110 },
111
112 'Test change to multiple files specifying paths': function (test) {
113 var originalText, replacedTextA, replacedTextB;
114 originalText = grunt.file.read('test/temp/testA.txt');
115 grunt.helper('text-replace-file-multiple', ['test/temp/testA.txt', 'test/temp/testB.txt'], 'test/temp/', [{from: 'world', to: 'planet'}]);
116 replacedTextA = grunt.file.read('test/temp/testA.txt');
117 replacedTextB = grunt.file.read('test/temp/testB.txt');
118 test.equal(originalText, 'Hello world');
119 test.equal(replacedTextA, 'Hello planet');
120 test.equal(replacedTextB, 'Hello planet');
121 test.done();
122 },
123
124 'Test change to multiple files specifying minimatch paths': function (test) {
125 var originalText, replacedTextA, replacedTextB;
126 originalText = grunt.file.read('test/temp/testA.txt');
127 grunt.helper('text-replace-file-multiple', ['test/temp/test*.txt'], 'test/temp/', [{from: 'world', to: 'planet'}]);
128 replacedTextA = grunt.file.read('test/temp/testA.txt');
129 replacedTextB = grunt.file.read('test/temp/testB.txt');
130 test.equal(originalText, 'Hello world');
131 test.equal(replacedTextA, 'Hello planet');
132 test.equal(replacedTextB, 'Hello planet');
133 test.done();
134 }
135 }
136
137};
\No newline at end of file