Skip to content

Commit

Permalink
Fixed bug in supplant where it would incorrectly interpret {} in angu…
Browse files Browse the repository at this point in the history
…lar error messages as an attempt to supplant undefined data
  • Loading branch information
Andrew Ramsay committed Feb 26, 2016
1 parent a2be3de commit c2da823
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/core/logger/logger.js
Expand Up @@ -24,11 +24,14 @@

var proto = AvLogger.prototype;

AvLogger.supplant = function(str, o) {
AvLogger.supplant = function(str, supplantData) {
if (!supplantData) {
return str;
}

var _supplant = function (a, b) {
var r = o[b];
return r;
var _supplant = function (match, key) {
var result = supplantData[key];
return result;
};

return str.replace(/\{([^{}]*)\}/g, _supplant);
Expand Down
84 changes: 84 additions & 0 deletions lib/core/logger/tests/logger-spec.js
@@ -0,0 +1,84 @@
/*global it, inject, module, spyOn, beforeEach, expect, describe */
describe('avLogger', function () {
'use strict';

var AvLogger;
var logger;
var formattedTimestamp = '';
var logMock = {
foo: 'bar',
log: function () {},
error: function () {}
};

beforeEach(module('availity', function (_AvLoggerProvider_) {
_AvLoggerProvider_.enabled(true);
}));
beforeEach(inject(function (_AvLogger_) {
AvLogger = _AvLogger_;
logger = new AvLogger('', logMock);

spyOn(AvLogger, 'getFormattedTimestamp').and.callFake(function () {
return formattedTimestamp;
});
}));

describe('logging', function () {
it('calls the delegate\'s log method', function () {
spyOn(logMock, 'log');
formattedTimestamp = '111';

logger.log('foo');

expect(logMock.log).toHaveBeenCalledWith('111 - foo');
});

it('calls the delegate\'s error method', function () {
spyOn(logMock, 'error');
formattedTimestamp = '111';

logger.error('foo');

expect(logMock.error).toHaveBeenCalledWith('111 - foo');
});

it('logs angular errors with a stack', function () {
spyOn(logMock, 'error');
formattedTimestamp = '111';
var err = new Error('Foo');
err.stack = 'bar';

logger.error(err, undefined); // Angular passes along a 'cause' param that is usually undefined

expect(logMock.error).toHaveBeenCalledWith('111 - Error: Foo\nbar');
});

it('handles logging angular errors with {} in them.', function () {
spyOn(logMock, 'error');
formattedTimestamp = '111';
var err = new Error('Foo {}');
err.stack = 'bar';

logger.error(err, undefined); // Angular passes along a 'cause' param that is usually undefined

expect(logMock.error).toHaveBeenCalledWith('111 - Error: Foo {}\nbar');
});

// TODO: More tests around scenarios where 'cause' is not undefined, as I think that might potentially break the logic in _log
});

describe('supplant', function () {
it('should replace interpolated values with the appropriate array element', function () {
var actual = AvLogger.supplant('my {0} message {1}', ['interpolated', 'works']);

expect(actual).toBe('my interpolated message works');
});

it('should return the raw string when no supplant data is passed in', function () {
var actual = AvLogger.supplant('no supplant data', undefined);

expect(actual).toBe('no supplant data');
});
});

});

0 comments on commit c2da823

Please sign in to comment.