Skip to content

Commit 6bf1e0a

Browse files
committed
Add tests
1 parent 96146c1 commit 6bf1e0a

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

fluent/test/bomb_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ suite('Reference bombs', function() {
3535
const val = bundle.formatPattern(msg.value, args, errs);
3636
assert.strictEqual(val, '{???}');
3737
assert.strictEqual(errs.length, 1);
38+
assert.ok(errs[0] instanceof RangeError);
39+
});
40+
41+
test('throws when errors are undefined', function() {
42+
const msg = bundle.getMessage('lolz');
43+
assert.throws(
44+
() => bundle.formatPattern(msg.value),
45+
RangeError,
46+
"Too many characters in placeable"
47+
);
3848
});
3949
});
4050
});

fluent/test/errors_test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"use strict";
2+
3+
import assert from "assert";
4+
import ftl from "@fluent/dedent";
5+
6+
import FluentBundle from "../src/bundle";
7+
8+
suite("Errors", function() {
9+
let bundle, errs;
10+
setup(function() {
11+
errs = [];
12+
});
13+
14+
suite("Reporting into an array", function(){
15+
suiteSetup(function() {
16+
bundle = new FluentBundle("en-US", { useIsolating: false });
17+
bundle.addMessages(ftl`
18+
foo = {$one} and {$two}
19+
`);
20+
});
21+
22+
test("Two errors are reported", function() {
23+
let msg = bundle.getMessage("foo");
24+
let val = bundle.formatPattern(msg.value, {}, errs);
25+
assert.strictEqual(val, "{$one} and {$two}");
26+
assert.strictEqual(errs.length, 2);
27+
assert.ok(errs[0] instanceof ReferenceError);
28+
assert.ok(errs[1] instanceof ReferenceError);
29+
});
30+
31+
test("Two calls", function() {
32+
let msg = bundle.getMessage("foo");
33+
let val;
34+
35+
val = bundle.formatPattern(msg.value, {}, errs);
36+
assert.strictEqual(val, "{$one} and {$two}");
37+
assert.strictEqual(errs.length, 2);
38+
assert.ok(errs[0] instanceof ReferenceError);
39+
assert.ok(errs[1] instanceof ReferenceError);
40+
41+
val = bundle.formatPattern(msg.value, {}, errs);
42+
assert.strictEqual(val, "{$one} and {$two}");
43+
assert.strictEqual(errs.length, 4);
44+
assert.ok(errs[0] instanceof ReferenceError);
45+
assert.ok(errs[1] instanceof ReferenceError);
46+
assert.ok(errs[2] instanceof ReferenceError);
47+
assert.ok(errs[3] instanceof ReferenceError);
48+
});
49+
50+
test("Non-empty errors array", function() {
51+
errs = ["Something"];
52+
let msg = bundle.getMessage("foo");
53+
let val = bundle.formatPattern(msg.value, {}, errs);
54+
55+
assert.strictEqual(val, "{$one} and {$two}");
56+
assert.strictEqual(errs.length, 3);
57+
assert.strictEqual(errs[0], "Something");
58+
assert.ok(errs[1] instanceof ReferenceError);
59+
assert.ok(errs[2] instanceof ReferenceError);
60+
});
61+
});
62+
63+
suite("Throwing", function(){
64+
suiteSetup(function() {
65+
bundle = new FluentBundle("en-US", { useIsolating: false });
66+
bundle.addMessages(ftl`
67+
foo = {$one} and {$two}
68+
`);
69+
});
70+
71+
test("First error is thrown", function() {
72+
let msg = bundle.getMessage("foo");
73+
assert.throws(
74+
() => bundle.formatPattern(msg.value, {}),
75+
ReferenceError,
76+
"Unknown variable: $one"
77+
);
78+
});
79+
80+
test("Two calls", function() {
81+
let msg = bundle.getMessage("foo");
82+
83+
assert.throws(
84+
() => bundle.formatPattern(msg.value, {}),
85+
ReferenceError,
86+
"Unknown variable: $one"
87+
);
88+
89+
assert.throws(
90+
() => bundle.formatPattern(msg.value, {}),
91+
ReferenceError,
92+
"Unknown variable: $one"
93+
);
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)