Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(mock.$log): keep in sync with $log
Browse files Browse the repository at this point in the history
Closes #2343
  • Loading branch information
chirayuk committed Jul 31, 2013
1 parent 664526d commit f274c0a
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 79 deletions.
12 changes: 6 additions & 6 deletions src/ng/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 41 additions & 12 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
};

/**
Expand Down Expand Up @@ -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
* <pre>
* $log.warn('Some Warning');
* var first = $log.warn.logs.unshift();
* $log.info('Some Info');
* var first = $log.info.logs.unshift();
* </pre>
*/
$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
* <pre>
* $log.info('Some Info');
* var first = $log.info.logs.unshift();
* $log.warn('Some Warning');
* var first = $log.warn.logs.unshift();
* </pre>
*/
$log.info.logs = [];
$log.warn.logs = [];
/**
* @ngdoc property
* @name ngMock.$log#error.logs
Expand All @@ -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 = []
};

/**
Expand All @@ -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 || ''));
Expand Down
164 changes: 103 additions & 61 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit f274c0a

Please sign in to comment.