From f274c0a66b28711d3b9cc7b0775e97755dd971e8 Mon Sep 17 00:00:00 2001 From: Chirayu Krishnappa Date: Wed, 31 Jul 2013 11:53:02 -0700 Subject: [PATCH] fix(mock.$log): keep in sync with $log Closes #2343 --- src/ng/log.js | 12 +-- src/ngMock/angular-mocks.js | 53 +++++++--- test/ngMock/angular-mocksSpec.js | 164 +++++++++++++++++++------------ 3 files changed, 150 insertions(+), 79 deletions(-) diff --git a/src/ng/log.js b/src/ng/log.js index 2a58d442399a..e0a1aec363f7 100644 --- a/src/ng/log.js +++ b/src/ng/log.js @@ -74,23 +74,23 @@ function $LogProvider(){ /** * @ngdoc method - * @name ng.$log#warn + * @name ng.$log#info * @methodOf ng.$log * * @description - * Write a warning message + * Write an information message */ - warn: consoleLog('warn'), + info: consoleLog('info'), /** * @ngdoc method - * @name ng.$log#info + * @name ng.$log#warn * @methodOf ng.$log * * @description - * Write an information message + * Write a warning message */ - info: consoleLog('info'), + warn: consoleLog('warn'), /** * @ngdoc method diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index bfb601fdf62b..f7a9fec78bc7 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -309,18 +309,32 @@ angular.mock.$ExceptionHandlerProvider = function() { * */ angular.mock.$LogProvider = function() { + var debug = true; function concat(array1, array2, index) { return array1.concat(Array.prototype.slice.call(array2, index)); } + this.debugEnabled = function(flag) { + if (isDefined(flag)) { + debug = flag; + return this; + } else { + return debug; + } + }; this.$get = function () { var $log = { log: function() { $log.log.logs.push(concat([], arguments, 0)); }, warn: function() { $log.warn.logs.push(concat([], arguments, 0)); }, info: function() { $log.info.logs.push(concat([], arguments, 0)); }, - error: function() { $log.error.logs.push(concat([], arguments, 0)); } + error: function() { $log.error.logs.push(concat([], arguments, 0)); }, + debug: function() { + if (debug) { + $log.debug.logs.push(concat([], arguments, 0)); + } + } }; /** @@ -349,34 +363,34 @@ angular.mock.$LogProvider = function() { $log.log.logs = []; /** * @ngdoc property - * @name ngMock.$log#warn.logs + * @name ngMock.$log#info.logs * @propertyOf ngMock.$log * * @description - * Array of messages logged using {@link ngMock.$log#warn}. + * Array of messages logged using {@link ngMock.$log#info}. * * @example *
-       * $log.warn('Some Warning');
-       * var first = $log.warn.logs.unshift();
+       * $log.info('Some Info');
+       * var first = $log.info.logs.unshift();
        * 
