<commit_before>var demand = require('must');
var NumberType = require('../NumberType');
var validators = require('../../validators');

exports.initList = function (List) {
	List.add({
		number: { type: NumberType },
		nested: {
			number: { type: NumberType },
		},
	});
};

exports.testFieldType = function (List) {
	var testItem = new List.model();

	describe('updateItem', function () {
		it('should update top level fields', function (done) {
			List.fields.number.updateItem(testItem, {
				number: 42,
			}, function () {
				demand(testItem.number).be(42);
				testItem.number = undefined;
				done();
			});
		});

		it('should update nested fields', function (done) {
			List.fields['nested.number'].updateItem(testItem, {
				nested: {
					number: 42,
				},
			}, function () {
				demand(testItem.nested.number).be(42);
				testItem.nested.number = undefined;
				done();
			});
		});

		it('should update nested fields with flat paths', function (done) {
			List.fields['nested.number'].updateItem(testItem, {
				'nested.number': 42,
			}, function () {
				demand(testItem.nested.number).be(42);
				testItem.nested.number = undefined;
				done();
			});
		});

		it('should null value with empty string', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: '',
			}, function () {
				demand(testItem.number).be(null);
				testItem.number = undefined;
				done();
			});
		});

		it('should null value when null', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: null,
			}, function () {
				demand(testItem.number).be(null);
				testItem.number = undefined;
				done();
			});
		});

		it('should not null value when undefined', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: undefined,
			}, function () {
				demand(testItem.number).be(1);
				testItem.number = undefined;
				done();
			});
		});

		it('should convert string values', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: '50.50',
			}, function () {
				demand(testItem.number).be(50.50);
				testItem.number = undefined;
				done();
			});
		});
	});

	it('should use the common number input validator', function () {
		demand(List.fields.number.validateInput === validators.number.input);
	});

	it('should use the common number required validator', function () {
		demand(List.fields.number.validateRequiredInput === validators.number.required);
	});

	describe('validateInput', function () {
		it('should validate numeric input', function (done) {
			List.fields.number.validateInput({ number: 1 }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate undefined input', function (done) {
			List.fields.number.validateInput({}, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate null input', function (done) {
			List.fields.number.validateInput({ number: null }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate empty string input', function (done) {
			List.fields.number.validateInput({ number: '' }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate numeric string input', function (done) {
			List.fields.number.validateInput({ number: '1' }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should invalidate string input', function (done) {
			List.fields.number.validateInput({ number: 'a' }, function (result) {
				demand(result).be(false);
				done();
			});
		});


		it('should invalidate object input', function (done) {
			List.fields.number.validateInput({ number: { things: 'stuff' } }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate array input', function (done) {
			List.fields.number.validateInput({ number: [1, 2, 3] }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate Boolean input', function (done) {
			List.fields.number.validateInput({ number: true }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate function input', function (done) {
			List.fields.number.validateInput({ number: function () {} }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate regexp input', function (done) {
			List.fields.number.validateInput({ number: /foo/ }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate date input', function (done) {
			List.fields.number.validateInput({ number: new Date() }, function (result) {
				demand(result).be(false);
				done();
			});
		});
	});

	/* Deprecated inputIsValid method tests */

	it('should validate numeric input', function () {
		demand(List.fields.number.inputIsValid({
			number: 0,
		})).be(true);<commit_msg>Add tests to validateRequiredInput of the number field<commit_after>var demand = require('must');
var NumberType = require('../NumberType');
var validators = require('../../validators');

exports.initList = function (List) {
	List.add({
		number: { type: NumberType },
		nested: {
			number: { type: NumberType },
		},
	});
};

exports.testFieldType = function (List) {
	var testItem = new List.model();

	describe('updateItem', function () {
		it('should update top level fields', function (done) {
			List.fields.number.updateItem(testItem, {
				number: 42,
			}, function () {
				demand(testItem.number).be(42);
				testItem.number = undefined;
				done();
			});
		});

		it('should update nested fields', function (done) {
			List.fields['nested.number'].updateItem(testItem, {
				nested: {
					number: 42,
				},
			}, function () {
				demand(testItem.nested.number).be(42);
				testItem.nested.number = undefined;
				done();
			});
		});

		it('should update nested fields with flat paths', function (done) {
			List.fields['nested.number'].updateItem(testItem, {
				'nested.number': 42,
			}, function () {
				demand(testItem.nested.number).be(42);
				testItem.nested.number = undefined;
				done();
			});
		});

		it('should null value with empty string', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: '',
			}, function () {
				demand(testItem.number).be(null);
				testItem.number = undefined;
				done();
			});
		});

		it('should null value when null', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: null,
			}, function () {
				demand(testItem.number).be(null);
				testItem.number = undefined;
				done();
			});
		});

		it('should not null value when undefined', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: undefined,
			}, function () {
				demand(testItem.number).be(1);
				testItem.number = undefined;
				done();
			});
		});

		it('should convert string values', function (done) {
			testItem.number = 1;
			List.fields.number.updateItem(testItem, {
				number: '50.50',
			}, function () {
				demand(testItem.number).be(50.50);
				testItem.number = undefined;
				done();
			});
		});
	});

	it('should use the common number input validator', function () {
		demand(List.fields.number.validateInput === validators.number.input);
	});

	it('should use the common number required validator', function () {
		demand(List.fields.number.validateRequiredInput === validators.number.required);
	});

	describe('validateInput', function () {
		it('should validate numeric input', function (done) {
			List.fields.number.validateInput({ number: 1 }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate undefined input', function (done) {
			List.fields.number.validateInput({}, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate null input', function (done) {
			List.fields.number.validateInput({ number: null }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate empty string input', function (done) {
			List.fields.number.validateInput({ number: '' }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate numeric string input', function (done) {
			List.fields.number.validateInput({ number: '1' }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should invalidate string input', function (done) {
			List.fields.number.validateInput({ number: 'a' }, function (result) {
				demand(result).be(false);
				done();
			});
		});


		it('should invalidate object input', function (done) {
			List.fields.number.validateInput({ number: { things: 'stuff' } }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate array input', function (done) {
			List.fields.number.validateInput({ number: [1, 2, 3] }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate Boolean input', function (done) {
			List.fields.number.validateInput({ number: true }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate function input', function (done) {
			List.fields.number.validateInput({ number: function () {} }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate regexp input', function (done) {
			List.fields.number.validateInput({ number: /foo/ }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate date input', function (done) {
			List.fields.number.validateInput({ number: new Date() }, function (result) {
				demand(result).be(false);
				done();
			});
		});
	});

	describe('validateRequiredInput', function () {
		it('should validate numeric input', function (done) {
			var testItem = new List.model();
			List.fields.number.validateRequiredInput(testItem, { number: 1 }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should validate numeric string input', function (done) {
			var testItem = new List.model();
			List.fields.number.validateRequiredInput(testItem, { number: '1' }, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should invalidate undefined input', function (done) {
			var testItem = new List.model();
			List.fields.number.validateRequiredInput(testItem, {}, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should validate undefined input if data exists', function (done) {
			var testItem = new List.model({
				number: 1,
			});
			List.fields.number.validateRequiredInput(testItem, {}, function (result) {
				demand(result).be(true);
				done();
			});
		});

		it('should invalidate null input', function (done) {
			var testItem = new List.model();
			List.fields.number.validateRequiredInput(testItem, { number: null }, function (result) {
				demand(result).be(false);
				done();
			});
		});

		it('should invalidate empty string input', function (done) {
			var testItem = new List.model();
			List.fields.number.validateRequiredInput(testItem, { number: '' }, function (result) {
				demand(result).be(false);
				done();
			});
		});
	});

	/* Deprecated inputIsValid method tests */

	it('should validate numeric input', function () {
		demand(List.fields.number.inputIsValid({
			number: 0,
		})).be(true);
{'fraction_non_alphanumeric': 0.17368829154820367, 'fraction_numerical': 0.005600068923925217, 'mean_word_length': 3.48531684698609, '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': '6940388', 'n_tokens_mistral': 4383, 'n_tokens_neox': 3512, 'n_words': 788}