Skip to content

Commit

Permalink
Merge pull request hexojs#1391 from leesei/master
Browse files Browse the repository at this point in the history
[helper] add log()
  • Loading branch information
leesei committed Jul 16, 2015
2 parents 5c46552 + a50719b commit b1faffd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/plugins/helper/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

var util = require('util');

// this solves circular reference in object
// this format object as string, resolves circular reference
function inspectObject(object, options){
return util.inspect(object, options);
}

// wrapper to log to console
function log(){
return console.log.apply(null, arguments);
}

exports.inspectObject = inspectObject;
exports.log = log;
1 change: 1 addition & 0 deletions lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ module.exports = function(ctx){

var debug = require('./debug');
helper.register('inspect', debug.inspectObject);
helper.register('log', debug.log);
};
31 changes: 31 additions & 0 deletions test/scripts/helpers/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var should = require('chai').should();

describe('debug', function(){
var debug = require('../../../lib/plugins/helper/debug');
var inspect = require('util').inspect;

it('inspect simple object', function(){
var obj = { foo: "bar" };
debug.inspectObject(obj).should.eql(inspect(obj));
});

it('inspect circular object', function(){
var obj = { foo: "bar" };
obj.circular = obj;
debug.inspectObject(obj).should.eql(inspect(obj));
});

it('inspect deep object', function(){
var obj = { baz: { thud: "narf", dur: { foo: "bar", baz: { bang: "zoom" } } } };
debug.inspectObject(obj).should.not.eql(inspect(obj, {depth: 5}));
debug.inspectObject(obj, {depth: 5}).should.eql(inspect(obj, {depth: 5}));
});

it('log should print to console', function(){
debug.log('Hello %s from debug.log()!', 'World');
// console should print 'Hello World from debug.log()!'
debug.should.be.a('object'); // void assert
});
});
3 changes: 2 additions & 1 deletion test/scripts/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

describe('Helpers', function(){
require('./debug');
require('./css');
require('./date');
require('./favicon_tag');
Expand All @@ -27,4 +28,4 @@ describe('Helpers', function(){
require('./tagcloud');
require('./toc');
require('./url_for');
});
});

0 comments on commit b1faffd

Please sign in to comment.