|
1 | | -// Copyright 2015-2016, Google, Inc. |
2 | | -// Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | -// you may not use this file except in compliance with the License. |
4 | | -// You may obtain a copy of the License at |
5 | | -// |
6 | | -// http://www.apache.org/licenses/LICENSE-2.0 |
7 | | -// |
8 | | -// Unless required by applicable law or agreed to in writing, software |
9 | | -// distributed under the License is distributed on an "AS IS" BASIS, |
10 | | -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11 | | -// See the License for the specific language governing permissions and |
12 | | -// limitations under the License. |
| 1 | +/** |
| 2 | + * Copyright 2016, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
13 | 15 |
|
14 | 16 | 'use strict'; |
15 | 17 |
|
16 | | -var ISO6391 = require('iso-639-1'); |
17 | | -var program = require('../translate'); |
| 18 | +const Translate = require(`@google-cloud/translate`); |
| 19 | +const path = require(`path`); |
| 20 | +const run = require(`../../utils`).run; |
18 | 21 |
|
19 | | -var text = 'Hello world!'; |
20 | | -var toLang = 'ru'; |
| 22 | +const cwd = path.join(__dirname, `..`); |
| 23 | +const cmd = `node translate.js`; |
| 24 | +const text = `Hello world!`; |
| 25 | +const toLang = `ru`; |
21 | 26 |
|
22 | | -describe('translate:translate', function () { |
| 27 | +describe(`translate:translate`, () => { |
| 28 | + const translate = Translate({ |
| 29 | + key: process.env.TRANSLATE_API_KEY |
| 30 | + }); |
23 | 31 | if (!process.env.TRANSLATE_API_KEY) { |
24 | | - process.stdout.write('Skipping Translate API tests...\n'); |
| 32 | + process.stdout.write(`Skipping Translate API tests...\n`); |
25 | 33 | return; |
26 | 34 | } |
27 | 35 |
|
28 | | - describe('detectLanguage', function () { |
29 | | - it('should detect language', function (done) { |
30 | | - program.detectLanguage(text, function (err, result, apiResponse) { |
31 | | - assert.equal(err, null); |
32 | | - assert.notEqual(result, undefined); |
33 | | - assert.equal(result.language, 'en', 'should have detected english'); |
34 | | - assert.equal(console.log.calledOnce, true); |
35 | | - assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]); |
36 | | - assert.notEqual(apiResponse, undefined); |
37 | | - done(); |
38 | | - }); |
| 36 | + it(`should detect language`, (done) => { |
| 37 | + 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(); |
39 | 42 | }); |
40 | 43 | }); |
41 | 44 |
|
42 | | - describe('listLanguages', function () { |
43 | | - it('should list languages', function (done) { |
44 | | - program.listLanguages(function (err, languages, apiResponse) { |
45 | | - assert.equal(err, null); |
46 | | - assert.equal(Array.isArray(languages), true); |
47 | | - assert.equal(languages.length > 0, true); |
48 | | - var matchingLanguages = languages.filter(function (language) { |
49 | | - return language.code === 'af' && language.name === 'Afrikaans'; |
50 | | - }); |
51 | | - assert.equal(matchingLanguages.length, 1, 'found language with name in English'); |
52 | | - assert.equal(console.log.calledOnce, true); |
53 | | - assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]); |
54 | | - assert.notEqual(apiResponse, undefined); |
55 | | - done(); |
56 | | - }); |
57 | | - }); |
| 45 | + it(`should list languages`, () => { |
| 46 | + const output = run(`${cmd} list`, cwd); |
| 47 | + assert.notEqual(output.indexOf(`Languages:`), -1); |
| 48 | + assert.notEqual(output.indexOf(`{ code: 'af', name: 'Afrikaans' }`), -1); |
58 | 49 | }); |
59 | 50 |
|
60 | | - describe('listLanguagesWithTarget', function () { |
61 | | - it('should list languages with a target', function (done) { |
62 | | - program.listLanguagesWithTarget('es', function (err, languages, apiResponse) { |
63 | | - assert.equal(err, null); |
64 | | - assert.equal(Array.isArray(languages), true); |
65 | | - assert.equal(languages.length > 0, true); |
66 | | - var matchingLanguages = languages.filter(function (language) { |
67 | | - return language.code === 'af' && language.name === 'afrikáans'; |
68 | | - }); |
69 | | - assert.equal(matchingLanguages.length, 1, 'found language with name in Spanish'); |
70 | | - assert.equal(console.log.calledOnce, true); |
71 | | - assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]); |
72 | | - assert.notEqual(apiResponse, undefined); |
73 | | - done(); |
74 | | - }); |
75 | | - }); |
| 51 | + it(`should list languages with a target`, () => { |
| 52 | + 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); |
76 | 55 | }); |
77 | 56 |
|
78 | | - describe('translateText', function () { |
79 | | - it('should translate text', function (done) { |
80 | | - var expected = 'Привет мир!'; |
81 | | - |
82 | | - program.translateText(text, toLang, undefined, function (err, translation, apiResponse) { |
83 | | - assert.equal(err, null); |
84 | | - assert.equal(translation, expected); |
85 | | - assert.equal(console.log.calledOnce, true); |
86 | | - assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]); |
87 | | - assert.notEqual(apiResponse, undefined); |
88 | | - done(); |
89 | | - }); |
| 57 | + it(`should translate text`, (done) => { |
| 58 | + 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(); |
90 | 64 | }); |
91 | 65 | }); |
92 | 66 | }); |
0 commit comments