forked from Pirate-Weather/translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
141 lines (117 loc) · 4.5 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
135
136
137
138
139
140
141
var assert = require("assert"),
fs = require("fs"),
path = require("path"),
template = require("./lib/template"),
translation = require("./"),
util = require("util");
describe("translation", function() {
describe("template", function() {
var convert = template({
"foo": "bar",
"bar": "meeple $2",
"baz": function(a, b) { return "meeple " + b; },
"quux": function() { return "glorple"; }
});
it("should return a number in string form", function() {
assert.strictEqual(convert(42), "42");
});
it("should throw an error given an unrecognized string", function() {
assert.throws(function() { convert("42"); });
});
it("should apply an expected value conversion", function() {
assert.strictEqual(convert("foo"), "bar");
});
it("should throw an error given a value that's expected to be a string template", function() {
assert.throws(function() { convert("bar"); });
});
it("should throw an error given a value that's expected to be a function template", function() {
assert.throws(function() { convert("baz"); });
});
it("should throw an error given an empty array", function() {
assert.throws(function() { convert([]); });
});
it("should apply a string template", function() {
assert.strictEqual(convert(["bar", 10, 20]), "meeple 20");
});
it("should fail to apply a function template given the wrong number of arguments", function() {
assert.throws(function() { convert(["baz", 10, 20, 30]); });
});
it("should apply a function template", function() {
assert.strictEqual(convert(["baz", 10, 20]), "meeple 20");
});
it("should recursively apply function templates", function() {
/* Actually, a "meeple meeple bar" sounds like it'd be a pretty tasty
* candy treat. */
assert.strictEqual(convert(["bar", 10, ["baz", 20, "foo"]]), "meeple meeple bar");
});
it("should throw an error given undefined", function() {
assert.throws(function() { convert(undefined); });
});
it("should throw an error given null", function() {
assert.throws(function() { convert(null); });
});
it("should throw an error given an object", function() {
assert.throws(function() { convert({}); });
});
it("should apply an expected value conversion given a zero-argument function", function() {
assert.strictEqual(convert("quux"), "glorple");
});
it("should fail to apply a zero-argument function given arguments", function() {
assert.throws(function() { convert(["quux"]); });
});
it("should fail to apply a function template given a value", function() {
assert.throws(function() { convert("baz"); });
});
it("should provide context to functions", function() {
var convert = template({
"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?";
}
});
convert(["foo", "bar", ["baz", "quux"], ["neem", ["glorp", 42]]]);
});
});
describe("language", function() {
fs.readdirSync(path.join(__dirname, "test_cases")).forEach(function(lang) {
if(lang.charAt(0) === ".")
return;
var name = path.basename(lang, ".json"),
translate = translation[name];
describe(name, function() {
var cases = JSON.parse(
fs.readFileSync(path.join(__dirname, "test_cases", lang), "utf8")
);
Object.keys(cases).forEach(function(summary) {
var source = cases[summary];
it(
util.format("should translate %j to \"%s\"", source, summary),
function() {
assert.strictEqual(translate(source), summary);
}
);
});
});
});
});
});