test_formatValue.html (2078B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test Localization.prototype.formatValue 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 value works. 38 let val = await loc.formatValue("key1"); 39 is(val, "Value"); 40 } 41 42 { 43 // Value with a string argument works. 44 let val = await loc.formatValue("key2", { user: "John" }); 45 is(val, "Value John"); 46 } 47 48 { 49 // Value with a number argument works. 50 let val = await loc.formatValue("key3", { count: -3.21 }); 51 is(val, "Value -3.21"); 52 } 53 54 { 55 // Verify that in automation, a missing 56 // argument causes an exception. 57 try { 58 let val = await loc.formatValue("key3"); 59 ok(false, "Missing argument didn't cause an exception."); 60 } catch (e) { 61 is(e.message, 62 "[fluent][resolver] errors in en-US/key3: Resolver error: Unknown variable: $count", 63 "Missing key causes an exception."); 64 } 65 } 66 67 { 68 // Incorrect argument type works. 69 // Due to how WebIDL handles union types, it'll convert 70 // the argument to a string `[object Object]`. 71 let val = await loc.formatValue("key2", { user: { name: true } }); 72 is(val, "Value [object Object]"); 73 } 74 75 SimpleTest.finish(); 76 })(); 77 </script> 78 </head> 79 <body> 80 </body> 81 </html>