-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
213 lines (167 loc) · 5.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var test = require('ava');
var Configstore = require('configstore');
var languages = require('./languages.js');
var translate = require('./index.js');
const config = new Configstore('google-translate-api');
test.beforeEach(() => {
config.clear();
});
test('translate without any options', async t => {
const res = await translate('vertaler');
t.is(res.text, 'translator');
t.false(res.from.language.didYouMean);
t.is(res.from.language.iso, 'nl');
t.false(res.from.text.autoCorrected);
t.is(res.from.text.value, '');
t.false(res.from.text.didYouMean);
});
test('translate from auto to dutch', async t => {
const res = await translate('translator', {from: 'auto', to: 'nl'});
t.is(res.text, 'vertaler');
t.false(res.from.language.didYouMean);
t.is(res.from.language.iso, 'en');
t.false(res.from.text.autoCorrected);
t.is(res.from.text.value, '');
t.false(res.from.text.didYouMean);
});
test('test pronunciation', async t => {
const res = await translate('translator', {from: 'auto', to: 'zh-CN'});
t.is(res.pronunciation, 'Yì zhě');
});
test('translate some english text setting the source language as portuguese', async t => {
const res = await translate('happy', {from: 'pt', to: 'nl'});
t.true(res.from.language.didYouMean);
t.is(res.from.language.iso, 'en');
});
test('translate some misspelled english text to dutch', async t => {
const res = await translate('I spea Dutch', {from: 'en', to: 'nl'});
if (res.from.text.autoCorrected || res.from.text.didYouMean) {
t.is(res.from.text.value, 'I [speak] Dutch');
} else {
t.fail();
}
});
test('translate some text and get the raw output alongside', async t => {
const res = await translate('vertaler', {raw: true});
t.truthy(res.raw);
});
test('test a supported language – by code', t => {
t.true(languages.isSupported('en'));
});
test('test an unsupported language – by code', t => {
t.false(languages.isSupported('js'));
});
test('test a supported language – by name', t => {
t.true(languages.isSupported('english'));
});
test('test an unsupported language – by name', t => {
t.false(languages.isSupported('javascript'));
});
test('get a language code by its name', t => {
t.is(languages.getCode('english'), 'en');
});
test('get an unsupported language code by its name', t => {
t.false(languages.getCode('javascript'));
});
test('get a supported language code by code', t => {
t.is(languages.getCode('en'), 'en');
});
test('call getCode with \'undefined\'', t => {
t.is(languages.getCode(undefined), false);
});
test('call getCode with \'null\'', t => {
t.is(languages.getCode(null), false);
});
test('call getCode with an empty string', t => {
t.is(languages.getCode(''), false);
});
test('call getCode with no arguments', t => {
t.is(languages.getCode(), false);
});
test('try to translate from an unsupported language', async t => {
try {
await translate('something', {from: 'js', to: 'en'});
t.fail();
} catch (err) {
t.is(err.code, 400);
t.is(err.message, 'The language \'js\' is not supported');
}
});
test('try to translate to an unsupported language', async t => {
try {
await translate('something', {from: 'en', to: 'js'});
t.fail();
} catch (err) {
t.is(err.code, 400);
t.is(err.message, 'The language \'js\' is not supported');
}
});
test('translate from dutch to english using language names instead of codes', async t => {
const res = await translate('iets', {from: 'dutch', to: 'english'});
t.is(res.from.language.iso, 'nl');
t.is(res.text, 'something');
});
test('translate via custom tld', async t => {
const res = await translate('vertaler', {tld: 'cn'});
t.is(res.text, 'translator');
t.false(res.from.language.didYouMean);
t.is(res.from.language.iso, 'nl');
t.false(res.from.text.autoCorrected);
t.is(res.from.text.value, '');
t.false(res.from.text.didYouMean);
});
test('translate via an external language from outside of the API', async t => {
translate.languages['sr-Latn'] = 'Serbian Latin';
const res = await translate('translator', {to: 'sr-Latn'});
t.is(res.text, 'преводилац');
t.is(res.from.language.iso, 'en');
});
test('pass got options', async t => {
let a = 0;
const gotopts = {
hooks: {
afterResponse: [
response => {
a++;
return response;
}
]
}
};
const res = await translate('vertaler', {}, gotopts);
t.is(res.text, 'translator');
t.is(a, 2);
});
test('test get zh code', t => {
t.false(languages.getCode('zh'));
});
test('test get zh-CN code', t => {
t.is(languages.getCode('zh-CN'), 'zh-CN');
});
test('test get zh-cn code', t => {
t.false(languages.getCode('zh-cn'));
});
test('test get zh-TW code', t => {
t.is(languages.getCode('zh-TW'), 'zh-TW');
});
test('test get zh-tw code', t => {
t.false(languages.getCode('zh-tw'));
});
test('test zh unsupported', t => {
t.false(languages.isSupported('zh'));
});
test('test zh-CN supported', t => {
t.true(languages.isSupported('zh-CN'));
});
test('test zh-cn unsupported', t => {
t.false(languages.isSupported('zh-cn'));
});
test('test zh-TW supported', t => {
t.true(languages.isSupported('zh-TW'));
});
test('test zh-tw unsupported', t => {
t.false(languages.isSupported('zh-tw'));
});
test('test zh-CN supported – by name', t => {
t.true(languages.isSupported('chinese (simplified)'));
});