<commit_before>// import Divmod.UnitTest
// import Methanal.Preds
// import Methanal.Validators
// import Methanal.Util
// import Methanal.Tests.Util



Methanal.Tests.Util.TestCase.subclass(
    Methanal.Tests.TestValidators, 'ValidatorsTestCase').methods(
    /**
     * Assert that C{value} is a valid input for C{validator}.
     */
    function assertValid(self, validator /*...*/) {
        var args = Array.prototype.slice.call(arguments, 2);
        var msg = validator.apply(null, args);
        self.assertIdentical(msg, undefined,
            'Expected valid input for ' + Methanal.Util.repr(validator) +
            ' (' + Methanal.Util.repr(args) + '), got: ' +
            Methanal.Util.repr(msg));
    },


    /**
     * Assert that C{value} is NOT a valid input for C{validator}.
     */
    function assertInvalid(self, validator /*...*/) {
        var args = Array.prototype.slice.call(arguments, 2);
        self.assertNotIdentical(validator.apply(null, args), undefined,
            'Expected invalid input for ' + Methanal.Util.repr(validator) +
            ', got: ' + Methanal.Util.repr(args));
    });



Methanal.Tests.TestValidators.ValidatorsTestCase.subclass(
    Methanal.Tests.TestValidators, 'TestValidators').methods(
    /**
     * Validator with an inverted predicate.
     */
    function test_invert(self) {
        var x = Methanal.Validators.pred(
            Methanal.Preds.invert(Methanal.Preds.lengthOf(3)),
            'Length cannot be 3');
        self.assertValid(x, 'a');
        self.assertValid(x, 'ab');
        self.assertInvalid(x, 'abc');
        self.assertValid(x, 'abcd');
    },


    /**
     * Logical intersection of combined validator results.
     */
    function test_intersection(self) {
        var even = Methanal.Validators.pred(function (n) {
            return n % 2 === 0;
        }, 'Must be even');
        var x = Methanal.Validators.intersection([
            Methanal.Validators.notNull, Methanal.Validators.numeric, even]);
        self.assertValid(x, '2');
        self.assertValid(x, '42');
        self.assertInvalid(x, '3');
        self.assertInvalid(x, 'a');
        self.assertInvalid(x, null);
    },


    /**
     * Any input, but at least one, value must pass the predicate C{func}.
     */
    function test_any(self) {
        var x = Methanal.Validators.any(Methanal.Preds.hasLength);
        self.assertValid(x, '1');
        self.assertValid(x, '1', '2');
        self.assertValid(x, null, '2');
        self.assertValid(x, '1', null);
        self.assertValid(x, '1', '', '3');
        self.assertInvalid(x, '', null);
        self.assertInvalid(x, '', null, '');
    },


    /**
     * All input values must pass the predicate C{func}.
     */
    function test_all(self) {
        var x = Methanal.Validators.all(Methanal.Preds.hasLength);
        self.assertValid(x, '1');
        self.assertValid(x, '1', '2');
        self.assertInvalid(x, null, '2');
        self.assertInvalid(x, '1', null);
        self.assertInvalid(x, '1', null, '3');
    },


    /**
     * Create a validator that is only executed if C{pred} is C{true}.
     */
    function test_ifThen(self) {
        var x = Methanal.Validators.ifThen(<commit_msg>Make test_any and test_all easier to follow.<commit_after>// import Divmod.UnitTest
// import Methanal.Preds
// import Methanal.Validators
// import Methanal.Util
// import Methanal.Tests.Util



Methanal.Tests.Util.TestCase.subclass(
    Methanal.Tests.TestValidators, 'ValidatorsTestCase').methods(
    /**
     * Assert that C{value} is a valid input for C{validator}.
     */
    function assertValid(self, validator /*...*/) {
        var args = Array.prototype.slice.call(arguments, 2);
        var msg = validator.apply(null, args);
        self.assertIdentical(msg, undefined,
            'Expected valid input for ' + Methanal.Util.repr(validator) +
            ' (' + Methanal.Util.repr(args) + '), got: ' +
            Methanal.Util.repr(msg));
    },


    /**
     * Assert that C{value} is NOT a valid input for C{validator}.
     */
    function assertInvalid(self, validator /*...*/) {
        var args = Array.prototype.slice.call(arguments, 2);
        self.assertNotIdentical(validator.apply(null, args), undefined,
            'Expected invalid input for ' + Methanal.Util.repr(validator) +
            ', got: ' + Methanal.Util.repr(args));
    });



Methanal.Tests.TestValidators.ValidatorsTestCase.subclass(
    Methanal.Tests.TestValidators, 'TestValidators').methods(
    /**
     * Validator with an inverted predicate.
     */
    function test_invert(self) {
        var x = Methanal.Validators.pred(
            Methanal.Preds.invert(Methanal.Preds.lengthOf(3)),
            'Length cannot be 3');
        self.assertValid(x, 'a');
        self.assertValid(x, 'ab');
        self.assertInvalid(x, 'abc');
        self.assertValid(x, 'abcd');
    },


    /**
     * Logical intersection of combined validator results.
     */
    function test_intersection(self) {
        var even = Methanal.Validators.pred(function (n) {
            return n % 2 === 0;
        }, 'Must be even');
        var x = Methanal.Validators.intersection([
            Methanal.Validators.notNull, Methanal.Validators.numeric, even]);
        self.assertValid(x, '2');
        self.assertValid(x, '42');
        self.assertInvalid(x, '3');
        self.assertInvalid(x, 'a');
        self.assertInvalid(x, null);
    },


    /**
     * Any input, but at least one, value must pass the predicate C{func}.
     */
    function test_any(self) {
        var any = Methanal.Validators.any(
            function identity(x) { return x });
        self.assertValid(any, true);
        self.assertValid(any, true, true);
        self.assertValid(any, false, true);
        self.assertValid(any, true, false);
        self.assertValid(any, true, false, true);
        self.assertInvalid(any, false);
        self.assertInvalid(any, false, false);
    },


    /**
     * All input values must pass the predicate C{func}.
     */
    function test_all(self) {
        var all = Methanal.Validators.all(
            function identity(x) { return x });
        self.assertValid(all, true);
        self.assertValid(all, true, true);
        self.assertInvalid(all, false, true);
        self.assertInvalid(all, false, true);
        self.assertInvalid(all, false, true, false);
    },


    /**
     * Create a validator that is only executed if C{pred} is C{true}.
     */
    function test_ifThen(self) {
        var x = Methanal.Validators.ifThen(
{'fraction_non_alphanumeric': 0.14198752092527775, 'fraction_numerical': 0.005174250494597474, 'mean_word_length': 2.3737166324435317, 'pattern_counts': {'":': 0, '<': 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': '1450029', 'n_tokens_mistral': 2076, 'n_tokens_neox': 2006, 'n_words': 439}