*/ - $log.warn.logs = []; + $log.info.logs = []; /** * @ngdoc property - * @name ngMock.$log#info.logs + * @name ngMock.$log#warn.logs * @propertyOf ngMock.$log * * @description - * Array of messages logged using {@link ngMock.$log#info}. + * Array of messages logged using {@link ngMock.$log#warn}. * * @example *
-       * $log.info('Some Info');
-       * var first = $log.info.logs.unshift();
+       * $log.warn('Some Warning');
+       * var first = $log.warn.logs.unshift();
        * 
*/ - $log.info.logs = []; + $log.warn.logs = []; /** * @ngdoc property * @name ngMock.$log#error.logs @@ -392,6 +406,21 @@ angular.mock.$LogProvider = function() { * */ $log.error.logs = []; + /** + * @ngdoc property + * @name ngMock.$log#debug.logs + * @propertyOf ngMock.$log + * + * @description + * Array of messages logged using {@link ngMock.$log#debug}. + * + * @example + *
+       * $log.debug('Some Error');
+       * var first = $log.debug.logs.unshift();
+       * 
+ */ + $log.debug.logs = [] }; /** @@ -404,7 +433,7 @@ angular.mock.$LogProvider = function() { */ $log.assertEmpty = function() { var errors = []; - angular.forEach(['error', 'warn', 'info', 'log'], function(logLevel) { + angular.forEach(['error', 'warn', 'info', 'log', 'debug'], function(logLevel) { angular.forEach($log[logLevel].logs, function(log) { angular.forEach(log, function (logItem) { errors.push('MOCK $log (' + logLevel + '): ' + String(logItem) + '\n' + (logItem.stack || '')); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 4d2388919a68..9002d854ee61 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -158,83 +158,125 @@ describe('ngMock', function() { describe('$log', function() { - var $log; - beforeEach(inject(['$log', function(log) { - $log = log; - }])); + forEach([true, false], function(debugEnabled) { + describe('debug ' + debugEnabled, function() { + beforeEach(module(function($logProvider) { + $logProvider.debugEnabled(debugEnabled); + })); - afterEach(inject(function($log){ - $log.reset(); - })); + afterEach(inject(function($log){ + $log.reset(); + })); - it('should provide log method', function() { - expect(function() { $log.log(''); }).not.toThrow(); + it("should skip debugging output if disabled", inject(function($log) { + $log.log('fake log'); + $log.info('fake log'); + $log.warn('fake log'); + $log.error('fake log'); + $log.debug('fake log'); + expect($log.log.logs).toContain(['fake log']); + expect($log.info.logs).toContain(['fake log']); + expect($log.warn.logs).toContain(['fake log']); + expect($log.error.logs).toContain(['fake log']); + if (debugEnabled) { + expect($log.debug.logs).toContain(['fake log']); + } else { + expect($log.debug.logs).toEqual([]); + } + })); + }); }); - it('should provide info method', function() { - expect(function() { $log.info(''); }).not.toThrow(); - }); + describe('debug enabled (default)', function() { + var $log; + beforeEach(inject(['$log', function(log) { + $log = log; + }])); - it('should provide warn method', function() { - expect(function() { $log.warn(''); }).not.toThrow(); - }); + afterEach(inject(function($log){ + $log.reset(); + })); - it('should provide error method', function() { - expect(function() { $log.error(''); }).not.toThrow(); - }); + it('should provide the debug method', function() { + expect(function() { $log.log(''); }).not.toThrow(); + }); - it('should store log messages', function() { - $log.log('fake log'); - expect($log.log.logs).toContain(['fake log']); - }); + it('should provide the debug method', function() { + expect(function() { $log.info(''); }).not.toThrow(); + }); - it('should store info messages', function() { - $log.info('fake log'); - expect($log.info.logs).toContain(['fake log']); - }); + it('should provide the debug method', function() { + expect(function() { $log.warn(''); }).not.toThrow(); + }); - it('should store warn messages', function() { - $log.warn('fake log'); - expect($log.warn.logs).toContain(['fake log']); - }); + it('should provide the debug method', function() { + expect(function() { $log.error(''); }).not.toThrow(); + }); - it('should store error messages', function() { - $log.error('fake log'); - expect($log.error.logs).toContain(['fake log']); - }); + it('should provide the debug method', function() { + expect(function() { $log.debug(''); }).not.toThrow(); + }); - it('should assertEmpty', function(){ - try { + it('should store log messages', function() { + $log.log('fake log'); + expect($log.log.logs).toContain(['fake log']); + }); + + it('should store info messages', function() { + $log.info('fake log'); + expect($log.info.logs).toContain(['fake log']); + }); + + it('should store warn messages', function() { + $log.warn('fake log'); + expect($log.warn.logs).toContain(['fake log']); + }); + + it('should store error messages', function() { + $log.error('fake log'); + expect($log.error.logs).toContain(['fake log']); + }); + + it('should store debug messages', function() { + $log.debug('fake log'); + expect($log.debug.logs).toContain(['fake log']); + }); + + it('should assertEmpty', function(){ + try { + $log.error(Error('MyError')); + $log.warn(Error('MyWarn')); + $log.info(Error('MyInfo')); + $log.log(Error('MyLog')); + $log.debug(Error('MyDebug')); + $log.assertEmpty(); + } catch (error) { + error = error.message || error; + expect(error).toMatch(/Error: MyError/m); + expect(error).toMatch(/Error: MyWarn/m); + expect(error).toMatch(/Error: MyInfo/m); + expect(error).toMatch(/Error: MyLog/m); + expect(error).toMatch(/Error: MyDebug/m); + } finally { + $log.reset(); + } + }); + + it('should reset state', function(){ $log.error(Error('MyError')); $log.warn(Error('MyWarn')); $log.info(Error('MyInfo')); $log.log(Error('MyLog')); - $log.assertEmpty(); - } catch (error) { - error = error.message || error; - expect(error).toMatch(/Error: MyError/m); - expect(error).toMatch(/Error: MyWarn/m); - expect(error).toMatch(/Error: MyInfo/m); - expect(error).toMatch(/Error: MyLog/m); - } finally { $log.reset(); - } - }); - - it('should reset state', function(){ - $log.error(Error('MyError')); - $log.warn(Error('MyWarn')); - $log.info(Error('MyInfo')); - $log.log(Error('MyLog')); - $log.reset(); - var passed = false; - try { - $log.assertEmpty(); // should not throw error! - passed = true; - } catch (e) { - passed = e; - } - expect(passed).toBe(true); + var passed = false; + try { + $log.assertEmpty(); // should not throw error! + passed = true; + } catch (e) { + passed = e; + } + expect(passed).toBe(true); + }); }); });