setLevel-asymmetry-test.js 3.33 KB
Newer Older
jatuporn Tonggasem's avatar
jatuporn Tonggasem committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
"use strict";
/* jshint loopfunc: true */
// This test shows an asymmetry between setLevel and isLevelEnabled 
// (in log4js-node@0.4.3 and earlier):
// 1) setLevel("foo") works, but setLevel(log4js.levels.foo) silently 
//    does not (sets the level to TRACE).
// 2) isLevelEnabled("foo") works as does isLevelEnabled(log4js.levels.foo).
//

// Basic set up
var vows = require('vows');
var assert = require('assert');
var log4js = require('../lib/log4js');
var logger = log4js.getLogger('test-setLevel-asymmetry');

// uncomment one or other of the following to see progress (or not) while running the tests
// var showProgress = console.log;
var showProgress = function() {};


// Define the array of levels as string to iterate over.
var strLevels= ['Trace','Debug','Info','Warn','Error','Fatal'];

var log4jsLevels =[];
// populate an array with the log4js.levels that match the strLevels.
// Would be nice if we could iterate over log4js.levels instead, 
// but log4js.levels.toLevel prevents that for now.
strLevels.forEach(function(l) {
  log4jsLevels.push(log4js.levels.toLevel(l));
});


// We are going to iterate over this object's properties to define an exhaustive list of vows.
var levelTypes = {
  'string': strLevels,
  'log4js.levels.level': log4jsLevels,
};

// Set up the basic vows batch for this test
var batch = {
  setLevel: {
  }
};

showProgress('Populating batch object...');

// Populating the batch object programmatically,
// as I don't have the patience to manually populate it with 
// the (strLevels.length x levelTypes.length) ^ 2 = 144 possible test combinations
for (var type in levelTypes) {
  var context = 'is called with a '+type;
  var levelsToTest = levelTypes[type];
  showProgress('Setting up the vows context for '+context);

  batch.setLevel[context]= {};
  levelsToTest.forEach( function(level) {
    var subContext = 'of '+level;
    var log4jsLevel=log4js.levels.toLevel(level.toString());

    showProgress('Setting up the vows sub-context for '+subContext);
    batch.setLevel[context][subContext] = {topic: level};
    for (var comparisonType in levelTypes) {
      levelTypes[comparisonType].forEach(function(comparisonLevel) {
        var t = type;
        var ct = comparisonType;
        var expectedResult = log4jsLevel.isLessThanOrEqualTo(comparisonLevel);
        var vow = 'isLevelEnabled(' + comparisonLevel + 
          ') called with a ' + comparisonType + 
          ' should return ' + expectedResult;
        showProgress('Setting up the vows vow for '+vow);

        batch.setLevel[context][subContext][vow] = function(levelToSet) {
          logger.setLevel(levelToSet);
          showProgress(
            '*** Checking setLevel( ' + level + 
              ' ) of type ' + t + 
              ', and isLevelEnabled( ' + comparisonLevel + 
              ' ) of type ' + ct + '. Expecting: ' + expectedResult
          );
          assert.equal(
            logger.isLevelEnabled(comparisonLevel), 
            expectedResult, 
            'Failed: calling setLevel( ' + level + 
              ' ) with type ' + type + 
              ', isLevelEnabled( ' + comparisonLevel + 
              ' ) of type ' + comparisonType + 
              ' did not return ' + expectedResult
          );
        };
      });
    }
  });

}

showProgress('Running tests...');

vows.describe('log4js setLevel asymmetry fix').addBatch(batch).export(module);