summaryrefslogtreecommitdiffstats
path: root/intl/l10n/test/mochitest/localization/test_formatValue.html
blob: e1ee02fa7a9fbeaadaf0a338ac74dfd3701f6178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!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 mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [
    {
      path: "/localization/en-US/mock.ftl",
      source: `
key1 = Value
key2 = Value { $user }
key3 = Value { $count }
`
    }
  ]);
  let registry = new L10nRegistry({
    bundleOptions: {
      useIsolating: false
    }
  });
  registry.registerSources([mockSource]);

  (async () => {
    SimpleTest.waitForExplicitFinish();

    const loc = new Localization(
      ['mock.ftl'],
      false,
      registry,
    );

    {
      // 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.message,
          "[fluent][resolver] errors in en-US/key3: Resolver error: 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>