Skip to content

Commit

Permalink
Merge pull request zaach#7 from ozten/join-existing-tests
Browse files Browse the repository at this point in the history
Add tests for --join-existing.
  • Loading branch information
zaach committed Jul 21, 2012
2 parents 188e9c8 + 835da5e commit 715822b
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/header.po
Expand Up @@ -3,6 +3,7 @@
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
Expand Down
14 changes: 11 additions & 3 deletions lib/jsxgettext.js
Expand Up @@ -64,11 +64,19 @@ function gen (sources, options) {
});

for (var str in strings) {
// TODO start and "\n" are ugly, but
// getComment shouldn't break into multiple lines with #:
var start = true;
Object.keys(strings[str].lines).forEach(function (key) {
if (start) {
generated += "#:";
start = false;
}
var ln = strings[str].lines[key];
generated += genComment(key, ln);
});
generated += msgid(str) +
generated += "\n" +
msgid(str) +
"msgstr "+JSON.stringify(strings[str].str) + "\n" +
"\n";
}
Expand All @@ -86,8 +94,8 @@ function genEJS (ejsSources, options) {
}

function genComment (file, additional) {
return "#: " + file +
(additional ? "\n#" + additional.split("\n").join("\n#") : '') + "\n";
return " " + file +
(additional ? "\n#" + additional.split("\n").join("\n#") : '') + "";
}

function msgid (str) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -17,5 +17,8 @@
"optionalDependencies": {},
"engines": {
"node": "*"
},
"scripts": {
"test": "node tests/all.js"
}
}
9 changes: 9 additions & 0 deletions tests/README.md
@@ -0,0 +1,9 @@

## Join Existing tests

The following commands created tests/outputs/messages_first

xgettext -L Perl --output-dir=tests/outputs/ --from-code=utf-8 --output=messages_firstpass.pot tests/inputs/first.js

xgettext -L Perl --output-dir=tests/outputs/ --from-code=utf-8 --output=messages_new.pot tests/inputs/first.js
xgettext -j -L Perl --output-dir=tests/outputs/ --from-code=utf-8 --output=messages_new.pot tests/inputs/second.js
12 changes: 12 additions & 0 deletions tests/all.js
@@ -0,0 +1,12 @@
// TODO figure out why
// exports['test join_existing'] = require('./join_existing');
// doesn't work
exports['test join_existing'] = require('./join_existing')['we gettext from first file'];

if (module == require.main) {
require('test').run(exports);
} else {
console.log("Just a module");
}


2 changes: 2 additions & 0 deletions tests/inputs/first.js
@@ -0,0 +1,2 @@
var msg = gettext('Hello World');
var dup = gettext('This message is used twice.');
1 change: 1 addition & 0 deletions tests/inputs/second.js
@@ -0,0 +1 @@
var dup = gettext('This message is used twice.');
96 changes: 96 additions & 0 deletions tests/join_existing.js
@@ -0,0 +1,96 @@
var
fs = require('fs'),
jsxgettext = require('../lib/jsxgettext'),
path = require('path');

// Tests the --join-existing feature

/*
* We use xgettext on files under inputs and save it's output
* under outputs. These tests run jsxgettext against the
* same inputs and test for identical output.
*/

var sourceFirstPass;

exports['we gettext from first file'] = function (assert, cb) {
// We'll extract strings from inputs/first.js
// This should match outputs/messages_firstpass.js
var inputFilename = path.join(__dirname, 'inputs', 'first.js');
fs.readFile(inputFilename, function (err, source) {
var opts = {},
sources = {'inputs/first.js': source},
result = jsxgettext.generate(sources, 'inputs/first.js', opts);
assert.equal(typeof result, 'string');
assert.ok(result.length > 0);
var outputFilename = path.join(__dirname, 'outputs',
'messages_firstpass.pot');
fs.readFile(outputFilename, function (err, source) {
assert.ok(! err);

var example = source.toString('utf8').split('\n');
var actual = result.split('\n');

for (var i=0; i < example.length; i++) {
// Dynamic lines
if (0 === example[i].indexOf('"POT-Creation-Date')) continue;

// Reproducable lines
assert.ok(i < actual.length,
'Num example output is at least as long as our result');
assert.equal(actual[i], example[i], 'We match line for line');
}
assert.ok((actual.length == example.length ||
actual.length - 1 == example.length),
'Actual and Expected are the same length');
});

sourceFirstPass = source;

// write to filesystem as join-existing will implicitly look for it, but...
// TODO: So jsxgettext does the right thing with or without messages.po
// that seems odd...
fs.writeFileSync('messages.pot', result, "utf8");
//cb();
test2(assert, cb);
});
};

var test2 = function (assert, cb) {
// We'll extract strings from inputs/second.js
// This should match outputs/messages.js
var inputFilename = path.join(__dirname, 'inputs', 'second.js');
fs.readFile(inputFilename, function (err, source) {
var opts = {"join-existing": true},
sources = {'inputs/first.js': sourceFirstPass,
'inputs/second.js': source},
result = jsxgettext.generate(sources, 'inputs/second.js', opts);

assert.equal(typeof result, 'string');
assert.ok(result.length > 0);
var outputFilename = path.join(__dirname, 'outputs',
'messages_secondpass.pot');
fs.readFile(outputFilename, function (err, source) {
assert.ok(! err);

var example = source.toString('utf8').split('\n');
var actual = result.split('\n');

for (var i=0; i < example.length; i++) {
// Dynamic lines
if (0 === example[i].indexOf('"POT-Creation-Date')) continue;
// Reproducable lines
assert.ok(i < actual.length,
'Num example output is at least as long as our result');
assert.equal(actual[i], example[i], 'We match line for line');
}
assert.ok((actual.length == example.length ||
actual.length - 1 == example.length),
'Actual and Expected are the same length');
fs.writeFileSync('messages2.pot', result, "utf8");
});
cb();
});
};

if (module == require.main) require('test').run(exports);
26 changes: 26 additions & 0 deletions tests/outputs/messages_firstpass.pot
@@ -0,0 +1,26 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-07-16 18:17-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: inputs/first.js:1
msgid "Hello World"
msgstr ""

#: inputs/first.js:2
msgid "This message is used twice."
msgstr ""
26 changes: 26 additions & 0 deletions tests/outputs/messages_secondpass.pot
@@ -0,0 +1,26 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-07-17 17:39-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: inputs/first.js:1
msgid "Hello World"
msgstr ""

#: inputs/first.js:2 inputs/second.js:1
msgid "This message is used twice."
msgstr ""

0 comments on commit 715822b

Please sign in to comment.