This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
test.js
134 lines (112 loc) · 3.96 KB
/
test.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use strict";
const assert = require("assert");
const Translation = require("./lib/translation");
const translations = require("./");
describe("translations", () => {
describe("Translation", () => {
const test = new Translation({
"foo": "bar",
"bar": "meeple $2",
"baz": (a, b) => "meeple " + b,
"quux": () => "glorple",
});
it("should return a number in string form", () => {
assert.strictEqual(test.translate(42), "42");
});
it("should throw an error given an unrecognized string", () => {
assert.throws(() => { test.translate("42"); });
});
it("should apply an expected value conversion", () => {
assert.strictEqual(test.translate("foo"), "bar");
});
it("should throw an error given a value expected to be a string", () => {
assert.throws(() => { test.translate("bar"); });
});
it("should throw an error given a value expected to be a function", () => {
assert.throws(() => { test.translate("baz"); });
});
it("should throw an error given an empty array", () => {
assert.throws(() => { test.translate([]); });
});
it("should apply a string template", () => {
assert.strictEqual(test.translate(["bar", 10, 20]), "meeple 20");
});
it("should fail to apply a function with the wrong arity", () => {
assert.throws(() => { test.translate(["baz", 10, 20, 30]); });
});
it("should apply a function template", () => {
assert.strictEqual(test.translate(["baz", 10, 20]), "meeple 20");
});
it("should recursively apply function templates", () => {
// Actually, a "meeple meeple bar" sounds like it'd be a pretty tasty
// candy treat.
assert.strictEqual(
test.translate(["bar", 10, ["baz", 20, "foo"]]),
"meeple meeple bar"
);
});
it("should throw an error given undefined", () => {
assert.throws(() => { test.translate(undefined); });
});
it("should throw an error given null", () => {
assert.throws(() => { test.translate(null); });
});
it("should throw an error given an object", () => {
assert.throws(() => { test.translate({}); });
});
it("should apply a zero-argument function", () => {
assert.strictEqual(test.translate("quux"), "glorple");
});
it("should fail to apply a zero-argument function given arguments", () => {
assert.throws(() => { test.translate(["quux"]); });
});
it("should fail to apply a function template given a value", () => {
assert.throws(() => { test.translate("baz"); });
});
it("should provide context to functions", () => {
const test = new Translation({
"foo": function(a, b, c) {
assert.deepEqual(this, ["foo"]);
return "Moop.";
},
"bar": function() {
assert.deepEqual(this, ["foo", "bar"]);
return "Boop.";
},
"baz": function(a) {
assert.deepEqual(this, ["foo", "baz"]);
return "Soup.";
},
"quux": function() {
assert.deepEqual(this, ["foo", "baz", "quux"]);
return "Floop.";
},
"neem": function(a) {
assert.deepEqual(this, ["foo", "neem"]);
return "Bloop.";
},
"glorp": function(a) {
assert.deepEqual(this, ["foo", "neem", "glorp"]);
return "Rope?";
},
});
test.translate(["foo", "bar", ["baz", "quux"], ["neem", ["glorp", 42]]]);
});
});
describe("languages", () => {
for(const lang in translations) {
describe(lang, () => {
const translation = translations[lang];
const cases = require("./test_cases/" + lang);
for(const summary in cases) {
const source = cases[summary];
it(
"should translate " + JSON.stringify(source) +
" to " + JSON.stringify(summary),
() => assert.strictEqual(translation.translate(source), summary)
);
}
});
}
});
});