From c9253f6e37acf78f9fb85ff6f220bb4548459127 Mon Sep 17 00:00:00 2001 From: Austin King <shout@ozten.com> Date: Thu, 19 Jan 2012 13:59:16 -0800 Subject: [PATCH] Adding unit tests for i18n.format --- lib/i18n.js | 1 + resources/static/shared/gettext.js | 2 +- tests/i18n-tests.js | 59 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100755 tests/i18n-tests.js diff --git a/lib/i18n.js b/lib/i18n.js index cfe804c6b..cb7979938 100644 --- a/lib/i18n.js +++ b/lib/i18n.js @@ -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 { diff --git a/resources/static/shared/gettext.js b/resources/static/shared/gettext.js index 46cca99b1..dab6ac4f1 100644 --- a/resources/static/shared/gettext.js +++ b/resources/static/shared/gettext.js @@ -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) { diff --git a/tests/i18n-tests.js b/tests/i18n-tests.js new file mode 100755 index 000000000..66c5e4bf5 --- /dev/null +++ b/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); -- GitLab