Skip to content

Commit ff93e43

Browse files
jmdobryAce Nassri
authored and
Ace Nassri
committed
Update Translate samples, with some minor tweaks to Language samples. (#255)
1 parent b5f2fb0 commit ff93e43

File tree

7 files changed

+158
-267
lines changed

7 files changed

+158
-267
lines changed

translate/package.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
"license": "Apache Version 2.0",
66
"author": "Google Inc.",
77
"scripts": {
8-
"test": "mocha -R spec --require intelli-espower-loader ../test/_setup.js test/*.test.js",
9-
"system-test": "mocha -R spec --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
8+
"test": "cd ..; npm run st -- translate/system-test/*.test.js"
109
},
1110
"dependencies": {
12-
"@google-cloud/translate": "^0.4.0",
11+
"@google-cloud/translate": "^0.5.0",
1312
"yargs": "^6.4.0"
1413
},
15-
"devDependencies": {
16-
"mocha": "^3.1.2"
17-
},
1814
"engines": {
1915
"node": ">=4.3.2"
2016
}

translate/quickstart.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
// Imports the Google Cloud client library
2020
const Translate = require('@google-cloud/translate');
2121

22-
// Your Translate API key
23-
const apiKey = 'YOUR_API_KEY';
22+
// Your Google Cloud Platform project ID
23+
const projectId = 'YOUR_PROJECT_ID';
2424

2525
// Instantiates a client
2626
const translateClient = Translate({
27-
key: apiKey
27+
projectId: projectId
2828
});
2929

3030
// The text to translate
@@ -33,13 +33,11 @@ const text = 'Hello, world!';
3333
const target = 'ru';
3434

3535
// Translates some text into Russian
36-
translateClient.translate(text, target, (err, translation) => {
37-
if (err) {
38-
console.error(err);
39-
return;
40-
}
36+
translateClient.translate(text, target)
37+
.then((results) => {
38+
const translation = results[0];
4139

42-
console.log(`Text: ${text}`);
43-
console.log(`Translation: ${translation}`);
44-
});
40+
console.log(`Text: ${text}`);
41+
console.log(`Translation: ${translation}`);
42+
});
4543
// [END translate_quickstart]

translate/system-test/quickstart.test.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,37 @@
1616
'use strict';
1717

1818
const proxyquire = require(`proxyquire`).noPreserveCache();
19-
const translate = proxyquire(`@google-cloud/translate`, {})({
20-
key: process.env.TRANSLATE_API_KEY
21-
});
22-
const string = `Hello, world!`;
23-
const expectedTranslation = `Привет мир!`;
24-
const targetLanguage = `ru`;
19+
const translate = proxyquire(`@google-cloud/translate`, {})();
2520

2621
describe(`translate:quickstart`, () => {
27-
let translateMock, TranslateMock;
28-
2922
it(`should translate a string`, (done) => {
30-
translateMock = {
31-
translate: (_string, _targetLanguage, _callback) => {
23+
const string = `Hello, world!`;
24+
const expectedTranslation = `Привет мир!`;
25+
const targetLanguage = `ru`;
26+
const translateMock = {
27+
translate: (_string, _targetLanguage) => {
3228
assert.equal(_string, string);
3329
assert.equal(_targetLanguage, targetLanguage);
34-
assert.equal(typeof _callback, 'function');
3530

36-
translate.translate(_string, _targetLanguage, (err, translation, apiResponse) => {
37-
_callback(err, translation, apiResponse);
38-
assert.ifError(err);
39-
assert.equal(translation, expectedTranslation);
40-
assert.notEqual(apiResponse, undefined);
41-
assert.equal(console.log.calledTwice, true);
42-
assert.deepEqual(console.log.firstCall.args, [`Text: ${string}`]);
43-
assert.deepEqual(console.log.secondCall.args, [`Translation: ${expectedTranslation}`]);
44-
done();
45-
});
31+
return translate.translate(_string, _targetLanguage)
32+
.then((results) => {
33+
const translation = results[0];
34+
assert.equal(translation, expectedTranslation);
35+
36+
setTimeout(() => {
37+
assert.equal(console.log.callCount, 2);
38+
assert.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
39+
assert.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
40+
done();
41+
}, 200);
42+
43+
return results;
44+
});
4645
}
4746
};
48-
TranslateMock = sinon.stub().returns(translateMock);
4947

5048
proxyquire(`../quickstart`, {
51-
'@google-cloud/translate': TranslateMock
49+
'@google-cloud/translate': sinon.stub().returns(translateMock)
5250
});
5351
});
5452
});

translate/system-test/translate.test.js

+36-24
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,57 @@ const run = require(`../../utils`).run;
2222
const cwd = path.join(__dirname, `..`);
2323
const cmd = `node translate.js`;
2424
const text = `Hello world!`;
25+
const text2 = `Goodbye!`;
2526
const toLang = `ru`;
2627

2728
describe(`translate:translate`, () => {
28-
const translate = Translate({
29-
key: process.env.TRANSLATE_API_KEY
30-
});
31-
if (!process.env.TRANSLATE_API_KEY) {
32-
process.stdout.write(`Skipping Translate API tests...\n`);
33-
return;
34-
}
29+
const translate = Translate();
3530

36-
it(`should detect language`, (done) => {
31+
it(`should detect language of a single string`, () => {
3732
const output = run(`${cmd} detect "${text}"`, cwd);
38-
translate.detect(text, (err, result) => {
39-
assert.ifError(err);
40-
assert.equal(output, `Detected: ${JSON.stringify(result)}`);
41-
done();
42-
});
33+
return translate.detect(text)
34+
.then((results) => {
35+
const expected = `Detections:\n${text} => ${results[0].language}`;
36+
assert.equal(output, expected);
37+
});
38+
});
39+
40+
it(`should detect language of multiple strings`, () => {
41+
const output = run(`${cmd} detect "${text}" "${text2}"`, cwd);
42+
return translate.detect([text, text2])
43+
.then((results) => {
44+
const expected = `Detections:\n${text} => ${results[0][0].language}\n${text2} => ${results[0][1].language}`;
45+
assert.equal(output, expected);
46+
});
4347
});
4448

4549
it(`should list languages`, () => {
4650
const output = run(`${cmd} list`, cwd);
47-
assert.notEqual(output.indexOf(`Languages:`), -1);
48-
assert.notEqual(output.indexOf(`{ code: 'af', name: 'Afrikaans' }`), -1);
51+
assert.equal(output.includes(`Languages:`), true);
52+
assert.equal(output.includes(`{ code: 'af', name: 'Afrikaans' }`), true);
4953
});
5054

5155
it(`should list languages with a target`, () => {
5256
const output = run(`${cmd} list es`, cwd);
53-
assert.notEqual(output.indexOf(`Languages:`), -1);
54-
assert.notEqual(output.indexOf(`{ code: 'af', name: 'afrikáans' }`), -1);
57+
assert.equal(output.includes(`Languages:`), true);
58+
assert.equal(output.includes(`{ code: 'af', name: 'afrikáans' }`), true);
5559
});
5660

57-
it(`should translate text`, (done) => {
61+
it(`should translate a single string`, () => {
5862
const output = run(`${cmd} translate ${toLang} "${text}"`, cwd);
59-
translate.translate(text, toLang, (err, translation) => {
60-
assert.ifError(err);
61-
assert.notEqual(output.indexOf(`Text: ["${text}"]`), -1);
62-
assert.notEqual(output.indexOf(`Translation: "${translation}"`), -1);
63-
done();
64-
});
63+
return translate.translate(text, toLang)
64+
.then((results) => {
65+
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
66+
assert.equal(output, expected);
67+
});
68+
});
69+
70+
it(`should translate multiple strings`, () => {
71+
const output = run(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
72+
return translate.translate([text, text2], toLang)
73+
.then((results) => {
74+
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
75+
assert.equal(output, expected);
76+
});
6577
});
6678
});

translate/test/quickstart.test.js

-43
This file was deleted.

translate/test/translate.test.js

-44
This file was deleted.

0 commit comments

Comments
 (0)