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

Commit f274c0a

Browse files
committed
fix(mock.$log): keep in sync with $log
Closes #2343
1 parent 664526d commit f274c0a

File tree

3 files changed

+150
-79
lines changed

3 files changed

+150
-79
lines changed

src/ng/log.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ function $LogProvider(){
7474

7575
/**
7676
* @ngdoc method
77-
* @name ng.$log#warn
77+
* @name ng.$log#info
7878
* @methodOf ng.$log
7979
*
8080
* @description
81-
* Write a warning message
81+
* Write an information message
8282
*/
83-
warn: consoleLog('warn'),
83+
info: consoleLog('info'),
8484

8585
/**
8686
* @ngdoc method
87-
* @name ng.$log#info
87+
* @name ng.$log#warn
8888
* @methodOf ng.$log
8989
*
9090
* @description
91-
* Write an information message
91+
* Write a warning message
9292
*/
93-
info: consoleLog('info'),
93+
warn: consoleLog('warn'),
9494

9595
/**
9696
* @ngdoc method

src/ngMock/angular-mocks.js

+41-12
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,32 @@ angular.mock.$ExceptionHandlerProvider = function() {
309309
*
310310
*/
311311
angular.mock.$LogProvider = function() {
312+
var debug = true;
312313

313314
function concat(array1, array2, index) {
314315
return array1.concat(Array.prototype.slice.call(array2, index));
315316
}
316317

318+
this.debugEnabled = function(flag) {
319+
if (isDefined(flag)) {
320+
debug = flag;
321+
return this;
322+
} else {
323+
return debug;
324+
}
325+
};
317326

318327
this.$get = function () {
319328
var $log = {
320329
log: function() { $log.log.logs.push(concat([], arguments, 0)); },
321330
warn: function() { $log.warn.logs.push(concat([], arguments, 0)); },
322331
info: function() { $log.info.logs.push(concat([], arguments, 0)); },
323-
error: function() { $log.error.logs.push(concat([], arguments, 0)); }
332+
error: function() { $log.error.logs.push(concat([], arguments, 0)); },
333+
debug: function() {
334+
if (debug) {
335+
$log.debug.logs.push(concat([], arguments, 0));
336+
}
337+
}
324338
};
325339

326340
/**
@@ -349,34 +363,34 @@ angular.mock.$LogProvider = function() {
349363
$log.log.logs = [];
350364
/**
351365
* @ngdoc property
352-
* @name ngMock.$log#warn.logs
366+
* @name ngMock.$log#info.logs
353367
* @propertyOf ngMock.$log
354368
*
355369
* @description
356-
* Array of messages logged using {@link ngMock.$log#warn}.
370+
* Array of messages logged using {@link ngMock.$log#info}.
357371
*
358372
* @example
359373
* <pre>
360-
* $log.warn('Some Warning');
361-
* var first = $log.warn.logs.unshift();
374+
* $log.info('Some Info');
375+
* var first = $log.info.logs.unshift();
362376
* </pre>
363377
*/
364-
$log.warn.logs = [];
378+
$log.info.logs = [];
365379
/**
366380
* @ngdoc property
367-
* @name ngMock.$log#info.logs
381+
* @name ngMock.$log#warn.logs
368382
* @propertyOf ngMock.$log
369383
*
370384
* @description
371-
* Array of messages logged using {@link ngMock.$log#info}.
385+
* Array of messages logged using {@link ngMock.$log#warn}.
372386
*
373387
* @example
374388
* <pre>
375-
* $log.info('Some Info');
376-
* var first = $log.info.logs.unshift();
389+
* $log.warn('Some Warning');
390+
* var first = $log.warn.logs.unshift();
377391
* </pre>
378392
*/
379-
$log.info.logs = [];
393+
$log.warn.logs = [];
380394
/**
381395
* @ngdoc property
382396
* @name ngMock.$log#error.logs
@@ -392,6 +406,21 @@ angular.mock.$LogProvider = function() {
392406
* </pre>
393407
*/
394408
$log.error.logs = [];
409+
/**
410+
* @ngdoc property
411+
* @name ngMock.$log#debug.logs
412+
* @propertyOf ngMock.$log
413+
*
414+
* @description
415+
* Array of messages logged using {@link ngMock.$log#debug}.
416+
*
417+
* @example
418+
* <pre>
419+
* $log.debug('Some Error');
420+
* var first = $log.debug.logs.unshift();
421+
* </pre>
422+
*/
423+
$log.debug.logs = []
395424
};
396425

397426
/**
@@ -404,7 +433,7 @@ angular.mock.$LogProvider = function() {
404433
*/
405434
$log.assertEmpty = function() {
406435
var errors = [];
407-
angular.forEach(['error', 'warn', 'info', 'log'], function(logLevel) {
436+
angular.forEach(['error', 'warn', 'info', 'log', 'debug'], function(logLevel) {
408437
angular.forEach($log[logLevel].logs, function(log) {
409438
angular.forEach(log, function (logItem) {
410439
errors.push('MOCK $log (' + logLevel + '): ' + String(logItem) + '\n' + (logItem.stack || ''));

test/ngMock/angular-mocksSpec.js

+103-61
Original file line numberDiff line numberDiff line change
@@ -158,83 +158,125 @@ describe('ngMock', function() {
158158

159159

160160
describe('$log', function() {
161-
var $log;
162-
beforeEach(inject(['$log', function(log) {
163-
$log = log;
164-
}]));
161+
forEach([true, false], function(debugEnabled) {
162+
describe('debug ' + debugEnabled, function() {
163+
beforeEach(module(function($logProvider) {
164+
$logProvider.debugEnabled(debugEnabled);
165+
}));
165166

166-
afterEach(inject(function($log){
167-
$log.reset();
168-
}));
167+
afterEach(inject(function($log){
168+
$log.reset();
169+
}));
169170

170-
it('should provide log method', function() {
171-
expect(function() { $log.log(''); }).not.toThrow();
171+
it("should skip debugging output if disabled", inject(function($log) {
172+
$log.log('fake log');
173+
$log.info('fake log');
174+
$log.warn('fake log');
175+
$log.error('fake log');
176+
$log.debug('fake log');
177+
expect($log.log.logs).toContain(['fake log']);
178+
expect($log.info.logs).toContain(['fake log']);
179+
expect($log.warn.logs).toContain(['fake log']);
180+
expect($log.error.logs).toContain(['fake log']);
181+
if (debugEnabled) {
182+
expect($log.debug.logs).toContain(['fake log']);
183+
} else {
184+
expect($log.debug.logs).toEqual([]);
185+
}
186+
}));
187+
});
172188
});
173189

174-
it('should provide info method', function() {
175-
expect(function() { $log.info(''); }).not.toThrow();
176-
});
190+
describe('debug enabled (default)', function() {
191+
var $log;
192+
beforeEach(inject(['$log', function(log) {
193+
$log = log;
194+
}]));
177195

178-
it('should provide warn method', function() {
179-
expect(function() { $log.warn(''); }).not.toThrow();
180-
});
196+
afterEach(inject(function($log){
197+
$log.reset();
198+
}));
181199

182-
it('should provide error method', function() {
183-
expect(function() { $log.error(''); }).not.toThrow();
184-
});
200+
it('should provide the debug method', function() {
201+
expect(function() { $log.log(''); }).not.toThrow();
202+
});
185203

186-
it('should store log messages', function() {
187-
$log.log('fake log');
188-
expect($log.log.logs).toContain(['fake log']);
189-
});
204+
it('should provide the debug method', function() {
205+
expect(function() { $log.info(''); }).not.toThrow();
206+
});
190207

191-
it('should store info messages', function() {
192-
$log.info('fake log');
193-
expect($log.info.logs).toContain(['fake log']);
194-
});
208+
it('should provide the debug method', function() {
209+
expect(function() { $log.warn(''); }).not.toThrow();
210+
});
195211

196-
it('should store warn messages', function() {
197-
$log.warn('fake log');
198-
expect($log.warn.logs).toContain(['fake log']);
199-
});
212+
it('should provide the debug method', function() {
213+
expect(function() { $log.error(''); }).not.toThrow();
214+
});
200215

201-
it('should store error messages', function() {
202-
$log.error('fake log');
203-
expect($log.error.logs).toContain(['fake log']);
204-
});
216+
it('should provide the debug method', function() {
217+
expect(function() { $log.debug(''); }).not.toThrow();
218+
});
205219

206-
it('should assertEmpty', function(){
207-
try {
220+
it('should store log messages', function() {
221+
$log.log('fake log');
222+
expect($log.log.logs).toContain(['fake log']);
223+
});
224+
225+
it('should store info messages', function() {
226+
$log.info('fake log');
227+
expect($log.info.logs).toContain(['fake log']);
228+
});
229+
230+
it('should store warn messages', function() {
231+
$log.warn('fake log');
232+
expect($log.warn.logs).toContain(['fake log']);
233+
});
234+
235+
it('should store error messages', function() {
236+
$log.error('fake log');
237+
expect($log.error.logs).toContain(['fake log']);
238+
});
239+
240+
it('should store debug messages', function() {
241+
$log.debug('fake log');
242+
expect($log.debug.logs).toContain(['fake log']);
243+
});
244+
245+
it('should assertEmpty', function(){
246+
try {
247+
$log.error(Error('MyError'));
248+
$log.warn(Error('MyWarn'));
249+
$log.info(Error('MyInfo'));
250+
$log.log(Error('MyLog'));
251+
$log.debug(Error('MyDebug'));
252+
$log.assertEmpty();
253+
} catch (error) {
254+
error = error.message || error;
255+
expect(error).toMatch(/Error: MyError/m);
256+
expect(error).toMatch(/Error: MyWarn/m);
257+
expect(error).toMatch(/Error: MyInfo/m);
258+
expect(error).toMatch(/Error: MyLog/m);
259+
expect(error).toMatch(/Error: MyDebug/m);
260+
} finally {
261+
$log.reset();
262+
}
263+
});
264+
265+
it('should reset state', function(){
208266
$log.error(Error('MyError'));
209267
$log.warn(Error('MyWarn'));
210268
$log.info(Error('MyInfo'));
211269
$log.log(Error('MyLog'));
212-
$log.assertEmpty();
213-
} catch (error) {
214-
error = error.message || error;
215-
expect(error).toMatch(/Error: MyError/m);
216-
expect(error).toMatch(/Error: MyWarn/m);
217-
expect(error).toMatch(/Error: MyInfo/m);
218-
expect(error).toMatch(/Error: MyLog/m);
219-
} finally {
220270
$log.reset();
221-
}
222-
});
223-
224-
it('should reset state', function(){
225-
$log.error(Error('MyError'));
226-
$log.warn(Error('MyWarn'));
227-
$log.info(Error('MyInfo'));
228-
$log.log(Error('MyLog'));
229-
$log.reset();
230-
var passed = false;
231-
try {
232-
$log.assertEmpty(); // should not throw error!
233-
passed = true;
234-
} catch (e) {
235-
passed = e;
236-
}
237-
expect(passed).toBe(true);
271+
var passed = false;
272+
try {
273+
$log.assertEmpty(); // should not throw error!
274+
passed = true;
275+
} catch (e) {
276+
passed = e;
277+
}
278+
expect(passed).toBe(true);
279+
});
238280
});
239281
});
240282

0 commit comments

Comments
 (0)