diff --git a/lib/language/index.js b/lib/language/index.js index 636824ad70bb..aa2d4ff60d44 100644 --- a/lib/language/index.js +++ b/lib/language/index.js @@ -165,7 +165,9 @@ Language.prototype.annotate = function(content, options, callback) { options = {}; } - options = extend({}, options, { content: content }); + options = extend({}, options, { + content: content + }); var document = this.document(options); document.annotate(options, callback); @@ -234,7 +236,9 @@ Language.prototype.detectEntities = function(content, options, callback) { options = {}; } - options = extend({}, options, { content: content }); + options = extend({}, options, { + content: content + }); var document = this.document(options); document.detectEntities(options, callback); @@ -303,9 +307,11 @@ Language.prototype.detectSentiment = function(content, options, callback) { options = {}; } - options = extend({}, options, { content: content }); + options = extend({}, options, { + content: content + }); - var document = this.document(content, options); + var document = this.document(options); document.detectSentiment(options, callback); }; diff --git a/test/language/document.js b/test/language/document.js index 6903212ea7a3..fb5add7b7a94 100644 --- a/test/language/document.js +++ b/test/language/document.js @@ -25,7 +25,7 @@ var util = require('../../lib/common/util.js'); function FakeFile() {} -describe.only('Document', function() { +describe('Document', function() { var DocumentCache; var Document; var document; diff --git a/test/language/index.js b/test/language/index.js index e69de29bb2d1..3c93d232516e 100644 --- a/test/language/index.js +++ b/test/language/index.js @@ -0,0 +1,309 @@ +/** + * Copyright 2016 Google Inc. All Rights Reserved. + * + * 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 assert = require('assert'); +var extend = require('extend'); +var googleProtoFiles = require('google-proto-files'); +var mockery = require('mockery-next'); + +var util = require('../../lib/common/util.js'); + +var fakeUtil = extend(true, {}, util); + +function FakeDocument() { + this.calledWith_ = arguments; +} + +function FakeGrpcService() { + this.calledWith_ = arguments; +} + +describe('Language', function() { + var Language; + var language; + + var OPTIONS = {}; + + before(function() { + mockery.registerMock('../../lib/common/grpc-service.js', FakeGrpcService); + mockery.registerMock('../../lib/common/util.js', fakeUtil); + mockery.registerMock('../../lib/language/document.js', FakeDocument); + + mockery.enable({ + useCleanCache: true, + warnOnUnregistered: false + }); + + Language = require('../../lib/language/index.js'); + }); + + after(function() { + mockery.deregisterAll(); + mockery.disable(); + }); + + beforeEach(function() { + language = new Language(OPTIONS); + }); + + describe('instantiation', function() { + it('should normalize the arguments', function() { + var options = { + projectId: 'project-id', + credentials: 'credentials', + email: 'email', + keyFilename: 'keyFile' + }; + + var normalizeArguments = fakeUtil.normalizeArguments; + var normalizeArgumentsCalled = false; + var fakeContext = {}; + + fakeUtil.normalizeArguments = function(context, options_) { + normalizeArgumentsCalled = true; + assert.strictEqual(context, fakeContext); + assert.strictEqual(options, options_); + return options_; + }; + + Language.call(fakeContext, options); + assert(normalizeArgumentsCalled); + + fakeUtil.normalizeArguments = normalizeArguments; + }); + + it('should inherit from GrpcService', function() { + assert(language instanceof FakeGrpcService); + + var calledWith = language.calledWith_[0]; + + assert.deepEqual(calledWith, { + baseUrl: 'language.googleapis.com', + service: 'language', + apiVersion: 'v1beta1', + protoServices: { + LanguageService: { + path: googleProtoFiles.language.v1beta1, + service: 'cloud.language' + } + }, + scopes: [ + 'https://www.googleapis.com/auth/cloud-platform' + ] + }); + }); + }); + + describe('annotate', function() { + var CONTENT = '...'; + var OPTIONS = { + property: 'value' + }; + + var EXPECTED_OPTIONS = { + withCustomOptions: extend({}, OPTIONS, { + content: CONTENT + }), + + withoutCustomOptions: extend({}, { + content: CONTENT + }) + }; + + it('should call annotate on a Document', function(done) { + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS.withCustomOptions); + + return { + annotate: function(options, callback) { + assert.deepEqual(options, EXPECTED_OPTIONS.withCustomOptions); + callback(); // done() + } + }; + }; + + language.annotate(CONTENT, OPTIONS, done); + }); + + it('should not require options', function(done) { + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS.withoutCustomOptions); + + return { + annotate: function(options, callback) { + assert.deepEqual(options, EXPECTED_OPTIONS.withoutCustomOptions); + callback(); // done() + } + }; + }; + + language.annotate(CONTENT, done); + }); + }); + + describe('detectEntities', function() { + var CONTENT = '...'; + var OPTIONS = { + property: 'value' + }; + + var EXPECTED_OPTIONS = { + withCustomOptions: extend({}, OPTIONS, { + content: CONTENT + }), + + withoutCustomOptions: extend({}, { + content: CONTENT + }) + }; + + it('should call detectEntities on a Document', function(done) { + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS.withCustomOptions); + + return { + detectEntities: function(options, callback) { + assert.deepEqual(options, EXPECTED_OPTIONS.withCustomOptions); + callback(); // done() + } + }; + }; + + language.detectEntities(CONTENT, OPTIONS, done); + }); + + it('should not require options', function(done) { + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS.withoutCustomOptions); + + return { + detectEntities: function(options, callback) { + assert.deepEqual(options, EXPECTED_OPTIONS.withoutCustomOptions); + callback(); // done() + } + }; + }; + + language.detectEntities(CONTENT, done); + }); + }); + + describe('detectSentiment', function() { + var CONTENT = '...'; + var OPTIONS = { + property: 'value' + }; + + var EXPECTED_OPTIONS = { + withCustomOptions: extend({}, OPTIONS, { + content: CONTENT + }), + + withoutCustomOptions: extend({}, { + content: CONTENT + }) + }; + + it('should call detectSentiment on a Document', function(done) { + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS.withCustomOptions); + + return { + detectSentiment: function(options, callback) { + assert.deepEqual(options, EXPECTED_OPTIONS.withCustomOptions); + callback(); // done() + } + }; + }; + + language.detectSentiment(CONTENT, OPTIONS, done); + }); + + it('should not require options', function(done) { + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS.withoutCustomOptions); + + return { + detectSentiment: function(options, callback) { + assert.deepEqual(options, EXPECTED_OPTIONS.withoutCustomOptions); + callback(); // done() + } + }; + }; + + language.detectSentiment(CONTENT, done); + }); + }); + + describe('document', function() { + var CONFIG = {}; + + it('should create a Document', function() { + var document = language.document(CONFIG); + + assert.strictEqual(document.calledWith_[0], language); + assert.strictEqual(document.calledWith_[1], CONFIG); + }); + }); + + describe('html', function() { + var CONTENT = '...'; + var OPTIONS = { + property: 'value' + }; + + var EXPECTED_OPTIONS = extend({}, OPTIONS, { + type: 'HTML', + content: CONTENT + }); + + it('should create a Document', function() { + var document = {}; + + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS); + return document; + }; + + assert.strictEqual(language.html(CONTENT, OPTIONS), document); + }); + }); + + describe('text', function() { + var CONTENT = '...'; + var OPTIONS = { + property: 'value' + }; + + var EXPECTED_OPTIONS = extend({}, OPTIONS, { + type: 'PLAIN_TEXT', + content: CONTENT + }); + + it('should create a Document', function() { + var document = {}; + + language.document = function(options) { + assert.deepEqual(options, EXPECTED_OPTIONS); + return document; + }; + + assert.strictEqual(language.text(CONTENT, OPTIONS), document); + }); + }); +});