From 1426f7c3fa7dda37ed5364a03c34cd5f21d019bc Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Mon, 3 Oct 2016 14:44:10 -0700 Subject: [PATCH] New quickstarts. (#226) * New quickstarts. * Address comments. --- translate/package.json | 7 +++-- translate/quickstart.js | 34 +++++++++++++--------- translate/system-test/quickstart.test.js | 36 +++++++++++++++--------- translate/test/quickstart.test.js | 33 +++++++++++++--------- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/translate/package.json b/translate/package.json index 53120d7e1c..8c7dca1419 100644 --- a/translate/package.json +++ b/translate/package.json @@ -11,9 +11,12 @@ "dependencies": { "@google-cloud/translate": "^0.2.0", "iso-639-1": "^1.2.1", - "yargs": "^5.0.0" + "yargs": "^6.0.0" }, "devDependencies": { - "mocha": "^3.0.2" + "mocha": "^3.1.0" + }, + "engines": { + "node": ">=4.3.2" } } diff --git a/translate/quickstart.js b/translate/quickstart.js index e3f49aedf6..2ed38c9ba5 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -32,8 +34,12 @@ const target = 'ru'; // Translates some text into Russian translateClient.translate(text, target, (err, translation) => { - if (!err) { - // The text was translated successfully + if (err) { + console.error(err); + return; } + + console.log(`Text: ${text}`); + console.log(`Translation: ${translation}`); }); // [END translate_quickstart] diff --git a/translate/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js index 3936592c5b..640b1afeb7 100644 --- a/translate/system-test/quickstart.test.js +++ b/translate/system-test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -18,6 +20,7 @@ const translate = proxyquire(`@google-cloud/translate`, {})({ key: process.env.TRANSLATE_API_KEY }); const string = `Hello, world!`; +const expectedTranslation = `Привет мир!`; const targetLanguage = `ru`; describe(`translate:quickstart`, () => { @@ -25,14 +28,19 @@ describe(`translate:quickstart`, () => { it(`should translate a string`, (done) => { translateMock = { - translate: (_string, _targetLanguage) => { + translate: (_string, _targetLanguage, _callback) => { assert.equal(_string, string); assert.equal(_targetLanguage, targetLanguage); + assert.equal(typeof _callback, 'function'); translate.translate(_string, _targetLanguage, (err, translation, apiResponse) => { + _callback(err, translation, apiResponse); assert.ifError(err); - assert.equal(translation, `Привет мир!`); + assert.equal(translation, expectedTranslation); assert.notEqual(apiResponse, undefined); + assert.equal(console.log.calledTwice, true); + assert.deepEqual(console.log.firstCall.args, [`Text: ${string}`]); + assert.deepEqual(console.log.secondCall.args, [`Translation: ${expectedTranslation}`]); done(); }); } diff --git a/translate/test/quickstart.test.js b/translate/test/quickstart.test.js index 85c20267b6..e9c3dd683d 100644 --- a/translate/test/quickstart.test.js +++ b/translate/test/quickstart.test.js @@ -1,15 +1,17 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; @@ -17,15 +19,16 @@ const proxyquire = require(`proxyquire`).noCallThru(); describe(`translate:quickstart`, () => { let translateMock, TranslateMock; + const error = new Error(`error`); before(() => { translateMock = { - translate: sinon.stub().yields(null, `Привет мир!`, {}) + translate: sinon.stub().yields(error) }; TranslateMock = sinon.stub().returns(translateMock); }); - it(`should translate a string`, () => { + it(`should handle error`, () => { proxyquire(`../quickstart`, { '@google-cloud/translate': TranslateMock }); @@ -34,5 +37,7 @@ describe(`translate:quickstart`, () => { assert.deepEqual(TranslateMock.firstCall.args, [{ key: 'YOUR_API_KEY' }]); assert.equal(translateMock.translate.calledOnce, true); assert.deepEqual(translateMock.translate.firstCall.args.slice(0, -1), ['Hello, world!', 'ru']); + assert.equal(console.error.calledOnce, true); + assert.deepEqual(console.error.firstCall.args, [error]); }); });