Skip to content

Commit

Permalink
Repo Migration (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored and Ace Nassri committed Nov 17, 2022
1 parent f55026a commit 69634b8
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 115 deletions.
3 changes: 3 additions & 0 deletions translate/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rules:
no-console: off
33 changes: 8 additions & 25 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,23 @@
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"repository": "googleapis/nodejs-translate",
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"test": "samples test run --cmd ava -- -T 20s --verbose system-test/*.test.js"
"ava": "ava -T 20s --verbose test/*.test.js ./system-test/*.test.js",
"cover": "nyc --reporter=lcov --cache ava -T 20s --verbose test/*.test.js ./system-test/*.test.js && nyc report",
"test": "repo-tools test run --cmd npm -- run cover"
},
"dependencies": {
"@google-cloud/translate": "1.0.0",
"yargs": "8.0.2"
"yargs": "10.0.3"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.17",
"ava": "0.21.0",
"@google-cloud/nodejs-repo-tools": "2.0.11",
"ava": "0.22.0",
"proxyquire": "1.8.0",
"sinon": "3.2.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"product": "translate",
"samples": [
{
"id": "translate",
"name": "Translate",
"file": "translate.js",
"docs_link": "https://cloud.google.com/translate/docs",
"usage": "node translate.js --help"
}
]
"sinon": "4.0.1"
}
}
11 changes: 6 additions & 5 deletions translate/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const Translate = require('@google-cloud/translate');
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const translateClient = Translate({
projectId: projectId
const translate = new Translate({
projectId: projectId,
});

// The text to translate
Expand All @@ -33,14 +33,15 @@ const text = 'Hello, world!';
const target = 'ru';

// Translates some text into Russian
translateClient.translate(text, target)
.then((results) => {
translate
.translate(text, target)
.then(results => {
const translation = results[0];

console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
})
.catch((err) => {
.catch(err => {
console.error('ERROR:', err);
});
// [END translate_quickstart]
5 changes: 5 additions & 0 deletions translate/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
rules:
node/no-unpublished-require: off
node/no-unsupported-features: off
no-empty: off
13 changes: 8 additions & 5 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test.before(tools.checkCredentials);
test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);

test.cb(`should translate a string`, (t) => {
test.cb(`should translate a string`, t => {
const string = `Hello, world!`;
const expectedTranslation = `Привет мир!`;
const targetLanguage = `ru`;
Expand All @@ -34,15 +34,18 @@ test.cb(`should translate a string`, (t) => {
t.is(_string, string);
t.is(_targetLanguage, targetLanguage);

return translate.translate(_string, _targetLanguage)
return translate
.translate(_string, _targetLanguage)
.then(([translation]) => {
t.is(translation, expectedTranslation);

setTimeout(() => {
try {
t.is(console.log.callCount, 2);
t.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
t.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
t.deepEqual(console.log.getCall(1).args, [
`Translation: ${expectedTranslation}`,
]);
t.end();
} catch (err) {
t.end(err);
Expand All @@ -51,10 +54,10 @@ test.cb(`should translate a string`, (t) => {

return [translation];
});
}
},
};

proxyquire(`../quickstart`, {
'@google-cloud/translate': sinon.stub().returns(translateMock)
'@google-cloud/translate': sinon.stub().returns(translateMock),
});
});
44 changes: 30 additions & 14 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,71 @@ const toLang = `ru`;

test.before(tools.checkCredentials);

test(`should detect language of a single string`, async (t) => {
test(`should detect language of a single string`, async t => {
const output = await tools.runAsync(`${cmd} detect "${text}"`, cwd);
const [detection] = await translate.detect(text);
const expected = `Detections:\n${text} => ${detection.language}`;
t.is(output, expected);
});

test(`should detect language of multiple strings`, async (t) => {
const output = await tools.runAsync(`${cmd} detect "${text}" "${text2}"`, cwd);
test(`should detect language of multiple strings`, async t => {
const output = await tools.runAsync(
`${cmd} detect "${text}" "${text2}"`,
cwd
);
const [detections] = await translate.detect([text, text2]);
const expected = `Detections:\n${text} => ${detections[0].language}\n${text2} => ${detections[1].language}`;
const expected = `Detections:\n${text} => ${detections[0]
.language}\n${text2} => ${detections[1].language}`;
t.is(output, expected);
});

test(`should list languages`, async (t) => {
test(`should list languages`, async t => {
const output = await tools.runAsync(`${cmd} list`, cwd);
t.true(output.includes(`Languages:`));
t.true(output.includes(`{ code: 'af', name: 'Afrikaans' }`));
});

test(`should list languages with a target`, async (t) => {
test(`should list languages with a target`, async t => {
const output = await tools.runAsync(`${cmd} list es`, cwd);
t.true(output.includes(`Languages:`));
t.true(output.includes(`{ code: 'af', name: 'afrikáans' }`));
});

test(`should translate a single string`, async (t) => {
const output = await tools.runAsync(`${cmd} translate ${toLang} "${text}"`, cwd);
test(`should translate a single string`, async t => {
const output = await tools.runAsync(
`${cmd} translate ${toLang} "${text}"`,
cwd
);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
t.is(output, expected);
});

test(`should translate multiple strings`, async (t) => {
const output = await tools.runAsync(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
test(`should translate multiple strings`, async t => {
const output = await tools.runAsync(
`${cmd} translate ${toLang} "${text}" "${text2}"`,
cwd
);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`;
t.is(output, expected);
});

test(`should translate a single string with a model`, async (t) => {
const output = await tools.runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
test(`should translate a single string with a model`, async t => {
const output = await tools.runAsync(
`${cmd} translate-with-model ${toLang} ${model} "${text}"`,
cwd
);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
t.is(output, expected);
});

test(`should translate multiple strings with a model`, async (t) => {
const output = await tools.runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
test(`should translate multiple strings with a model`, async t => {
const output = await tools.runAsync(
`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`,
cwd
);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`;
t.is(output, expected);
Expand Down
Loading

0 comments on commit 69634b8

Please sign in to comment.