<commit_before>var assert = require('assert');
var env = require('../index');


describe('validate()', function() {
    var basicSpec = { REQD: { required: true },
                      PARSED: { parse: function(x) { return x + 'foo'; } },
                      CHOICEVAR: { choices: ['one', 'two', 'three'] },
                      REGEXVAR: { regex: /number\d/ },
                      MYBOOL: { parse: env.toBoolean },
                      MYNUM: { parse: env.toNumber }
                    };
    it('throws an error if a required field is not present', function() {
        assert.throws(
            function() { env.validate({}, basicSpec); }, env.EnvError);
    });

    it('validates from a set of choices if given', function() {
        assert.throws(
            function() { env.validate({ REQD: 'asdf', CHOICEVAR: 'asdf'}, basicSpec); }, env.EnvError);

        var myEnv = env.validate({ REQD: 'asdf', CHOICEVAR: 'two'}, basicSpec);
        assert.strictEqual(myEnv.CHOICEVAR, 'two');
        assert.strictEqual(env.get('CHOICEVAR'), 'two');
    });

    it('validates against a regex if one is provided', function() {
        assert.throws(
            function() { env.validate({ REQD: 'asdf', REGEXVAR: 'fail'}, basicSpec); }, env.EnvError);

        var myEnv = env.validate({ REQD: 'asdf', REGEXVAR: 'number4'}, basicSpec);
        assert.strictEqual(myEnv.REGEXVAR, 'number4');
        assert.strictEqual(env.get('REGEXVAR'), 'number4');
    });

    it('works with a custom parse function', function() {
        var myEnv = env.validate({ REQD: 'asdf', PARSED: 'bar'}, basicSpec);
        assert.strictEqual(myEnv.PARSED, 'barfoo');
    });

    it('works with the env.toNumber() parser', function() {
        var myEnv = env.validate({ REQD: 'asdf', MYNUM: '123'}, basicSpec);
        assert.strictEqual(myEnv.MYNUM, 123);

        // If a non-number was entered, toNumber should throw:
        assert.throws(
            function() { env.validate({ REQD: 'asdf', MYNUM: 'asdf12'}, basicSpec); }, env.EnvError);
    });

    it('works with the env.toBoolean() parser', function() {
        var myEnv = env.validate({ REQD: 'asdf', MYBOOL: 'true'}, basicSpec);
        assert.strictEqual(myEnv.MYBOOL, true);
    });
});


describe('get()', function() {
    var basicSpec = { MYVAR: { required: true } };
    var randomKey = 'RANDOMKEY123456';
    process.env[randomKey] = 'HELLO MOCHA';

    it('does not work before validation has happened', function() {
        assert.strictEqual(env.get(randomKey), undefined);

        // Accepts a default argument that will still work for unvalidated vars:
        assert.strictEqual(env.get(randomKey, 'defaultStr'), 'defaultStr');
    });<commit_msg>Add a test case for using JSON.parse as a parse function.<commit_after>var assert = require('assert');
var env = require('../index');


describe('validate()', function() {
    var basicSpec = { REQD: { required: true },
                      PARSED: { parse: function(x) { return x + 'foo'; } },
                      CHOICEVAR: { choices: ['one', 'two', 'three'] },
                      REGEXVAR: { regex: /number\d/ },
                      JSONVAR: { parse: JSON.parse },
                      MYBOOL: { parse: env.toBoolean },
                      MYNUM: { parse: env.toNumber }
                    };
    it('throws an error if a required field is not present', function() {
        assert.throws(
            function() { env.validate({}, basicSpec); }, env.EnvError);
    });

    it('validates from a set of choices if given', function() {
        assert.throws(
            function() { env.validate({ REQD: 'asdf', CHOICEVAR: 'asdf'}, basicSpec); }, env.EnvError);

        var myEnv = env.validate({ REQD: 'asdf', CHOICEVAR: 'two'}, basicSpec);
        assert.strictEqual(myEnv.CHOICEVAR, 'two');
        assert.strictEqual(env.get('CHOICEVAR'), 'two');
    });

    it('validates against a regex if one is provided', function() {
        assert.throws(
            function() { env.validate({ REQD: 'asdf', REGEXVAR: 'fail'}, basicSpec); }, env.EnvError);

        var myEnv = env.validate({ REQD: 'asdf', REGEXVAR: 'number4'}, basicSpec);
        assert.strictEqual(myEnv.REGEXVAR, 'number4');
        assert.strictEqual(env.get('REGEXVAR'), 'number4');
    });

    it('works with a custom parse function', function() {
        var myEnv = env.validate({ REQD: 'asdf', PARSED: 'bar'}, basicSpec);
        assert.strictEqual(myEnv.PARSED, 'barfoo');
    });

    it('works with JSON.parse', function() {
        var myEnv = env.validate({ REQD: 'asdf', JSONVAR: '{"foo": 123, "bar": "baz"}'}, basicSpec);
        assert.strictEqual(myEnv.JSONVAR.foo, 123);
        assert.strictEqual(myEnv.JSONVAR.bar, 'baz');
    });


    it('works with the env.toNumber() parser', function() {
        var myEnv = env.validate({ REQD: 'asdf', MYNUM: '123'}, basicSpec);
        assert.strictEqual(myEnv.MYNUM, 123);

        // If a non-number was entered, toNumber should throw:
        assert.throws(
            function() { env.validate({ REQD: 'asdf', MYNUM: 'asdf12'}, basicSpec); }, env.EnvError);
    });

    it('works with the env.toBoolean() parser', function() {
        var myEnv = env.validate({ REQD: 'asdf', MYBOOL: 'true'}, basicSpec);
        assert.strictEqual(myEnv.MYBOOL, true);
    });
});


describe('get()', function() {
    var basicSpec = { MYVAR: { required: true } };
    var randomKey = 'RANDOMKEY123456';
    process.env[randomKey] = 'HELLO MOCHA';

    it('does not work before validation has happened', function() {
        assert.strictEqual(env.get(randomKey), undefined);

        // Accepts a default argument that will still work for unvalidated vars:
        assert.strictEqual(env.get(randomKey, 'defaultStr'), 'defaultStr');
    });
{'fraction_non_alphanumeric': 0.16355785837651124, 'fraction_numerical': 0.0069084628670120895, 'mean_word_length': 2.6307210031347963, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '452140', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1732, 'n_words': 456}