-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathcompile.js
More file actions
292 lines (260 loc) · 9.04 KB
/
compile.js
File metadata and controls
292 lines (260 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict';
var assert = require('assert');
var util = require('util');
var fs = require('fs');
var vm = require('vm');
var Compiler = require('..').Compiler;
// Fake Angular environment
function makeEnv(mod, catalog) {
return {
angular: {
module: function (modDefined) {
assert.equal(modDefined, mod);
return {
run: function (block) {
assert.equal(block[0], 'gettextCatalog');
return block[1](catalog);
}
};
}
}
};
}
// Fake Angular environment with RequireJS module loader
function makeRequireJsExpliciteEnv(mod, name, module, catalog) {
return {
define: function (expliciteName, modules, callback) {
assert.equal(expliciteName, name);
assert.equal(modules[0], 'angular');
assert.equal(modules[1], module);
var angular = {
module: function (modDefined) {
assert.equal(modDefined, mod);
return {
run: function (block) {
assert.equal(block[0], 'gettextCatalog');
block[1](catalog);
}
};
}
};
callback(angular);
}
};
}
// Fake Angular environment with RequireJS module loader
function makeRequireJsEnv(mod, module, catalog) {
return {
define: function (modules, callback) {
assert.equal(modules[0], 'angular');
assert.equal(modules[1], module);
var angular = {
module: function (modDefined) {
assert.equal(modDefined, mod);
return {
run: function (block) {
assert.equal(block[0], 'gettextCatalog');
block[1](catalog);
}
};
}
};
callback(angular);
}
};
}
function testCompile(filenames, options) {
var compiler = new Compiler(options);
var inputs = filenames.map(function (filename) {
return fs.readFileSync(filename, 'utf8');
});
return compiler.convertPo(inputs);
}
describe('Compile', function () {
it('Compiles a .po file into a .js catalog', function () {
var files = ['test/fixtures/nl.po'];
var output = testCompile(files);
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
assert.equal(language, 'nl');
assert.equal(strings['Hello!'], 'Hallo!');
assert.equal(strings['This is a test'], 'Dit is een test');
assert.deepEqual(strings.Bird, ['Vogel', 'Vogels']);
assert.deepEqual(strings['Hello "world"'], 'Hallo "wereld"');
}
};
var context = vm.createContext(makeEnv('gettext', catalog));
vm.runInContext(output, context);
assert(catalog.called);
});
it('Accepts a module parameter', function () {
var files = ['test/fixtures/nl.po'];
var output = testCompile(files, {
module: 'myApp'
});
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
}
};
var context = vm.createContext(makeEnv('myApp', catalog));
vm.runInContext(output, context);
assert(catalog.called);
});
it('Accepts a defaultLanguage parameter', function () {
var files = ['test/fixtures/nl.po'];
var output = testCompile(files, {
defaultLanguage: 'nl'
});
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
}
};
var context = vm.createContext(makeEnv('gettext', catalog));
vm.runInContext(output, context);
assert(catalog.called);
assert.notEqual(output.indexOf('gettextCatalog.currentLanguage = \'nl\';'), -1);
assert.equal(catalog.currentLanguage, 'nl');
});
it('Accepts a requirejs and modulePath parameter', function () {
var files = ['test/fixtures/nl.po'];
var output = testCompile(files, {
module: 'myApp',
requirejs: true,
requirejsAngular: 'angular',
requirejsModule: './test/fixtures/module'
});
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
}
};
var context = vm.createContext(makeRequireJsEnv('myApp', './test/fixtures/module', catalog));
vm.runInContext(output, context);
assert(catalog.called);
});
it('Accepts a requirejs and name parameter', function () {
var files = ['test/fixtures/nl.po'];
var output = testCompile(files, {
module: 'myApp',
requirejs: true,
requirejsName: 'testName',
requirejsAngular: 'angular',
requirejsModule: './test/fixtures/module'
});
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
}
};
var context = vm.createContext(makeRequireJsExpliciteEnv('myApp', 'testName', './test/fixtures/module', catalog));
vm.runInContext(output, context);
assert(catalog.called);
});
it('Allows merging multiple languages', function () {
var files = ['test/fixtures/nl.po', 'test/fixtures/fr.po'];
var output = testCompile(files);
var languages = 0;
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
assert(language === 'nl' || language === 'fr');
languages++;
}
};
var context = vm.createContext(makeEnv('gettext', catalog));
vm.runInContext(output, context);
assert.equal(languages, 2);
assert(catalog.called);
});
it('Can output to JSON', function () {
var files = ['test/fixtures/nl.po', 'test/fixtures/fr.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.fr, {
'Hello!': 'Bonjour!',
'This is a test': 'Ceci est un test',
'Bird': ['Oiseau', 'Oiseaux']
});
assert.deepEqual(data.nl, {
'Hello!': 'Hallo!',
'This is a test': 'Dit is een test',
'Bird': ['Vogel', 'Vogels'],
'Hello "world"': 'Hallo "wereld"'
});
});
it('Ignores empty strings', function () {
var files = ['test/fixtures/empty.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.nl, {
'This is a test': 'Dit is een test'
});
});
it('Ignores fuzzy strings', function () {
var files = ['test/fixtures/fuzzy.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.nl, {
'This is a test': 'Dit is een test'
});
});
it('Can output multiple inputs to single JSON', function () {
var files = ['test/fixtures/fr.po', 'test/fixtures/depth/fr.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.fr, {
'Hello!': 'Bonjour!',
'This is a test': 'Ceci est un test',
'Bird': ['Oiseau', 'Oiseaux'],
'Goodbye!': 'Au revoir!'
});
});
it('Can handle contexts', function () {
var files = ['test/fixtures/context.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.nl, {
'Hello!': {
'$$noContext': 'Hallo!',
'male': 'Hallo (male)!'
},
'Goodbye': 'Vaarwel',
'Ciao': { female: 'Ciao (female)' }
});
});
it('Does not compile obsolete strings', function () {
var files = ['test/fixtures/obsolete.po'];
var output = testCompile(files);
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
assert.equal(language, 'nl');
assert.equal(strings['Hello!'], 'Hallo!');
assert.equal(strings['Hello "world"'], undefined);
}
};
var context = vm.createContext(makeEnv('gettext', catalog));
vm.runInContext(output, context);
assert(catalog.called);
});
});