setLevel-asymmetry-test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. /* jshint loopfunc: true */
  3. // This test shows an asymmetry between setLevel and isLevelEnabled
  4. // (in log4js-node@0.4.3 and earlier):
  5. // 1) setLevel("foo") works, but setLevel(log4js.levels.foo) silently
  6. // does not (sets the level to TRACE).
  7. // 2) isLevelEnabled("foo") works as does isLevelEnabled(log4js.levels.foo).
  8. //
  9. // Basic set up
  10. var vows = require('vows');
  11. var assert = require('assert');
  12. var log4js = require('../lib/log4js');
  13. var logger = log4js.getLogger('test-setLevel-asymmetry');
  14. // uncomment one or other of the following to see progress (or not) while running the tests
  15. // var showProgress = console.log;
  16. var showProgress = function() {};
  17. // Define the array of levels as string to iterate over.
  18. var strLevels= ['Trace','Debug','Info','Warn','Error','Fatal'];
  19. var log4jsLevels =[];
  20. // populate an array with the log4js.levels that match the strLevels.
  21. // Would be nice if we could iterate over log4js.levels instead,
  22. // but log4js.levels.toLevel prevents that for now.
  23. strLevels.forEach(function(l) {
  24. log4jsLevels.push(log4js.levels.toLevel(l));
  25. });
  26. // We are going to iterate over this object's properties to define an exhaustive list of vows.
  27. var levelTypes = {
  28. 'string': strLevels,
  29. 'log4js.levels.level': log4jsLevels,
  30. };
  31. // Set up the basic vows batch for this test
  32. var batch = {
  33. setLevel: {
  34. }
  35. };
  36. showProgress('Populating batch object...');
  37. // Populating the batch object programmatically,
  38. // as I don't have the patience to manually populate it with
  39. // the (strLevels.length x levelTypes.length) ^ 2 = 144 possible test combinations
  40. for (var type in levelTypes) {
  41. var context = 'is called with a '+type;
  42. var levelsToTest = levelTypes[type];
  43. showProgress('Setting up the vows context for '+context);
  44. batch.setLevel[context]= {};
  45. levelsToTest.forEach( function(level) {
  46. var subContext = 'of '+level;
  47. var log4jsLevel=log4js.levels.toLevel(level.toString());
  48. showProgress('Setting up the vows sub-context for '+subContext);
  49. batch.setLevel[context][subContext] = {topic: level};
  50. for (var comparisonType in levelTypes) {
  51. levelTypes[comparisonType].forEach(function(comparisonLevel) {
  52. var t = type;
  53. var ct = comparisonType;
  54. var expectedResult = log4jsLevel.isLessThanOrEqualTo(comparisonLevel);
  55. var vow = 'isLevelEnabled(' + comparisonLevel +
  56. ') called with a ' + comparisonType +
  57. ' should return ' + expectedResult;
  58. showProgress('Setting up the vows vow for '+vow);
  59. batch.setLevel[context][subContext][vow] = function(levelToSet) {
  60. logger.setLevel(levelToSet);
  61. showProgress(
  62. '*** Checking setLevel( ' + level +
  63. ' ) of type ' + t +
  64. ', and isLevelEnabled( ' + comparisonLevel +
  65. ' ) of type ' + ct + '. Expecting: ' + expectedResult
  66. );
  67. assert.equal(
  68. logger.isLevelEnabled(comparisonLevel),
  69. expectedResult,
  70. 'Failed: calling setLevel( ' + level +
  71. ' ) with type ' + type +
  72. ', isLevelEnabled( ' + comparisonLevel +
  73. ' ) of type ' + comparisonType +
  74. ' did not return ' + expectedResult
  75. );
  76. };
  77. });
  78. }
  79. });
  80. }
  81. showProgress('Running tests...');
  82. vows.describe('log4js setLevel asymmetry fix').addBatch(batch).export(module);