-
Notifications
You must be signed in to change notification settings - Fork 608
/
test.js
144 lines (119 loc) · 3.87 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
import test from 'ava';
import languages from './languages';
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(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 => {
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);
}
});