mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 13:21:05 +00:00
Bug 1560342 - Add basic tests for Localization. r=stas
Differential Revision: https://phabricator.services.mozilla.com/D35587 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
224a1bb5c4
commit
738b080795
@ -33,6 +33,7 @@ LOCAL_INCLUDES += [
|
||||
]
|
||||
|
||||
XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini']
|
||||
MOCHITEST_CHROME_MANIFESTS += ['test/mochitest/chrome.ini']
|
||||
|
||||
SPHINX_TREES['l10n'] = 'docs'
|
||||
|
||||
|
3
intl/l10n/test/mochitest/chrome.ini
Normal file
3
intl/l10n/test/mochitest/chrome.ini
Normal file
@ -0,0 +1,3 @@
|
||||
[localization/test_formatValue.html]
|
||||
[localization/test_formatValues.html]
|
||||
[localization/test_formatMessages.html]
|
@ -0,0 +1,87 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test Localization.prototype.formatMessages API</title>
|
||||
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
const { FluentBundle } =
|
||||
ChromeUtils.import("resource://gre/modules/Fluent.jsm");
|
||||
|
||||
async function* mockGenerateMessages(locales, resourceIds) {
|
||||
const bundle = new FluentBundle(locales, {
|
||||
useIsolating: false,
|
||||
});
|
||||
bundle.addMessages(`
|
||||
key1 = Value
|
||||
.title = Title 1
|
||||
.accesskey = K
|
||||
key2 =
|
||||
.label = This is a label for { $user }
|
||||
`);
|
||||
yield bundle;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const loc = new Localization(
|
||||
['mock.ftl'],
|
||||
mockGenerateMessages,
|
||||
);
|
||||
|
||||
{
|
||||
// Simple mix works.
|
||||
let msgs = await loc.formatMessages([
|
||||
{id: "key1"},
|
||||
{id: "key2", args: { user: "Amy"}},
|
||||
]);
|
||||
is(msgs[0].value, "Value");
|
||||
is(msgs[0].attributes[0].name, "title");
|
||||
is(msgs[0].attributes[0].value, "Title 1");
|
||||
is(msgs[0].attributes[1].name, "accesskey");
|
||||
is(msgs[0].attributes[1].value, "K");
|
||||
|
||||
is(msgs[1].value, null);
|
||||
is(msgs[1].attributes[0].name, "label");
|
||||
is(msgs[1].attributes[0].value, "This is a label for Amy");
|
||||
}
|
||||
|
||||
{
|
||||
// Missing arguments cause exception in automation.
|
||||
try {
|
||||
let msgs = await loc.formatMessages([
|
||||
{id: "key1"},
|
||||
{id: "key2"},
|
||||
]);
|
||||
ok(false, "Missing argument didn't cause an exception.");
|
||||
} catch (e) {
|
||||
is(e,
|
||||
"[fluent][resolver] errors in mock.ftl/key2: ReferenceError: Unknown variable: user.",
|
||||
"Missing key causes an exception.");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Missing keys cause exception in automation.
|
||||
try {
|
||||
let msgs = await loc.formatMessages([
|
||||
{ id: "key1" },
|
||||
{ id: "key4" },
|
||||
]);
|
||||
ok(false, "Missing key didn't cause an exception.");
|
||||
} catch (e) {
|
||||
is(e,
|
||||
"[fluent] Missing translations in mock.ftl: key4.",
|
||||
"Missing key causes an exception.");
|
||||
}
|
||||
}
|
||||
SimpleTest.finish();
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
78
intl/l10n/test/mochitest/localization/test_formatValue.html
Normal file
78
intl/l10n/test/mochitest/localization/test_formatValue.html
Normal file
@ -0,0 +1,78 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test Localization.prototype.formatValue API</title>
|
||||
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
const { FluentBundle } =
|
||||
ChromeUtils.import("resource://gre/modules/Fluent.jsm");
|
||||
|
||||
async function* mockGenerateMessages(locales, resourceIds) {
|
||||
const bundle = new FluentBundle(locales, {
|
||||
useIsolating: false,
|
||||
});
|
||||
bundle.addMessages(`
|
||||
key1 = Value
|
||||
key2 = Value { $user }
|
||||
key3 = Value { $count }
|
||||
`);
|
||||
yield bundle;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const loc = new Localization(
|
||||
['mock.ftl'],
|
||||
mockGenerateMessages,
|
||||
);
|
||||
|
||||
{
|
||||
// Simple value works.
|
||||
let val = await loc.formatValue("key1");
|
||||
is(val, "Value");
|
||||
}
|
||||
|
||||
{
|
||||
// Value with a string argument works.
|
||||
let val = await loc.formatValue("key2", { user: "John" });
|
||||
is(val, "Value John");
|
||||
}
|
||||
|
||||
{
|
||||
// Value with a number argument works.
|
||||
let val = await loc.formatValue("key3", { count: -3.21 });
|
||||
is(val, "Value -3.21");
|
||||
}
|
||||
|
||||
{
|
||||
// Verify that in automation, a missing
|
||||
// argument causes an exception.
|
||||
try {
|
||||
let val = await loc.formatValue("key3");
|
||||
ok(false, "Missing argument didn't cause an exception.");
|
||||
} catch (e) {
|
||||
is(e,
|
||||
"[fluent][resolver] errors in mock.ftl/key3: ReferenceError: Unknown variable: count.",
|
||||
"Missing key causes an exception.");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Incorrect argument type works.
|
||||
// Due to how WebIDL handles union types, it'll convert
|
||||
// the argument to a string `[object Object]`.
|
||||
let val = await loc.formatValue("key2", { user: { name: true } });
|
||||
is(val, "Value [object Object]");
|
||||
}
|
||||
|
||||
SimpleTest.finish();
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
83
intl/l10n/test/mochitest/localization/test_formatValues.html
Normal file
83
intl/l10n/test/mochitest/localization/test_formatValues.html
Normal file
@ -0,0 +1,83 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test Localization.prototype.formatValues API</title>
|
||||
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
const { FluentBundle } =
|
||||
ChromeUtils.import("resource://gre/modules/Fluent.jsm");
|
||||
|
||||
async function* mockGenerateMessages(locales, resourceIds) {
|
||||
const bundle = new FluentBundle(locales,
|
||||
{
|
||||
useIsolating: false,
|
||||
});
|
||||
bundle.addMessages(`
|
||||
key1 = Value
|
||||
key2 = Value { $user }
|
||||
key3 = Value { $count }
|
||||
`);
|
||||
yield bundle;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const loc = new Localization(
|
||||
['mock.ftl'],
|
||||
mockGenerateMessages,
|
||||
);
|
||||
|
||||
{
|
||||
// Simple mix works.
|
||||
let vals = await loc.formatValues([
|
||||
{id: "key1"},
|
||||
{id: "key2", args: { user: "Amy"}},
|
||||
{id: "key3", args: { count: -32.12 }},
|
||||
]);
|
||||
is(vals[0], "Value");
|
||||
is(vals[1], "Value Amy");
|
||||
is(vals[2], "Value -32.12");
|
||||
}
|
||||
|
||||
{
|
||||
// Missing arguments cause exception in automation.
|
||||
try {
|
||||
let vals = await loc.formatValues([
|
||||
{id: "key1"},
|
||||
{id: "key2"},
|
||||
{id: "key3", args: { count: -32.12 }},
|
||||
]);
|
||||
ok(false, "Missing argument didn't cause an exception.");
|
||||
} catch (e) {
|
||||
is(e,
|
||||
"[fluent][resolver] errors in mock.ftl/key2: ReferenceError: Unknown variable: user.",
|
||||
"Missing key causes an exception.");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Missing keys cause exception in automation.
|
||||
try {
|
||||
let vals = await loc.formatValues([
|
||||
{ id: "key1" },
|
||||
{ id: "key4", args: { count: -32.12 } },
|
||||
]);
|
||||
ok(false, "Missing key didn't cause an exception.");
|
||||
} catch (e) {
|
||||
is(e,
|
||||
"[fluent] Missing translations in mock.ftl: key4.",
|
||||
"Missing key causes an exception.");
|
||||
}
|
||||
}
|
||||
|
||||
SimpleTest.finish();
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user