Skip to content

Commit

Permalink
New quickstarts. (#226)
Browse files Browse the repository at this point in the history
* New quickstarts.

* Address comments.
  • Loading branch information
jmdobry authored and Ace Nassri committed Nov 17, 2022
1 parent aeb8001 commit 1426f7c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 44 deletions.
7 changes: 5 additions & 2 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
34 changes: 20 additions & 14 deletions translate/quickstart.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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]
36 changes: 22 additions & 14 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -18,21 +20,27 @@ const translate = proxyquire(`@google-cloud/translate`, {})({
key: process.env.TRANSLATE_API_KEY
});
const string = `Hello, world!`;
const expectedTranslation = `Привет мир!`;
const targetLanguage = `ru`;

describe(`translate:quickstart`, () => {
let translateMock, TranslateMock;

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();
});
}
Expand Down
33 changes: 19 additions & 14 deletions translate/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
// 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';

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
});
Expand All @@ -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]);
});
});

0 comments on commit 1426f7c

Please sign in to comment.