Skip to content

Commit

Permalink
Adding unit tests for i18n.format
Browse files Browse the repository at this point in the history
  • Loading branch information
ozten committed Jan 19, 2012
1 parent 5df3862 commit c9253f6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/i18n.js
Expand Up @@ -189,6 +189,7 @@ exports.localeFrom = localeFrom = function (language) {
* format("%s %s", ["Hello", "World"]);
*/
exports.format = format = function (fmt, obj, named) {
if (! fmt) return "";
if (named) {
return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
} else {
Expand Down
2 changes: 1 addition & 1 deletion resources/static/shared/gettext.js
Expand Up @@ -16,8 +16,8 @@ function Gettext(params) {
},
// See lib/i18n.js format docs
format: function (fmt, obj, named) {
if (! fmt) return "";
if (! fmt.replace) {
console.log("format called with", fmt);
return fmt;
}
if (named) {
Expand Down
59 changes: 59 additions & 0 deletions tests/i18n-tests.js
@@ -0,0 +1,59 @@
#!/usr/bin/env node

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

require('./lib/test_env.js');

const assert = require('assert'),
vows = require('vows'),
i18n = require('../lib/i18n');

var suite = vows.describe('i18n');

suite.addBatch({
"format a string with place values": {
topic: function () {
return i18n.format("%s %s!", ["Hello", "World"]);
},
"was interpolated": function (err, str) {
assert.equal(str, "Hello World!");
}
}
});

suite.addBatch({
"format a string with named values": {
topic: function () {
var params = { salutation: "Hello", place: "World" };
return i18n.format("%(salutation)s %(place)s!", params, true);
},
"was interpolated": function (err, str) {
assert.equal(str, "Hello World!");
}
}
});

suite.addBatch({
"format a string without interpolation": {
topic: function () {
return i18n.format("Hello World!");
},
"was interpolated": function (err, str) {
assert.equal(str, "Hello World!");
}
},
"format a null": {
topic: function () {
return i18n.format(null);
},
"was interpolated": function (err, str) {
assert.equal(str, "");
}
}
});

// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);

0 comments on commit c9253f6

Please sign in to comment.