Skip to content

Commit 5df96a4

Browse files
committed
Add remaining Translate samples.
1 parent 241d28f commit 5df96a4

File tree

5 files changed

+110
-88
lines changed

5 files changed

+110
-88
lines changed

translate/README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,25 @@ __Usage:__ `node translate --help`
3333

3434
```
3535
Commands:
36-
detect <text> Detect the language of the provided text
37-
list List available translation languages.
38-
translate <text> Translate the provided text to the target language.
36+
detect <input..> Detect the language of the provided text or texts
37+
list [target] List available translation languages.
38+
translate <input..> Translate the provided text or texts to the target language.
3939
4040
Options:
41-
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment
42-
variable. [string]
43-
--help Show help [boolean]
41+
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable. [string]
42+
--help Show help [boolean]
4443
4544
Examples:
46-
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
47-
node translate list -k your-key List available translation languages.
48-
node translate translate -k your-key --to ru "Good Translate "Good morning!" to Russian,
49-
morning!" auto-detecting English.
50-
node translate translate -k your-key --to ru Translate "Good morning!" to Russian from
51-
--from en "Good morning!" English.
45+
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
46+
node translate detect -k your-key "Hello world!" "Goodbye" Detect the language of "Hello world!" and "Goodbye".
47+
node translate list -k your-key List available translation languages with names in
48+
English.
49+
node translate list es -k your-key List available translation languages with names in
50+
Spanish.
51+
node translate translate -k your-key --to ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting
52+
English.
53+
node translate translate -k your-key --to ru --from en "Good Translate "Good morning!" to Russian from English.
54+
morning!"
5255
5356
For more information, see https://cloud.google.com/translate/docs
5457
```

translate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
1010
},
1111
"dependencies": {
12-
"@google-cloud/translate": "^0.1.1",
12+
"@google-cloud/translate": "^0.2.0",
1313
"iso-639-1": "^1.2.1",
1414
"yargs": "^5.0.0"
1515
},

translate/system-test/translate.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ describe('translate:translate', function () {
2828
assert.ifError(err);
2929
assert(result, 'should have received a result');
3030
assert.equal(result.language, 'en', 'should have detected english');
31-
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
31+
assert(console.log.calledWith('Detected language:', result));
3232
done();
3333
});
3434
});
3535
});
3636

