fix(mock.$log): keep in sync with $log

Closes #2343
This commit is contained in:
Chirayu Krishnappa 2013-07-31 11:53:02 -07:00
parent 664526d69c
commit f274c0a66b
3 changed files with 159 additions and 88 deletions

View file

@ -72,16 +72,6 @@ function $LogProvider(){
*/
log: consoleLog('log'),
/**
* @ngdoc method
* @name ng.$log#warn
* @methodOf ng.$log
*
* @description
* Write a warning message
*/
warn: consoleLog('warn'),
/**
* @ngdoc method
* @name ng.$log#info
@ -92,6 +82,16 @@ function $LogProvider(){
*/
info: consoleLog('info'),
/**
* @ngdoc method
* @name ng.$log#warn
* @methodOf ng.$log
*
* @description
* Write a warning message
*/
warn: consoleLog('warn'),
/**
* @ngdoc method
* @name ng.$log#error

View file

@ -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));
}
}
};
/**
@ -347,21 +361,6 @@ angular.mock.$LogProvider = function() {
* </pre>
*/
$log.log.logs = [];
/**
* @ngdoc property
* @name ngMock.$log#warn.logs
* @propertyOf ngMock.$log
*
* @description
* Array of messages logged using {@link ngMock.$log#warn}.
*
* @example
* <pre>
* $log.warn('Some Warning');
* var first = $log.warn.logs.unshift();
* </pre>
*/
$log.warn.logs = [];
/**
* @ngdoc property
* @name ngMock.$log#info.logs
@ -377,6 +376,21 @@ angular.mock.$LogProvider = function() {
* </pre>
*/
$log.info.logs = [];
/**
* @ngdoc property
* @name ngMock.$log#warn.logs
* @propertyOf ngMock.$log
*
* @description
* Array of messages logged using {@link ngMock.$log#warn}.
*
* @example
* <pre>
* $log.warn('Some Warning');
* var first = $log.warn.logs.unshift();
* </pre>
*/
$log.warn.logs = [];
/**
* @ngdoc property
* @name ngMock.$log#error.logs
@ -392,6 +406,21 @@ angular.mock.$LogProvider = function() {
* </pre>
*/
$log.error.logs = [];
/**
* @ngdoc property
* @name ngMock.$log#debug.logs
* @propertyOf ngMock.$log
*
* @description
* Array of messages logged using {@link ngMock.$log#debug}.
*
* @example
* <pre>
* $log.debug('Some Error');
* var first = $log.debug.logs.unshift();
* </pre>
*/
$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 || ''));

View file

@ -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);
});
});
});