-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify Translate samples and bring up to standard. (#228)
- Loading branch information
Showing
3 changed files
with
178 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,66 @@ | ||
// 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'; | ||
|
||
var ISO6391 = require('iso-639-1'); | ||
var program = require('../translate'); | ||
const Translate = require(`@google-cloud/translate`); | ||
const path = require(`path`); | ||
const run = require(`../../utils`).run; | ||
|
||
var text = 'Hello world!'; | ||
var toLang = 'ru'; | ||
const cwd = path.join(__dirname, `..`); | ||
const cmd = `node translate.js`; | ||
const text = `Hello world!`; | ||
const toLang = `ru`; | ||
|
||
describe('translate:translate', function () { | ||
describe(`translate:translate`, () => { | ||
const translate = Translate({ | ||
key: process.env.TRANSLATE_API_KEY | ||
}); | ||
if (!process.env.TRANSLATE_API_KEY) { | ||
process.stdout.write('Skipping Translate API tests...\n'); | ||
process.stdout.write(`Skipping Translate API tests...\n`); | ||
return; | ||
} | ||
|
||
describe('detectLanguage', function () { | ||
it('should detect language', function (done) { | ||
program.detectLanguage(text, function (err, result, apiResponse) { | ||
assert.equal(err, null); | ||
assert.notEqual(result, undefined); | ||
assert.equal(result.language, 'en', 'should have detected english'); | ||
assert.equal(console.log.calledOnce, true); | ||
assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]); | ||
assert.notEqual(apiResponse, undefined); | ||
done(); | ||
}); | ||
it(`should detect language`, (done) => { | ||
const output = run(`${cmd} detect "${text}"`, cwd); | ||
translate.detect(text, (err, result) => { | ||
assert.ifError(err); | ||
assert.equal(output, `Detected: ${JSON.stringify(result)}`); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('listLanguages', function () { | ||
it('should list languages', function (done) { | ||
program.listLanguages(function (err, languages, apiResponse) { | ||
assert.equal(err, null); | ||
assert.equal(Array.isArray(languages), true); | ||
assert.equal(languages.length > 0, true); | ||
var matchingLanguages = languages.filter(function (language) { | ||
return language.code === 'af' && language.name === 'Afrikaans'; | ||
}); | ||
assert.equal(matchingLanguages.length, 1, 'found language with name in English'); | ||
assert.equal(console.log.calledOnce, true); | ||
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]); | ||
assert.notEqual(apiResponse, undefined); | ||
done(); | ||
}); | ||
}); | ||
it(`should list languages`, () => { | ||
const output = run(`${cmd} list`, cwd); | ||
assert.notEqual(output.indexOf(`Languages:`), -1); | ||
assert.notEqual(output.indexOf(`{ code: 'af', name: 'Afrikaans' }`), -1); | ||
}); | ||
|
||
describe('listLanguagesWithTarget', function () { | ||
it('should list languages with a target', function (done) { | ||
program.listLanguagesWithTarget('es', function (err, languages, apiResponse) { | ||
assert.equal(err, null); | ||
assert.equal(Array.isArray(languages), true); | ||
assert.equal(languages.length > 0, true); | ||
var matchingLanguages = languages.filter(function (language) { | ||
return language.code === 'af' && language.name === 'afrikáans'; | ||
}); | ||
assert.equal(matchingLanguages.length, 1, 'found language with name in Spanish'); | ||
assert.equal(console.log.calledOnce, true); | ||
assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]); | ||
assert.notEqual(apiResponse, undefined); | ||
done(); | ||
}); | ||
}); | ||
it(`should list languages with a target`, () => { | ||
const output = run(`${cmd} list es`, cwd); | ||
assert.notEqual(output.indexOf(`Languages:`), -1); | ||
assert.notEqual(output.indexOf(`{ code: 'af', name: 'afrikáans' }`), -1); | ||
}); | ||
|
||
describe('translateText', function () { | ||
it('should translate text', function (done) { | ||
var expected = 'Привет мир!'; | ||
|
||
program.translateText(text, toLang, undefined, function (err, translation, apiResponse) { | ||
assert.equal(err, null); | ||
assert.equal(translation, expected); | ||
assert.equal(console.log.calledOnce, true); | ||
assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]); | ||
assert.notEqual(apiResponse, undefined); | ||
done(); | ||
}); | ||
it(`should translate text`, (done) => { | ||
const output = run(`${cmd} translate ${toLang} "${text}"`, cwd); | ||
translate.translate(text, toLang, (err, translation) => { | ||
assert.ifError(err); | ||
assert.notEqual(output.indexOf(`Text: ["${text}"]`), -1); | ||
assert.notEqual(output.indexOf(`Translation: "${translation}"`), -1); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.