test_formatValues.html (2180B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test Localization.prototype.formatValues API</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 8 <script type="application/javascript"> 9 "use strict"; 10 const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [ 11 { 12 path: "/localization/en-US/mock.ftl", 13 source: ` 14 key1 = Value 15 key2 = Value { $user } 16 key3 = Value { $count } 17 ` 18 } 19 ]); 20 let registry = new L10nRegistry({ 21 bundleOptions: { 22 useIsolating: false 23 } 24 }); 25 registry.registerSources([mockSource]); 26 27 (async () => { 28 SimpleTest.waitForExplicitFinish(); 29 30 const loc = new Localization( 31 ['mock.ftl'], 32 false, 33 registry, 34 ); 35 36 { 37 // Simple mix works. 38 let vals = await loc.formatValues([ 39 {id: "key1"}, 40 {id: "key2", args: { user: "Amy"}}, 41 {id: "key3", args: { count: -32.12 }}, 42 ]); 43 is(vals[0], "Value"); 44 is(vals[1], "Value Amy"); 45 is(vals[2], "Value -32.12"); 46 } 47 48 { 49 // Missing arguments cause exception in automation. 50 try { 51 let vals = await loc.formatValues([ 52 {id: "key1"}, 53 {id: "key2"}, 54 {id: "key3", args: { count: -32.12 }}, 55 ]); 56 ok(false, "Missing argument didn't cause an exception."); 57 } catch (e) { 58 is(e.message, 59 "[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user", 60 "Missing key causes an exception."); 61 } 62 } 63 64 { 65 // Missing keys cause exception in automation. 66 try { 67 let vals = await loc.formatValues([ 68 { id: "key1" }, 69 { id: "key4", args: { count: -32.12 } }, 70 ]); 71 ok(false, "Missing key didn't cause an exception."); 72 } catch (e) { 73 is(e.message, 74 "[fluent] Missing message in locale en-US: key4", 75 "Missing key causes an exception."); 76 } 77 } 78 79 SimpleTest.finish(); 80 })(); 81 </script> 82 </head> 83 <body> 84 </body> 85 </html>