Skip to content

Commit

Permalink
Add remaining Translate samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Aug 31, 2016
1 parent 241d28f commit 89aa0d7
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 88 deletions.
27 changes: 15 additions & 12 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ __Usage:__ `node translate --help`

```
Commands:
detect <text> Detect the language of the provided text
list List available translation languages.
translate <text> Translate the provided text to the target language.
detect <input..> Detect the language of the provided text or texts
list [target] List available translation languages.
translate <input..> Translate the provided text or texts to the target language.
Options:
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment
variable. [string]
--help Show help [boolean]
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable. [string]
--help Show help [boolean]
Examples:
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
node translate list -k your-key List available translation languages.
node translate translate -k your-key --to ru "Good Translate "Good morning!" to Russian,
morning!" auto-detecting English.
node translate translate -k your-key --to ru Translate "Good morning!" to Russian from
--from en "Good morning!" English.
node translate detect -k your-key "Hello world!" Detect the language of "Hello world!".
node translate detect -k your-key "Hello world!" "Goodbye" Detect the language of "Hello world!" and "Goodbye".
node translate list -k your-key List available translation languages with names in
English.
node translate list es -k your-key List available translation languages with names in
Spanish.
node translate translate -k your-key --to ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting
English.
node translate translate -k your-key --to ru --from en "Good Translate "Good morning!" to Russian from English.
morning!"
For more information, see https://cloud.google.com/translate/docs
```
Expand Down
2 changes: 1 addition & 1 deletion translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"@google-cloud/translate": "^0.1.1",
"@google-cloud/translate": "^0.2.0",
"iso-639-1": "^1.2.1",
"yargs": "^5.0.0"
},
Expand Down
16 changes: 13 additions & 3 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ describe('translate:translate', function () {
assert.ifError(err);
assert(result, 'should have received a result');
assert.equal(result.language, 'en', 'should have detected english');
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
assert(console.log.calledWith('Detected language:', result));
done();
});
});
});

