2013-12-17 19:35:40 +04:00
|
|
|
var assert = require('assert');
|
|
|
|
var fs = require('fs');
|
|
|
|
var PO = require('..');
|
|
|
|
|
|
|
|
function assertHasLine(str, line) {
|
2014-06-19 16:16:30 +04:00
|
|
|
var lines = str.split('\n');
|
2013-12-17 19:35:40 +04:00
|
|
|
var found = false;
|
|
|
|
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
if (lines[i].trim() === line) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 16:16:30 +04:00
|
|
|
assert(found, 'Could not find line: ' + line);
|
2013-12-17 19:35:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Write', function () {
|
|
|
|
it('write flags', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
2014-06-19 16:16:30 +04:00
|
|
|
assertHasLine(str, '#, fuzzy');
|
2013-12-17 19:35:40 +04:00
|
|
|
});
|
2013-12-18 19:46:09 +04:00
|
|
|
|
|
|
|
it('write msgid', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
2014-06-19 16:16:30 +04:00
|
|
|
assertHasLine(str, 'msgid "Sources"');
|
2013-12-18 19:46:09 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('write msgstr', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
2014-06-19 16:16:30 +04:00
|
|
|
assertHasLine(str, 'msgstr "Source"');
|
2013-12-18 19:46:09 +04:00
|
|
|
});
|
2014-01-21 18:06:11 +04:00
|
|
|
|
2014-03-02 02:21:18 +04:00
|
|
|
it('write translator comment', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
2014-06-19 16:16:30 +04:00
|
|
|
assertHasLine(str, '# Translator comment');
|
2014-03-02 22:11:37 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('write extracted comment', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
|
|
|
assertHasLine(str, '#. Extracted comment');
|
2014-03-02 02:21:18 +04:00
|
|
|
});
|
|
|
|
|
2014-03-07 13:15:50 +04:00
|
|
|
it('write obsolete items', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/commented.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
|
|
|
|
|
|
|
assertHasLine(str, '#~ msgid "Add order"');
|
|
|
|
assertHasLine(str, '#~ msgstr "Order toevoegen"');
|
|
|
|
});
|
|
|
|
|
2014-06-19 15:38:07 +04:00
|
|
|
it('write obsolete items with comment', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/commented.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
|
|
|
|
|
|
|
//this is what msgcat tool of gettext does: don't print #~ for comments
|
|
|
|
assertHasLine(str, '# commented obsolete item');
|
|
|
|
assertHasLine(str, '#, fuzzy');
|
|
|
|
|
|
|
|
//items made obsolete by commenting every keyword with #~
|
|
|
|
assertHasLine(str, '#~ msgid "Commented item"');
|
|
|
|
assertHasLine(str, '#~ msgstr "not sure"');
|
|
|
|
assertHasLine(str, '#~ msgid "Second commented item"');
|
|
|
|
assertHasLine(str, '#~ msgstr "also not sure"');
|
|
|
|
});
|
|
|
|
|
2014-03-05 13:13:30 +04:00
|
|
|
describe('C-Strings', function () {
|
|
|
|
it('should escape "', function () {
|
|
|
|
var item = new PO.Item();
|
|
|
|
|
|
|
|
item.msgid = '" should be written escaped';
|
|
|
|
assertHasLine(item.toString(), 'msgid "\\" should be written escaped"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shoudl escape \\', function () {
|
|
|
|
var item = new PO.Item();
|
|
|
|
|
|
|
|
item.msgid = '\\ should be written escaped';
|
|
|
|
assertHasLine(item.toString(), 'msgid "\\\\ should be written escaped"');
|
|
|
|
});
|
2014-03-05 14:37:48 +04:00
|
|
|
|
2014-06-23 14:17:40 +04:00
|
|
|
it('should escape \\n', function () {
|
|
|
|
var item = new PO.Item();
|
|
|
|
|
|
|
|
item.msgid = '\n should be written escaped';
|
|
|
|
assertHasLine(item.toString(), 'msgid ""');
|
2014-06-23 14:52:10 +04:00
|
|
|
assertHasLine(item.toString(), '"\\n"');
|
2014-06-23 14:17:40 +04:00
|
|
|
assertHasLine(item.toString(), '" should be written escaped"');
|
|
|
|
});
|
|
|
|
|
2014-03-05 14:37:48 +04:00
|
|
|
it('should write identical file after parsing a file', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/c-strings.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
|
|
|
|
|
|
|
assert.equal(str, input);
|
|
|
|
});
|
2014-03-05 13:13:30 +04:00
|
|
|
});
|
|
|
|
|
2014-01-22 17:10:15 +04:00
|
|
|
describe('msgctxt', function () {
|
|
|
|
it('should write context field to file', function () {
|
|
|
|
var input = fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8');
|
|
|
|
var po = PO.parse(input);
|
|
|
|
var str = po.toString();
|
|
|
|
assertHasLine(str, 'msgctxt "folder action"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore omitted context field', function () {
|
|
|
|
var po = new PO();
|
|
|
|
var item = new PO.Item();
|
|
|
|
po.items.push(item);
|
|
|
|
assert.ok(po.toString().indexOf('msgctxt') < 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write empty context field', function () {
|
|
|
|
var po = new PO();
|
|
|
|
var item = new PO.Item();
|
|
|
|
|
|
|
|
item.msgctxt = '';
|
|
|
|
po.items.push(item);
|
|
|
|
assert.ok(po.toString().indexOf('msgctxt') >= 0);
|
|
|
|
});
|
2014-01-21 18:06:11 +04:00
|
|
|
});
|
2013-12-17 19:35:40 +04:00
|
|
|
});
|