-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
143 lines (118 loc) · 3.83 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
import test from 'ava';
import translate from './index';
test('translate without any options', async t => {
try {
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);
} catch (err) {
t.fail(err.code);
}
});
test('translate from auto to dutch', async t => {
try {
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);
} catch (err) {
t.fail(err.code);
}
});
test('translate some english text setting the source language as portuguese', async t => {
try {
const res = await translate('translator', {from: 'pt', to: 'nl'});
t.true(res.from.language.didYouMean);
t.is(res.from.language.iso, 'en');
} catch (err) {
t.fail(err.code);
}
});
test('translate some misspelled english text to dutch', async t => {
try {
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();
}
} catch (err) {
t.fail(err.code);
}
});
test.todo('try to translate some text without an internet connection');
test('translate some text and get the raw output alongside', async t => {
try {
const res = await translate('vertaler', {raw: true});
t.truthy(res.raw);
} catch (err) {
t.fail(err.code);
}
});
test('test a supported language – by code', t => {
t.true(translate.isSupported('en'));
});
test('test an unsupported language – by code', t => {
t.false(translate.isSupported('js'));
});
test('test a supported language – by name', t => {
t.true(translate.isSupported('english'));
});
test('test an unsupported language – by name', t => {
t.false(translate.isSupported('javascript'));
});
test('get a language code by its name', t => {
t.is(translate.getCode('english'), 'en');
});
test('get an unsupported language code by its name', t => {
t.false(translate.getCode('javascript'));
});
test('get a supported language code by code', t => {
t.is(translate.getCode('en'), 'en');
});
test('call getCode with \'undefined\'', t => {
t.is(translate.getCode(undefined), false);
});
test('call getCode with \'null\'', t => {
t.is(translate.getCode(null), false);
});
test('call getCode with an empty string', t => {
t.is(translate.getCode(''), false);
});
test('call getCode with no arguments', t => {
t.is(translate.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 => {
try {
const res = await translate('iets', {from: 'dutch', to: 'english'});
t.is(res.from.language.iso, 'nl');
t.is(res.text, 'something');
} catch (err) {
t.fail(err.code);
}
});