|
| 1 | +// Copyright 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. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +var proxyquire = require('proxyquire').noCallThru(); |
| 17 | +var text = 'Hello world!'; |
| 18 | +var apiKey = 'key'; |
| 19 | + |
| 20 | +function getSample () { |
| 21 | + var languagesMock = [ |
| 22 | + 'en', |
| 23 | + 'ru' |
| 24 | + ]; |
| 25 | + var resultMock = { |
| 26 | + language: 'en', |
| 27 | + confidence: 0.75, |
| 28 | + input: text |
| 29 | + }; |
| 30 | + var translationMock = 'Привет мир!'; |
| 31 | + var translateMock = { |
| 32 | + getLanguages: sinon.stub().callsArgWith(0, null, languagesMock), |
| 33 | + detect: sinon.stub().callsArgWith(1, null, resultMock), |
| 34 | + translate: sinon.stub().callsArgWith(2, null, translationMock) |
| 35 | + }; |
| 36 | + var TranslateMock = sinon.stub().returns(translateMock); |
| 37 | + |
| 38 | + return { |
| 39 | + program: proxyquire('../translate', { |
| 40 | + '@google-cloud/translate': TranslateMock, |
| 41 | + yargs: proxyquire('yargs', {}) |
| 42 | + }), |
| 43 | + mocks: { |
| 44 | + Translate: TranslateMock, |
| 45 | + translate: translateMock, |
| 46 | + languages: languagesMock, |
| 47 | + result: resultMock, |
| 48 | + translation: translationMock |
| 49 | + } |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +describe('translate:translate', function () { |
| 54 | + describe('detectLanguage', function () { |
| 55 | + it('should detect language', function () { |
| 56 | + var sample = getSample(); |
| 57 | + var callback = sinon.stub(); |
| 58 | + |
| 59 | + sample.program.detectLanguage(text, apiKey, callback); |
| 60 | + |
| 61 | + assert(sample.mocks.translate.detect.calledOnce, 'method called once'); |
| 62 | + assert.equal(sample.mocks.translate.detect.firstCall.args.length, 2, 'method received 2 arguments'); |
| 63 | + assert.equal(sample.mocks.translate.detect.firstCall.args[0], text, 'method received correct argument'); |
| 64 | + assert(callback.calledOnce, 'callback called once'); |
| 65 | + assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments'); |
| 66 | + assert.ifError(callback.firstCall.args[0], 'callback did not receive error'); |
| 67 | + assert.strictEqual(callback.firstCall.args[1], sample.mocks.result, 'callback received result'); |
| 68 | + assert(console.log.calledWith('Detected %s with confidence %d', 'English', sample.mocks.result.confidence)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should handle error', function () { |
| 72 | + var error = 'error'; |
| 73 | + var sample = getSample(); |
| 74 | + var callback = sinon.stub(); |
| 75 | + sample.mocks.translate.detect = sinon.stub().callsArgWith(1, error); |
| 76 | + |
| 77 | + sample.program.detectLanguage(text, apiKey, callback); |
| 78 | + |
| 79 | + assert(callback.calledOnce, 'callback called once'); |
| 80 | + assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument'); |
| 81 | + assert(callback.firstCall.args[0], 'callback received error'); |
| 82 | + assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('listLanguages', function () { |
| 87 | + it('should list languages', function () { |
| 88 | + var sample = getSample(); |
| 89 | + var callback = sinon.stub(); |
| 90 | + |
| 91 | + sample.program.listLanguages(apiKey, callback); |
| 92 | + |
| 93 | + assert(sample.mocks.translate.getLanguages.calledOnce, 'method called once'); |
| 94 | + assert.equal(sample.mocks.translate.getLanguages.firstCall.args.length, 1, 'method received 1 argument'); |
| 95 | + assert(callback.calledOnce, 'callback called once'); |
| 96 | + assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments'); |
| 97 | + assert.ifError(callback.firstCall.args[0], 'callback did not receive error'); |
| 98 | + assert.strictEqual(callback.firstCall.args[1], sample.mocks.languages, 'callback received result'); |
| 99 | + assert(console.log.calledWith('Found %d language(s)!', sample.mocks.languages.length)); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle error', function () { |
| 103 | + var error = 'error'; |
| 104 | + var sample = getSample(); |
| 105 | + var callback = sinon.stub(); |
| 106 | + sample.mocks.translate.getLanguages = sinon.stub().callsArgWith(0, error); |
| 107 | + |
| 108 | + sample.program.listLanguages(apiKey, callback); |
| 109 | + |
| 110 | + assert(callback.calledOnce, 'callback called once'); |
| 111 | + assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument'); |
| 112 | + assert(callback.firstCall.args[0], 'callback received error'); |
| 113 | + assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('translateText', function () { |
| 118 | + it('should translate text', function () { |
| 119 | + var sample = getSample(); |
| 120 | + var callback = sinon.stub(); |
| 121 | + var options = { |
| 122 | + text: text, |
| 123 | + to: 'ru', |
| 124 | + apiKey: apiKey |
| 125 | + }; |
| 126 | + |
| 127 | + sample.program.translateText(options, callback); |
| 128 | + |
| 129 | + assert(sample.mocks.translate.translate.calledOnce, 'method called once'); |
| 130 | + assert.equal(sample.mocks.translate.translate.firstCall.args.length, 3, 'method received 3 arguments'); |
| 131 | + assert.equal(sample.mocks.translate.translate.firstCall.args[0], text, 'method received correct first argument'); |
| 132 | + assert.deepEqual(sample.mocks.translate.translate.firstCall.args[1], { |
| 133 | + to: 'ru', |
| 134 | + from: undefined |
| 135 | + }, 'method received correct second argument'); |
| 136 | + assert(callback.calledOnce, 'callback called once'); |
| 137 | + assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments'); |
| 138 | + assert.ifError(callback.firstCall.args[0], 'callback did not receive error'); |
| 139 | + assert.strictEqual(callback.firstCall.args[1], sample.mocks.translation, 'callback received result'); |
| 140 | + assert(console.log.calledWith('Translated text to %s', 'Russian')); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle error', function () { |
| 144 | + var error = 'error'; |
| 145 | + var sample = getSample(); |
| 146 | + var callback = sinon.stub(); |
| 147 | + var options = { |
| 148 | + text: text, |
| 149 | + to: 'ru', |
| 150 | + apiKey: apiKey |
| 151 | + }; |
| 152 | + sample.mocks.translate.translate = sinon.stub().callsArgWith(2, error); |
| 153 | + |
| 154 | + sample.program.translateText(options, callback); |
| 155 | + |
| 156 | + assert(callback.calledOnce, 'callback called once'); |
| 157 | + assert.equal(callback.firstCall.args.length, 1, 'callback received 1 argument'); |
| 158 | + assert(callback.firstCall.args[0], 'callback received error'); |
| 159 | + assert.equal(callback.firstCall.args[0].message, error.message, 'error has correct message'); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('main', function () { |
| 164 | + it('should call detectLanguage', function () { |
| 165 | + var program = getSample().program; |
| 166 | + |
| 167 | + sinon.stub(program, 'detectLanguage'); |
| 168 | + program.main(['detect', text, '-k', apiKey]); |
| 169 | + assert(program.detectLanguage.calledOnce); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should call listLanguages', function () { |
| 173 | + var program = getSample().program; |
| 174 | + |
| 175 | + sinon.stub(program, 'listLanguages'); |
| 176 | + program.main(['list', '-k', apiKey]); |
| 177 | + assert(program.listLanguages.calledOnce); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should call translateText', function () { |
| 181 | + var program = getSample().program; |
| 182 | + |
| 183 | + sinon.stub(program, 'translateText'); |
| 184 | + program.main(['translate', text, '-k', apiKey, '-t', 'ru']); |
| 185 | + assert(program.translateText.calledOnce); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments