forked from Pirate-Weather/translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
141 lines (117 loc) · 4.55 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 expect = require("chai").expect,
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() {
expect(convert(42)).to.equal("42");
});
it("should throw an error given an unrecognized string", function() {
expect(function() { convert("42"); }).to.throw();
});
it("should apply an expected value conversion", function() {
expect(convert("foo")).to.equal("bar");
});
it("should throw an error given a value that's expected to be a string template", function() {
expect(function() { convert("bar"); }).to.throw();
});
it("should throw an error given a value that's expected to be a function template", function() {
expect(function() { convert("baz"); }).to.throw();
});
it("should throw an error given an empty array", function() {
expect(function() { convert([]); }).to.throw();
});
it("should apply a string template", function() {
expect(convert(["bar", 10, 20])).to.equal("meeple 20");
});
it("should fail to apply a function template given the wrong number of arguments", function() {
expect(function() { convert(["baz", 10, 20, 30]); }).to.throw();
});
it("should apply a function template", function() {
expect(convert(["baz", 10, 20])).to.equal("meeple 20");
});
it("should recursively apply function templates", function() {
/* Actually, a "meeple meeple bar" sounds like it'd be a pretty tasty
* candy treat. */
expect(convert(["bar", 10, ["baz", 20, "foo"]])).to.equal("meeple meeple bar");
});
it("should throw an error given undefined", function() {
expect(function() { convert(undefined); }).to.throw();
});
it("should throw an error given null", function() {
expect(function() { convert(null); }).to.throw();
});
it("should throw an error given an object", function() {
expect(function() { convert({}); }).to.throw();
});
it("should apply an expected value conversion given a zero-argument function", function() {
expect(convert("quux")).to.equal("glorple");
});
it("should fail to apply a zero-argument function given arguments", function() {
expect(function() { convert(["quux"]); }).to.throw();
});
it("should fail to apply a function template given a value", function() {
expect(function() { convert("baz"); }).to.throw();
});
it("should provide context to functions", function() {
var convert = template({
"foo": function(a, b, c) {
expect(this).to.deep.equal(["foo"]);
return "Moop.";
},
"bar": function() {
expect(this).to.deep.equal(["foo", "bar"]);
return "Boop.";
},
"baz": function(a) {
expect(this).to.deep.equal(["foo", "baz"]);
return "Soup.";
},
"quux": function() {
expect(this).to.deep.equal(["foo", "baz", "quux"]);
return "Floop.";
},
"neem": function(a) {
expect(this).to.deep.equal(["foo", "neem"]);
return "Bloop.";
},
"glorp": function(a) {
expect(this).to.deep.equal(["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() {
expect(translate(source)).to.equal(summary);
}
);
});
});
});
});
});