<commit_before>/**
 * <p>Unit tests</p>
 *
 * @module test
 */

var vows = require('vows');
var assert = require('assert');

// Make a copy of the command line args
var argvOrg = process.argv;

/**
 * <p>Tests for underlying node-config utilities.  To run type:</p>
 * <pre>npm test</pre>
 * <p>Or, in a project that uses node-config:</p>
 * <pre>npm test config</pre>
 *
 * @class ProtectedTest
 */

var CONFIG;
vows.describe('Protected (hackable) utilities test')
.addBatch({
  // We initialize the object in a batch so that the globals get changed at /run-time/ not /require-time/,
  // avoiding conflicts with other tests.
  // We initialize in our own /batch/ because batches are run in serial, while individual contexts run in parallel. 
  'Library initialization': {
    topic : function () {
      // Change the configuration directory for testing
      process.env.NODE_CONFIG_DIR = __dirname + '/config';

      // Hardcode $NODE_ENV=test for testing
      process.env.NODE_ENV='test';

      // Test for multi-instance applications
      process.env.NODE_APP_INSTANCE='3';

      // Test $NODE_CONFIG environment and --NODE_CONFIG command line parameter
      process.env.NODE_CONFIG='{"EnvOverride":{"parm3":"overridden from $NODE_CONFIG","parm4":100}}';
      process.argv.push('--NODE_CONFIG={"EnvOverride":{"parm5":"overridden from --NODE_CONFIG","parm6":101}}');

      // Test Environment Variable Substitution
      var override = 'CUSTOM VALUE FROM JSON ENV MAPPING';
      process.env['CUSTOM_JSON_ENVIRONMENT_VAR'] = override;

      // Dependencies
      CONFIG = requireUncached('../lib/config');

      return CONFIG;

    },
    'Library is available': function(config) {
      assert.isObject(config);
    }
  }
})
.addBatch({
  'isObject() tests': {
    'The function exists': function() {
      assert.isFunction(CONFIG.util.isObject);
    },
    'Correctly identifies objects': function() {
   	  assert.isTrue(CONFIG.util.isObject({A:"b"}));
    },
    'Correctly excludes non-objects': function() {
   	  assert.isFalse(CONFIG.util.isObject("some string"));
   	  assert.isFalse(CONFIG.util.isObject(45));
   	  assert.isFalse(CONFIG.util.isObject([2, 3]));
   	  assert.isFalse(CONFIG.util.isObject(["a", "b"]));
   	  assert.isFalse(CONFIG.util.isObject(null));
   	  assert.isFalse(CONFIG.util.isObject(undefined));
    }
  },

  '_cloneDeep() tests': {
    topic: function() {
      // Return an object for copy tests
      return {
        elem0:true,
        elem1:"Element 1",
        elem2:2,
        elem3:[1,2,3],
        elem4:function(){return "hello";},
        elem5:{sub1:"sub 1",sub2:2,sub3:[1,2,3]},
        elem6: {date: new Date, regexp: /test/i}
      };
    },
    'The function exists': function() {
      assert.isFunction(CONFIG.util.cloneDeep);
    },
    'Original and copy should test equivalent (deep)': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      assert.deepEqual(copy, orig);
    },
    'The objects should be different': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      copy.elem1 = false;
      assert.notDeepEqual(copy, orig);
    },
    'Object clones should be objects': function(orig) {
      assert.isObject(CONFIG.util.cloneDeep({a:1, b:2}));
    },
    'Array clones should be arrays': function(orig) {
      assert.isArray(CONFIG.util.cloneDeep(["a", "b", 3]));
    },
    'Arrays should be copied by value, not by reference': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      assert.deepEqual(copy, orig);
      copy.elem3[0] = 2;
      // If the copy wasn't deep, elem3 would be the same object
      assert.notDeepEqual(copy, orig);
    },
    'Objects should be copied by value, not by reference': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      copy.elem5.sub2 = 3;
      assert.notDeepEqual(copy, orig);
      copy = CONFIG.util.cloneDeep(orig);
      copy.elem5.sub3[1] = 3;
      assert.notDeepEqual(copy, orig);
    },
    'Regexps and dates are preserved': function (orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      assert.equal(copy.elem6.date.constructor.name, 'Date');
      assert.equal(copy.elem6.regexp.toString(), '/test/i');
    }
  },

  'extendDeep() tests': {
    'The function exists': function() {
      assert.isFunction(CONFIG.util.extendDeep);
    },
    'Performs normal extend': function() {
      var orig = {elem1:"val1", elem2:"val2"};
      var extWith = {elem3:"val3"};
      var shouldBe = {elem1:"val1", elem2:"val2", elem3:"val3"};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Replaces non-objects': function() {
      var orig = {elem1:"val1", elem2:["val2","val3"],elem3:{sub1:"val4"}};
      var extWith = {elem1:1,elem2:["val4"],elem3:"val3"};
      var shouldBe = {elem1:1, elem2:["val4"],elem3:"val3"};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Merges objects': function() {
      var orig = {e1:"val1", elem2:{sub1:"val4",sub2:"val5"}};
      var extWith = {elem2:{sub2:"val6",sub3:"val7"}};
      var shouldBe = {e1:"val1", elem2:{sub1:"val4",sub2:"val6",sub3:"val7"}};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Merges dates': function() {
      var orig = {e1:"val1", elem2:{sub1:"val4",sub2:new Date(2015, 0, 1)}};
      var extWith = {elem2:{sub2:new Date(2015, 0, 2),sub3:"val7"}};
      var shouldBe = {e1:"val1", elem2:{sub1:"val4",sub2:new Date(2015, 0, 2),sub3:"val7"}};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Correctly types new objects and arrays': function() {
      var orig = {e1:"val1", e3:["val5"]};
      var extWith = {e2:{elem1:"val1"}, e3:["val6","val7"]};
      var shouldBe = {e1:"val1", e2:{elem1:"val1"}, e3:["val6","val7"]};
      var ext = CONFIG.util.extendDeep({}, orig, extWith);
      assert.isObject(ext.e2);
      assert.isArray(ext.e3);
    },
    'Keeps non-merged objects intact': function() {
      var orig     = {e1:"val1", elem2:{sub1:"val4",sub2:"val5"}};
      var shouldBe = {e1:"val1", elem2:{sub1:"val4",sub2:"val5"}};
      var extWith = {elem3:{sub2:"val6",sub3:"val7"}};
      CONFIG.util.extendDeep({}, orig, extWith);
      assert.deepEqual(orig, shouldBe);
    }
  },

  'equalsDeep() tests': {
    'The function exists': function() {
      assert.isFunction(CONFIG.util.equalsDeep);
    },
    'Succeeds on two empty objects': function() {
      assert.isTrue(CONFIG.util.equalsDeep({}, {}));
    },<commit_msg>Add test for extendDeep getting prototype methods<commit_after>/**
 * <p>Unit tests</p>
 *
 * @module test
 */

var vows = require('vows');
var assert = require('assert');

// Make a copy of the command line args
var argvOrg = process.argv;

/**
 * <p>Tests for underlying node-config utilities.  To run type:</p>
 * <pre>npm test</pre>
 * <p>Or, in a project that uses node-config:</p>
 * <pre>npm test config</pre>
 *
 * @class ProtectedTest
 */

var CONFIG;
vows.describe('Protected (hackable) utilities test')
.addBatch({
  // We initialize the object in a batch so that the globals get changed at /run-time/ not /require-time/,
  // avoiding conflicts with other tests.
  // We initialize in our own /batch/ because batches are run in serial, while individual contexts run in parallel. 
  'Library initialization': {
    topic : function () {
      // Change the configuration directory for testing
      process.env.NODE_CONFIG_DIR = __dirname + '/config';

      // Hardcode $NODE_ENV=test for testing
      process.env.NODE_ENV='test';

      // Test for multi-instance applications
      process.env.NODE_APP_INSTANCE='3';

      // Test $NODE_CONFIG environment and --NODE_CONFIG command line parameter
      process.env.NODE_CONFIG='{"EnvOverride":{"parm3":"overridden from $NODE_CONFIG","parm4":100}}';
      process.argv.push('--NODE_CONFIG={"EnvOverride":{"parm5":"overridden from --NODE_CONFIG","parm6":101}}');

      // Test Environment Variable Substitution
      var override = 'CUSTOM VALUE FROM JSON ENV MAPPING';
      process.env['CUSTOM_JSON_ENVIRONMENT_VAR'] = override;

      // Dependencies
      CONFIG = requireUncached('../lib/config');

      return CONFIG;

    },
    'Library is available': function(config) {
      assert.isObject(config);
    }
  }
})
.addBatch({
  'isObject() tests': {
    'The function exists': function() {
      assert.isFunction(CONFIG.util.isObject);
    },
    'Correctly identifies objects': function() {
   	  assert.isTrue(CONFIG.util.isObject({A:"b"}));
    },
    'Correctly excludes non-objects': function() {
   	  assert.isFalse(CONFIG.util.isObject("some string"));
   	  assert.isFalse(CONFIG.util.isObject(45));
   	  assert.isFalse(CONFIG.util.isObject([2, 3]));
   	  assert.isFalse(CONFIG.util.isObject(["a", "b"]));
   	  assert.isFalse(CONFIG.util.isObject(null));
   	  assert.isFalse(CONFIG.util.isObject(undefined));
    }
  },

  '_cloneDeep() tests': {
    topic: function() {
      // Return an object for copy tests
      return {
        elem0:true,
        elem1:"Element 1",
        elem2:2,
        elem3:[1,2,3],
        elem4:function(){return "hello";},
        elem5:{sub1:"sub 1",sub2:2,sub3:[1,2,3]},
        elem6: {date: new Date, regexp: /test/i}
      };
    },
    'The function exists': function() {
      assert.isFunction(CONFIG.util.cloneDeep);
    },
    'Original and copy should test equivalent (deep)': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      assert.deepEqual(copy, orig);
    },
    'The objects should be different': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      copy.elem1 = false;
      assert.notDeepEqual(copy, orig);
    },
    'Object clones should be objects': function(orig) {
      assert.isObject(CONFIG.util.cloneDeep({a:1, b:2}));
    },
    'Array clones should be arrays': function(orig) {
      assert.isArray(CONFIG.util.cloneDeep(["a", "b", 3]));
    },
    'Arrays should be copied by value, not by reference': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      assert.deepEqual(copy, orig);
      copy.elem3[0] = 2;
      // If the copy wasn't deep, elem3 would be the same object
      assert.notDeepEqual(copy, orig);
    },
    'Objects should be copied by value, not by reference': function(orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      copy.elem5.sub2 = 3;
      assert.notDeepEqual(copy, orig);
      copy = CONFIG.util.cloneDeep(orig);
      copy.elem5.sub3[1] = 3;
      assert.notDeepEqual(copy, orig);
    },
    'Regexps and dates are preserved': function (orig) {
      var copy = CONFIG.util.cloneDeep(orig);
      assert.equal(copy.elem6.date.constructor.name, 'Date');
      assert.equal(copy.elem6.regexp.toString(), '/test/i');
    }
  },

  'extendDeep() tests': {
    'The function exists': function() {
      assert.isFunction(CONFIG.util.extendDeep);
    },
    'Performs normal extend': function() {
      var orig = {elem1:"val1", elem2:"val2"};
      var extWith = {elem3:"val3"};
      var shouldBe = {elem1:"val1", elem2:"val2", elem3:"val3"};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Replaces non-objects': function() {
      var orig = {elem1:"val1", elem2:["val2","val3"],elem3:{sub1:"val4"}};
      var extWith = {elem1:1,elem2:["val4"],elem3:"val3"};
      var shouldBe = {elem1:1, elem2:["val4"],elem3:"val3"};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Merges objects': function() {
      var orig = {e1:"val1", elem2:{sub1:"val4",sub2:"val5"}};
      var extWith = {elem2:{sub2:"val6",sub3:"val7"}};
      var shouldBe = {e1:"val1", elem2:{sub1:"val4",sub2:"val6",sub3:"val7"}};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Merges dates': function() {
      var orig = {e1:"val1", elem2:{sub1:"val4",sub2:new Date(2015, 0, 1)}};
      var extWith = {elem2:{sub2:new Date(2015, 0, 2),sub3:"val7"}};
      var shouldBe = {e1:"val1", elem2:{sub1:"val4",sub2:new Date(2015, 0, 2),sub3:"val7"}};
      assert.deepEqual(CONFIG.util.extendDeep(orig, extWith), shouldBe);
    },
    'Correctly types new objects and arrays': function() {
      var orig = {e1:"val1", e3:["val5"]};
      var extWith = {e2:{elem1:"val1"}, e3:["val6","val7"]};
      var shouldBe = {e1:"val1", e2:{elem1:"val1"}, e3:["val6","val7"]};
      var ext = CONFIG.util.extendDeep({}, orig, extWith);
      assert.isObject(ext.e2);
      assert.isArray(ext.e3);
    },
    'Keeps non-merged objects intact': function() {
      var orig     = {e1:"val1", elem2:{sub1:"val4",sub2:"val5"}};
      var shouldBe = {e1:"val1", elem2:{sub1:"val4",sub2:"val5"}};
      var extWith = {elem3:{sub2:"val6",sub3:"val7"}};
      CONFIG.util.extendDeep({}, orig, extWith);
      assert.deepEqual(orig, shouldBe);
    },
    'Keeps prototype methods intact': function() {
      var orig = Object.create({has: function() {}});
      var result = CONFIG.util.extendDeep({}, orig, {});
      assert.isFunction(result.has);
    }
  },

  'equalsDeep() tests': {
    'The function exists': function() {
      assert.isFunction(CONFIG.util.equalsDeep);
    },
    'Succeeds on two empty objects': function() {
      assert.isTrue(CONFIG.util.equalsDeep({}, {}));
    },
{'fraction_non_alphanumeric': 0.17096774193548386, 'fraction_numerical': 0.02700675168792198, 'mean_word_length': 3.264555342290467, 'pattern_counts': {'":': 12, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7626240', 'n_tokens_mistral': 4535, 'n_tokens_neox': 4410, 'n_words': 954}