From 5df96a41ef99e0a742aa86ae63e5463cf05c9845 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 30 Aug 2016 14:37:40 -0700 Subject: [PATCH] Add remaining Translate samples. --- translate/README.md | 27 +++--- translate/package.json | 2 +- translate/system-test/translate.test.js | 16 +++- translate/test/translate.test.js | 105 +++++++++++++----------- translate/translate.js | 48 +++++------ 5 files changed, 110 insertions(+), 88 deletions(-) diff --git a/translate/README.md b/translate/README.md index 173ff49046b..519a2871e7f 100644 --- a/translate/README.md +++ b/translate/README.md @@ -33,22 +33,25 @@ __Usage:__ `node translate --help` ``` Commands: - detect Detect the language of the provided text - list List available translation languages. - translate Translate the provided text to the target language. + detect Detect the language of the provided text or texts + list [target] List available translation languages. + translate 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 ``` diff --git a/translate/package.json b/translate/package.json index 79251cdc1e7..53120d7e1c5 100644 --- a/translate/package.json +++ b/translate/package.json @@ -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" }, diff --git a/translate/system-test/translate.test.js b/translate/system-test/translate.test.js index d5d8369b27d..818fa8b4e63 100644 --- a/translate/system-test/translate.test.js +++ b/translate/system-test/translate.test.js @@ -28,7 +28,7 @@ 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(); }); }); @@ -36,7 +36,17 @@ describe('translate:translate', function () { 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); @@ -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' }; diff --git a/translate/test/translate.test.js b/translate/test/translate.test.js index bff3027bf1f..f4a03a87aa4 100644 --- a/translate/test/translate.test.js +++ b/translate/test/translate.test.js @@ -16,6 +16,7 @@ var proxyquire = require('proxyquire').noCallThru(); var text = 'Hello world!'; var apiKey = 'key'; +var target = 'es'; function getSample () { var languagesMock = [ @@ -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); @@ -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]); }); }); @@ -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]); }); }); @@ -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 () { @@ -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]); }); }); @@ -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 () { @@ -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 () { @@ -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 diff --git a/translate/translate.js b/translate/translate.js index 8f24c680c75..3a75621a5ef 100644 --- a/translate/translate.js +++ b/translate/translate.js @@ -27,30 +27,25 @@ var ISO6391 = require('iso-639-1'); // [START detect_language] /** - * Detect the language of the provided text. + * Detect the language of the provided text or texts. * - * @param {string} text The text for which to detect the language. + * @param {string|string[]} input The text or texts for which to detect the language. * @param {string} apiKey Your Translate API key. * @param {function} cb The callback function. */ -function detectLanguage (text, apiKey, callback) { +function detectLanguage (input, apiKey, callback) { // Instantiate a translate client var translate = Translate({ key: apiKey }); // See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate - translate.detect(text, function (err, result) { + translate.detect(input, function (err, result) { if (err) { return callback(err); } - console.log( - 'Detected %s (%s) with confidence %d', - ISO6391.getName(result.language), - result.language, - result.confidence - ); + console.log('Detected language:', result); return callback(null, result); }); } @@ -58,12 +53,17 @@ function detectLanguage (text, apiKey, callback) { // [START list_languages] /** - * List all of the authenticated project's buckets. + * Get a list of supported languages. To specify language names in a language + * other than English, specify a target. * + * @param {string} target The target language in which to include language names. * @param {string} apiKey Your Translate API key. * @param {function} cb The callback function. */ -function listLanguages (apiKey, callback) { +function listLanguages (target, apiKey, callback) { + // Include language names in English by default + target = target || 'en'; + // Instantiate a translate client var translate = Translate({ key: apiKey @@ -83,10 +83,10 @@ function listLanguages (apiKey, callback) { // [START translate_text] /** - * Translate the provided text. + * Translate the provided text or texts. * * @param {object} options Configuration options. - * @param {string} options.text The text to translate. + * @param {string} options.input The text or texts to translate. * @param {string} options.from The language of the source text. * @param {string} options.to The language to which to translate the text. * @param {string} options.apiKey Your Translate API key. @@ -104,7 +104,7 @@ function translateText (options, callback) { }; // See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate - translate.translate(options.text, config, function (err, translation) { + translate.translate(options.input, config, function (err, translation) { if (err) { return callback(err); } @@ -132,13 +132,13 @@ var program = module.exports = { cli .demand(1) - .command('detect ', 'Detect the language of the provided text', {}, function (options) { - program.detectLanguage(options.text, options.apiKey, utils.makeHandler(false)); + .command('detect ', 'Detect the language of the provided text or texts', {}, function (options) { + program.detectLanguage(options.input, options.apiKey, utils.makeHandler(false)); }) - .command('list', 'List available translation languages.', {}, function (options) { - program.listLanguages(options.apiKey, utils.makeHandler()); + .command('list [target]', 'List available translation languages.', {}, function (options) { + program.listLanguages(options.target, options.apiKey, utils.makeHandler()); }) - .command('translate ', 'Translate the provided text to the target language.', { + .command('translate ', 'Translate the provided text or texts to the target language.', { to: { alias: 't', demand: true, @@ -153,7 +153,7 @@ cli description: 'The language of the source text.' } }, function (options) { - program.translateText(utils.pick(options, ['text', 'to', 'from', 'apiKey']), utils.makeHandler()); + program.translateText(utils.pick(options, ['input', 'to', 'from', 'apiKey']), utils.makeHandler()); }) .option('apiKey', { alias: 'k', @@ -164,10 +164,12 @@ cli description: 'Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.' }) .example('node $0 detect -k your-key "Hello world!"', 'Detect the language of "Hello world!".') - .example('node $0 list -k your-key', 'List available translation languages.') + .example('node $0 detect -k your-key "Hello world!" "Goodbye"', 'Detect the language of "Hello world!" and "Goodbye".') + .example('node $0 list -k your-key', 'List available translation languages with names in English.') + .example('node $0 list es -k your-key', 'List available translation languages with names in Spanish.') .example('node $0 translate -k your-key --to ru "Good morning!"', 'Translate "Good morning!" to Russian, auto-detecting English.') .example('node $0 translate -k your-key --to ru --from en "Good morning!"', 'Translate "Good morning!" to Russian from English.') - .wrap(100) + .wrap(120) .recommendCommands() .epilogue('For more information, see https://cloud.google.com/translate/docs');