From 910d983b829bf02424c0f4cb4c14125d00f6e521 Mon Sep 17 00:00:00 2001 From: dceejay Date: Fri, 6 Mar 2015 14:14:47 +0000 Subject: [PATCH] More tests for red, log, info and util. --- red/log.js | 14 +++++--------- red/util.js | 2 +- test/red/api/info_spec.js | 1 + test/red/log_spec.js | 28 ++++++++++++++++++++++------ test/red/red_spec.js | 8 ++++---- test/red/util_spec.js | 29 +++++++++++++++++++---------- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/red/log.js b/red/log.js index 48ab37bf6..ccaa8e226 100644 --- a/red/log.js +++ b/red/log.js @@ -17,7 +17,6 @@ var util = require("util"); var EventEmitter = require("events").EventEmitter; - var levels = { off: 1, fatal: 10, @@ -27,7 +26,8 @@ var levels = { debug: 50, trace: 60, metric: 99 -} +}; + var levelNames = { 10: "fatal", 20: "error", @@ -36,7 +36,7 @@ var levelNames = { 50: "debug", 60: "trace", 99: "metric" -} +}; var logHandlers = []; @@ -47,6 +47,7 @@ var ConsoleLogHandler = function(settings) { this.metricsOn = settings.metrics||false; metricsEnabled = this.metricsOn; this.on("log",function(msg) { + /* istanbul ignore else */ if (this.shouldReportMessage(msg.level)) { if (msg.level == log.METRIC) { util.log("[metric] "+JSON.stringify(msg)); @@ -70,7 +71,7 @@ var log = module.exports = { DEBUG: 50, TRACE: 60, METRIC: 99, - + init: function(settings) { logHandlers = []; var consoleSettings = {}; @@ -79,11 +80,9 @@ var log = module.exports = { } log.addHandler(new ConsoleLogHandler(consoleSettings)); }, - addHandler: function(func) { logHandlers.push(func); }, - log: function(msg) { msg.timestamp = Date.now(); logHandlers.forEach(function(handler) { @@ -105,10 +104,7 @@ var log = module.exports = { debug: function(msg) { log.log({level:log.DEBUG,msg:msg}); }, - - metric: function() { return metricsEnabled; } } - diff --git a/red/util.js b/red/util.js index a50a2405e..6e4da1339 100644 --- a/red/util.js +++ b/red/util.js @@ -89,6 +89,7 @@ function compareObjects(obj1,obj2) { return false; } for (var k in obj1) { + /* istanbul ignore else */ if (obj1.hasOwnProperty(k)) { if (!compareObjects(obj1[k],obj2[k])) { return false; @@ -104,4 +105,3 @@ module.exports = { cloneMessage: cloneMessage, compareObjects: compareObjects }; - diff --git a/test/red/api/info_spec.js b/test/red/api/info_spec.js index 0e5b02451..75e4cc59b 100644 --- a/test/red/api/info_spec.js +++ b/test/red/api/info_spec.js @@ -52,6 +52,7 @@ describe("info api", function() { } res.body.should.have.property("httpNodeRoot","testHttpNodeRoot"); res.body.should.have.property("version","testVersion"); + res.body.should.have.property("paletteCategories",["red","blue","green"]); res.body.should.not.have.property("foo",123); done(); }); diff --git a/test/red/log_spec.js b/test/red/log_spec.js index d2ed97094..bbea8b4c9 100644 --- a/test/red/log_spec.js +++ b/test/red/log_spec.js @@ -14,6 +14,8 @@ * limitations under the License. **/ var should = require("should"); +var sinon = require("sinon"); +var util = require("util"); describe("red/log", function() { it('can be required without errors', function() { @@ -21,19 +23,33 @@ describe("red/log", function() { }); var log = require("../../red/log"); + var sett = {logging: { console: { level: 'metric', metrics: true } } } + log.init(sett); + + beforeEach(function () { + var spy = sinon.spy(util, 'log'); + }); + + afterEach(function() { + util.log.restore(); + }); it('it can raise an error', function() { - var m = {level:20, msg:"This is an error", type:"test", id:"12345", name:"ERROR" }; - var ret = log.error(m); + var ret = log.error("This is an error"); + sinon.assert.calledWithMatch(util.log,""); }); it('it can raise a trace', function() { - var m = {level:60, msg:"This is a trace", type:"test", id:"12345", name:"TRACE" }; - var ret = log.trace(m); + var ret = log.trace("This is a trace"); + sinon.assert.calledWithMatch(util.log,""); }); it('it can raise a debug', function() { - var m = {level:50, msg:"This is a debug", type:"test", id:"12345", name:"DEBUG" }; - var ret = log.debug(m); + var ret = log.debug("This is a debug"); + sinon.assert.calledWithMatch(util.log,""); + }); + + it('it checks level of metrics', function() { + log.metric().should.equal(true); }); }); diff --git a/test/red/red_spec.js b/test/red/red_spec.js index 913424654..cd002ebea 100644 --- a/test/red/red_spec.js +++ b/test/red/red_spec.js @@ -24,22 +24,22 @@ describe("red/red", function() { it('returns an app object', function() { var srv = RED.app.use("/test", function() { return "app"; }); - //srv.should.be.an.instanceOf(Object); + srv.should.be.an.instanceOf(Object); }); it('returns an httpAdmin object', function() { var srv = RED.httpAdmin.use("/test", function() { return "Admin"; }); - //srv.should.be.an.instanceOf(Object); + srv.should.be.an.instanceOf(Object); }); it('returns an httpNode object', function() { var srv = RED.httpNode.use("/test", function() { return "Node"; }); - //srv.should.be.an.instanceOf(Object); + srv.should.be.an.instanceOf(Object); }); it('it returns a server object', function() { var srv = RED.server; - //srv.should.be.an.instanceOf(Object).and.have.property('domain', null); + srv.should.be.an.instanceOf(Object).and.have.property('domain', null); //srv.should.be.an.instanceOf(Object).and.have.property('timeout', 120000); }); diff --git a/test/red/util_spec.js b/test/red/util_spec.js index cd65a933d..2c7878a8c 100644 --- a/test/red/util_spec.js +++ b/test/red/util_spec.js @@ -17,6 +17,16 @@ var should = require("should"); var util = require("../../red/util"); describe("red/util", function() { + // only test for things not tested by overall grunt + describe('compareObjects', function() { + it('unequal arrays are unequal', function() { + util.compareObjects(["a"],"a").should.equal(false); + }); + it('unequal key lengths are unequal', function() { + util.compareObjects({"a":1},{"a":1,"b":1}).should.equal(false); + }); + }); + describe('ensureString', function() { it('strings are preserved', function() { util.ensureString('string').should.equal('string'); @@ -66,35 +76,34 @@ describe("red/util", function() { } }); }); - + describe('cloneMessage', function() { it('clones a simple message', function() { var msg = {string:"hi",array:[1,2,3],object:{a:1,subobject:{b:2}}}; - + var cloned = util.cloneMessage(msg); - + cloned.should.eql(msg); - + cloned.should.not.equal(msg); cloned.array.should.not.equal(msg.string); cloned.object.should.not.equal(msg.object); cloned.object.subobject.should.not.equal(msg.object.subobject); - + cloned.should.not.have.property("req"); cloned.should.not.have.property("res"); }); it('does not clone http req/res properties', function() { var msg = {req:{a:1},res:{b:2}}; - + var cloned = util.cloneMessage(msg); - + cloned.should.eql(msg); cloned.should.not.equal(msg); - + cloned.req.should.equal(msg.req); cloned.res.should.equal(msg.res); }); - + }); }); -