describe('listLanguages', function () {
it('should list languages', function (done) {
program.listLanguages(apiKey, function (err, languages) {
program.listLanguages(null, apiKey, function (err, languages) {
assert.ifError(err);
assert(Array.isArray(languages));
assert(languages.length > 0);
assert(console.log.calledWith('Found %d language(s)!', languages.length));
done();
});
});

it('should list languages with a different target', function (done) {
program.listLanguages('es', apiKey, function (err, languages) {
assert.ifError(err);
assert(Array.isArray(languages));
assert(languages.length > 0);
Expand All @@ -49,7 +59,7 @@ describe('translate:translate', function () {
describe('translateText', function () {
it('should translate text', function (done) {
var options = {
text: text,
input: text,
apiKey: apiKey,
to: 'ru'
};
Expand Down
105 changes: 56 additions & 49 deletions translate/test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var proxyquire = require('proxyquire').noCallThru();
var text = 'Hello world!';
var apiKey = 'key';
var target = 'es';

function getSample () {
var languagesMock = [
Expand All @@ -29,9 +30,9 @@ function getSample () {
};
var translationMock = 'Привет мир!';
var translateMock = {
getLanguages: sinon.stub().callsArgWith(0, null, languagesMock),
detect: sinon.stub().callsArgWith(1, null, resultMock),
translate: sinon.stub().callsArgWith(2, null, translationMock)
getLanguages: sinon.stub().yields(null, languagesMock),
detect: sinon.stub().yields(null, resultMock),
translate: sinon.stub().yields(null, translationMock)
};
var TranslateMock = sinon.stub().returns(translateMock);

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

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

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

it('should handle error', function () {
var error = new Error('error');
var sample = getSample();
var callback = sinon.stub();
sample.mocks.translate.detect = sinon.stub().callsArgWith(1, error);
sample.mocks.translate.detect.yields(error);

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

assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
assert(callback.firstCall.args[0], 'callback received error');
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
assert.equal(callback.calledOnce, true);
assert.deepEqual(callback.firstCall.args, [error]);
});
});

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

sample.program.listLanguages(apiKey, callback);
sample.program.listLanguages(null, apiKey, callback);

assert(sample.mocks.translate.getLanguages.calledOnce, 'method called once');
assert.equal(sample.mocks.translate.getLanguages.firstCall.args.length, 1, 'method received 1 argument');
assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
assert.strictEqual(callback.firstCall.args[1], sample.mocks.languages, 'callback received result');
assert(console.log.calledWith('Found %d language(s)!', sample.mocks.languages.length));
assert.equal(sample.mocks.translate.getLanguages.calledOnce, true);
assert.deepEqual(sample.mocks.translate.getLanguages.firstCall.args.slice(0, -1), []);
assert.equal(callback.calledOnce, true);
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.languages]);
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', sample.mocks.languages.length]);

sample.program.listLanguages(target, apiKey, callback);

assert.equal(sample.mocks.translate.getLanguages.calledTwice, true);
assert.deepEqual(sample.mocks.translate.getLanguages.firstCall.args.slice(0, -1), []);
assert.equal(callback.calledTwice, true);
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.languages]);
assert.equal(console.log.calledTwice, true);
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', sample.mocks.languages.length]);
});

it('should handle error', function () {
var error = new Error('error');
var sample = getSample();
var callback = sinon.stub();
sample.mocks.translate.getLanguages = sinon.stub().callsArgWith(0, error);
sample.mocks.translate.getLanguages.yields(error);

sample.program.listLanguages(apiKey, callback);
sample.program.listLanguages(null, apiKey, callback);

assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
assert(callback.firstCall.args[0], 'callback received error');
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
assert.equal(callback.calledOnce, true);
assert.deepEqual(callback.firstCall.args, [error]);
});
});

Expand All @@ -119,25 +122,22 @@ describe('translate:translate', function () {
var sample = getSample();
var callback = sinon.stub();
var options = {
text: text,
input: [text],
to: 'ru',
apiKey: apiKey
};

sample.program.translateText(options, callback);

assert(sample.mocks.translate.translate.calledOnce, 'method called once');
assert.equal(sample.mocks.translate.translate.firstCall.args.length, 3, 'method received 3 arguments');
assert.equal(sample.mocks.translate.translate.firstCall.args[0], text, 'method received correct first argument');
assert.deepEqual(sample.mocks.translate.translate.firstCall.args[1], {
assert.equal(sample.mocks.translate.translate.calledOnce, true);
assert.deepEqual(sample.mocks.translate.translate.firstCall.args.slice(0, -1), [[text], {
to: 'ru',
from: undefined
}, 'method received correct second argument');
assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
assert.strictEqual(callback.firstCall.args[1], sample.mocks.translation, 'callback received result');
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
}]);
assert.equal(callback.calledOnce, true);
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.translation]);
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, ['Translated text to %s:', 'Russian']);
});

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

sample.program.translateText(options, callback);

assert(callback.calledOnce, 'callback called once');
assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument');
assert(callback.firstCall.args[0], 'callback received error');
assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message');
assert.equal(callback.calledOnce, true);
assert.deepEqual(callback.firstCall.args, [error]);
});
});

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

it('should call listLanguages', function () {
Expand All @@ -176,7 +174,16 @@ describe('translate:translate', function () {
sinon.stub(program, 'listLanguages');
program.main(['list', '-k', apiKey]);
assert.equal(program.listLanguages.calledOnce, true);
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [apiKey]);
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [undefined, apiKey]);
});

it('should call listLanguages with a target', function () {
var program = getSample().program;

sinon.stub(program, 'listLanguages');
program.main(['list', target, '-k', apiKey]);
assert.equal(program.listLanguages.calledOnce, true);
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [target, apiKey]);
});

it('should call translateText', function () {
Expand All @@ -186,7 +193,7 @@ describe('translate:translate', function () {
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
assert.equal(program.translateText.calledOnce, true);
assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [{
text: text,
input: [text],
to: 'ru',
from: undefined,
apiKey: apiKey
Expand Down
Loading

0 comments on commit 89aa0d7

Please sign in to comment.