UNPKG

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