3737
describe('listLanguages', function () {
3838
it('should list languages', function (done) {
39-
program.listLanguages(apiKey, function (err, languages) {
39+
program.listLanguages(null, apiKey, function (err, languages) {
40+
assert.ifError(err);
41+
assert(Array.isArray(languages));
42+
assert(languages.length > 0);
43+
assert(console.log.calledWith('Found %d language(s)!', languages.length));
44+
done();
45+
});
46+
});
47+
48+
it('should list languages with a different target', function (done) {
49+
program.listLanguages('es', apiKey, function (err, languages) {
4050
assert.ifError(err);
4151
assert(Array.isArray(languages));
4252
assert(languages.length > 0);
@@ -49,7 +59,7 @@ describe('translate:translate', function () {
4959
describe('translateText', function () {
5060
it('should translate text', function (done) {
5161
var options = {
52-
text: text,
62+
input: text,
5363
apiKey: apiKey,
5464
to: 'ru'
5565
};

translate/test/translate.test.js

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var proxyquire = require('proxyquire').noCallThru();
1717
var text = 'Hello world!';
1818
var apiKey = 'key';
19+
var target = 'es';
1920

2021
function getSample () {
2122
var languagesMock = [
@@ -29,9 +30,9 @@ function getSample () {
2930
};
3031
var translationMock = 'Привет мир!';
3132
var translateMock = {
32-
getLanguages: sinon.stub().callsArgWith(0, null, languagesMock),
33-
detect: sinon.stub().callsArgWith(1, null, resultMock),
34-
translate: sinon.stub().callsArgWith(2, null, translationMock)
33+
getLanguages: sinon.stub().yields(null, languagesMock),
34+
detect: sinon.stub().yields(null, resultMock),
35+
translate: sinon.stub().yields(null, translationMock)
3536
};
3637
var TranslateMock = sinon.stub().returns(translateMock);
3738

@@ -58,28 +59,24 @@ describe('translate:translate', function () {
5859

5960
sample.program.detectLanguage(text, apiKey, callback);
6061

61-
assert(sample.mocks.translate.detect.calledOnce, 'method called once');
62-
assert.equal(sample.mocks.translate.detect.firstCall.args.length, 2, 'method received 2 arguments');
63-
assert.equal(sample.mocks.translate.detect.firstCall.args[0], text, 'method received correct argument');
64-
assert(callback.calledOnce, 'callback called once');
65-
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
66-
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
67-
assert.strictEqual(callback.firstCall.args[1], sample.mocks.result, 'callback received result');
68-
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', sample.mocks.result.confidence));
62+
assert.equal(sample.mocks.translate.detect.calledOnce, true);
63+
assert.deepEqual(sample.mocks.translate.detect.firstCall.args.slice(0, -1), [text]);
64+
assert.equal(callback.calledOnce, true);
65+
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.result]);
66+
assert.equal(console.log.calledOnce, true);
67+
assert.deepEqual(console.log.firstCall.args, ['Detected language:', sample.mocks.result]);
6968
});
7069

7170
it('should handle error', function () {
7271
var error = new Error('error');
7372
var sample = getSample();
7473
var callback = sinon.stub();
75-
sample.mocks.translate.detect = sinon.stub().callsArgWith(1, error);
74+
sample.mocks.translate.detect.yields(error);
7675

7776
sample.program.detectLanguage(text, apiKey, callback);
7877

79-
assert(callback.calledOnce, 'callback called once');
80-
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
81-
assert(callback.firstCall.args[0], 'callback received error');
82-
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
78+
assert.equal(callback.calledOnce, true);
79+
assert.deepEqual(callback.firstCall.args, [error]);
8380
});
8481
});
8582

@@ -88,29 +85,35 @@ describe('translate:translate', function () {
8885
var sample = getSample();
8986
var callback = sinon.stub();
9087

91-
sample.program.listLanguages(apiKey, callback);
88+
sample.program.listLanguages(null, apiKey, callback);
9289

93-
assert(sample.mocks.translate.getLanguages.calledOnce, 'method called once');
94-
assert.equal(sample.mocks.translate.getLanguages.firstCall.args.length, 1, 'method received 1 argument');
95-
assert(callback.calledOnce, 'callback called once');
96-
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
97-
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
98-
assert.strictEqual(callback.firstCall.args[1], sample.mocks.languages, 'callback received result');
99-
assert(console.log.calledWith('Found %d language(s)!', sample.mocks.languages.length));
90+
assert.equal(sample.mocks.translate.getLanguages.calledOnce, true);
91+
assert.deepEqual(sample.mocks.translate.getLanguages.firstCall.args.slice(0, -1), []);
92+
assert.equal(callback.calledOnce, true);
93+
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.languages]);
94+
assert.equal(console.log.calledOnce, true);
95+
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', sample.mocks.languages.length]);
96+
97+
sample.program.listLanguages(target, apiKey, callback);
98+
99+
assert.equal(sample.mocks.translate.getLanguages.calledTwice, true);
100+
assert.deepEqual(sample.mocks.translate.getLanguages.firstCall.args.slice(0, -1), []);
101+
assert.equal(callback.calledTwice, true);
102+
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.languages]);
103+
assert.equal(console.log.calledTwice, true);
104+
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', sample.mocks.languages.length]);
100105
});
101106

102107
it('should handle error', function () {
103108
var error = new Error('error');
104109
var sample = getSample();
105110
var callback = sinon.stub();
106-
sample.mocks.translate.getLanguages = sinon.stub().callsArgWith(0, error);
111+
sample.mocks.translate.getLanguages.yields(error);
107112

108-
sample.program.listLanguages(apiKey, callback);
113+
sample.program.listLanguages(null, apiKey, callback);
109114

110-
assert(callback.calledOnce, 'callback called once');
111-
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
112-
assert(callback.firstCall.args[0], 'callback received error');
113-
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
115+
assert.equal(callback.calledOnce, true);
116+
assert.deepEqual(callback.firstCall.args, [error]);
114117
});
115118
});
116119

@@ -119,25 +122,22 @@ describe('translate:translate', function () {
119122
var sample = getSample();
120123
var callback = sinon.stub();
121124
var options = {
122-
text: text,
125+
input: [text],
123126
to: 'ru',
124127
apiKey: apiKey
125128
};
126129

127130
sample.program.translateText(options, callback);
128131

129-
assert(sample.mocks.translate.translate.calledOnce, 'method called once');
130-
assert.equal(sample.mocks.translate.translate.firstCall.args.length, 3, 'method received 3 arguments');
131-
assert.equal(sample.mocks.translate.translate.firstCall.args[0], text, 'method received correct first argument');
132-
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[1], {
132+
assert.equal(sample.mocks.translate.translate.calledOnce, true);
133+
assert.deepEqual(sample.mocks.translate.translate.firstCall.args.slice(0, -1), [[text], {
133134
to: 'ru',
134135
from: undefined
135-
}, 'method received correct second argument');
136-
assert(callback.calledOnce, 'callback called once');
137-
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
138-
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
139-
assert.strictEqual(callback.firstCall.args[1], sample.mocks.translation, 'callback received result');
140-
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
136+
}]);
137+
assert.equal(callback.calledOnce, true);
138+
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.translation]);
139+
assert.equal(console.log.calledOnce, true);
140+
assert.deepEqual(console.log.firstCall.args, ['Translated text to %s:', 'Russian']);
141141
});
142142

143143
it('should handle error', function () {
@@ -149,14 +149,12 @@ describe('translate:translate', function () {
149149
to: 'ru',
150150
apiKey: apiKey
151151
};
152-
sample.mocks.translate.translate = sinon.stub().callsArgWith(2, error);
152+
sample.mocks.translate.translate.yields(error);
153153

154154
sample.program.translateText(options, callback);
155155

156-
assert(callback.calledOnce, 'callback called once');
157-
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
158-
assert(callback.firstCall.args[0], 'callback received error');
159-
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
156+
assert.equal(callback.calledOnce, true);
157+
assert.deepEqual(callback.firstCall.args, [error]);
160158
});
161159
});
162160

@@ -167,7 +165,7 @@ describe('translate:translate', function () {
167165
sinon.stub(program, 'detectLanguage');
168166
program.main(['detect', text, '-k', apiKey]);
169167
assert.equal(program.detectLanguage.calledOnce, true);
170-
assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [text, apiKey]);
168+
assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [[text], apiKey]);
171169
});
172170

173171
it('should call listLanguages', function () {
@@ -176,7 +174,16 @@ describe('translate:translate', function () {
176174
sinon.stub(program, 'listLanguages');
177175
program.main(['list', '-k', apiKey]);
178176
assert.equal(program.listLanguages.calledOnce, true);
179-
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [apiKey]);
177+
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [undefined, apiKey]);
178+
});
179+
180+
it('should call listLanguages with a target', function () {
181+
var program = getSample().program;
182+
183+
sinon.stub(program, 'listLanguages');
184+
program.main(['list', target, '-k', apiKey]);
185+
assert.equal(program.listLanguages.calledOnce, true);
186+
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [target, apiKey]);
180187
});
181188

182189
it('should call translateText', function () {
@@ -186,7 +193,7 @@ describe('translate:translate', function () {
186193
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
187194
assert.equal(program.translateText.calledOnce, true);
188195
assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [{
189-
text: text,
196+
input: [text],
190197
to: 'ru',
191198
from: undefined,
192199
apiKey: apiKey

0 commit comments

Comments
 (0)