Skip to content

Commit

Permalink
traceAspect coverage to 100% -useless but fun
Browse files Browse the repository at this point in the history
  • Loading branch information
saitodisse committed Sep 5, 2014
1 parent 5b98d2d commit bdad165
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
37 changes: 5 additions & 32 deletions src/trace-aspect.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
var meld = require('meld');
var joinpoint, depth, padding, simpleReporter;

joinpoint = meld.joinpoint;
var depth, padding;

// Call stack depth tracking for the default reporter
depth = 0;

// Padding characters for indenting traces. This will get expanded as needed
padding = '................................';

simpleReporter = {
onCall: function(info) {
console.log(indent(++depth) + info.method + ' CALL ', info.args);
},
onReturn: function(info) {
console.log(indent(depth--) + info.method + ' RETURN ', info.result);
},
onThrow: function(info) {
console.log(indent(depth--) + info.method + ' THROW ' + info.exception);
}
};

/**
* Creates an aspect that traces method/function calls and reports them
* to the supplied reporter.
Expand All @@ -31,16 +17,16 @@ simpleReporter = {
* @param {function} [reporter.onThrow] invoked when a method throws an exception
* @return {object} a tracing aspect that can be added with meld.add
*/
module.exports = function createTraceAspect(reporter) {
module.exports = function createTraceAspect(reporter, joinpoint) {

if(!reporter) {
reporter = simpleReporter;
if(!joinpoint){
joinpoint = meld.joinpoint;
}

return {
before: function() {
var jp = joinpoint();
reporter.onCall && reporter.onCall({ method: jp.method, target: jp.target, args: jp.args.slice() }).bind(reporter);
reporter.onCall && reporter.onCall({ method: jp.method, target: jp.target, args: jp.args.slice() });
},

afterReturning: function(result) {
Expand All @@ -54,16 +40,3 @@ module.exports = function createTraceAspect(reporter) {
}
};
};

/**
* Create indentation padding for tracing info based on the supplied call stack depth
* @param {number} depth call stack depth
* @return {String} padding that can be used to indent tracing output
*/
function indent(depth) {
if(depth > padding.length) {
padding += padding;
}

return padding.slice(0, depth-1);
}
1 change: 0 additions & 1 deletion test/reporter.onCall.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
var assert = require('assert'),
Reporter = require('../src/reporter'),
fakeConsole = require('./fake-console'),
sinon = require('sinon'),
reporter
;

Expand Down
65 changes: 65 additions & 0 deletions test/trace-aspect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/** @license MIT License (c) Copyright (c) 2014 Julio Makdisse Saito */

/**
* Mogger
* Meld + Trace + Colorful logger
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @author Julio Makdisse Saito (saitodisse@gmail.com)
* @version 0.5.0
*/


var assert = require('assert');
var sinon = require('sinon');
var _ = require('lodash');
var traceAspect = require('../src/trace-aspect');

describe('Trace Aspect (meld)', function (){

var onCallSpy, onReturnSpy, onThrowSpy, traceAspectFunction;
beforeEach(function () {
var jp = function() {
return {
method: '',
target: '',
args: []
};
};

onCallSpy = sinon.spy();
onReturnSpy = sinon.spy();
onThrowSpy = sinon.spy();
var reportSpy = {
onCall: onCallSpy,
onReturn: onReturnSpy,
onThrow: onThrowSpy,
};

traceAspectFunction = traceAspect(reportSpy, jp);
});

it('no jointpoint', function() {
traceAspectFunction = traceAspect(null);
assert.equal(true, _.isObject(traceAspectFunction));
});

it('before', function() {
traceAspectFunction.before();
assert.equal(true, onCallSpy.called);
});

it('afterReturning', function() {
traceAspectFunction.afterReturning({result: null});
assert.equal(true, onReturnSpy.called);
});

it('afterThrowing', function() {
traceAspectFunction.afterThrowing({exception: null});
assert.equal(true, onThrowSpy.called);
});


});

0 comments on commit bdad165

Please sign in to comment.