Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SORD-158 🐛 Fix integer regex. #205

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 86 additions & 46 deletions src/components/numberField/NumberValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,107 @@
import { NumberValidator } from './NumberValidator';

describe('NumberValidator', () => {
let validator: NumberValidator;
const commonValidator = (decimal: boolean) => {
describe('allowNegatives', () => {
it('should return true when minValue is less than 0', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.allowNegatives()).toBe(true);
});

beforeEach(() => {
validator = new NumberValidator(-10, 10, true);
});

describe('allowNegatives', () => {
it('should return true when minValue is less than 0', () => {
const validator = new NumberValidator(-1, 10, true);
expect(validator.allowNegatives()).toBe(true);
it('should return false when minValue is not less than 0', () => {
const validator = new NumberValidator(0, 10, decimal);
expect(validator.allowNegatives()).toBe(false);
});
});

it('should return false when minValue is not less than 0', () => {
const validator = new NumberValidator(0, 10, true);
expect(validator.allowNegatives()).toBe(false);
});
});
describe('validateFormat', () => {
it('should return true for valid integer numbers', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateFormat('')).toBe(true);
expect(validator.validateFormat('-')).toBe(true);
expect(validator.validateFormat('5')).toBe(true);
expect(validator.validateFormat('-5')).toBe(true);
expect(validator.validateFormat('-0')).toBe(true);
});

describe('validateFormat', () => {
test('validateFormat should return true for valid numbers', () => {
expect(validator.validateFormat('5')).toBe(true);
expect(validator.validateFormat('-5')).toBe(true);
expect(validator.validateFormat('5.5')).toBe(true);
expect(validator.validateFormat('-5.5')).toBe(true);
it('should return false for invalid numbers', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateFormat('abc')).toBe(false);
expect(validator.validateFormat('01')).toBe(false);
expect(validator.validateFormat('5.5.5')).toBe(false);
expect(validator.validateFormat('00.5')).toBe(false);
expect(validator.validateFormat('01.0')).toBe(false);
expect(validator.validateFormat('0.51')).toBe(false);
expect(validator.validateFormat('0.50')).toBe(false);
});
});

test('validateFormat should return false for invalid numbers', () => {
expect(validator.validateFormat('abc')).toBe(false);
expect(validator.validateFormat('5.5.5')).toBe(false);
});
});
describe('validateMinMax', () => {
it('should return true for valid numbers', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateMinMax('5')).toBe(true);
});

describe('validateMinMax', () => {
test('validateMinMax returns true for valid numbers', () => {
expect(validator.validateMinMax('5')).toBe(true);
});
it('should return false for numbers out of range', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateMinMax('15')).toBe(false);
expect(validator.validateMinMax('-2')).toBe(false);
});

test('validateMinMax returns false for numbers out of range', () => {
expect(validator.validateMinMax('15')).toBe(false);
});
it('should return true for negative numbers when allowed', () => {
const validator = new NumberValidator(-5, 10, decimal);
expect(validator.validateMinMax('-5')).toBe(true);
});

test('validateMinMax returns true for negative numbers when allowed', () => {
expect(validator.validateMinMax('-5')).toBe(true);
});
it('should return false for negative numbers when out of range', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateMinMax('-5')).toBe(false);
});

test('validateMinMax returns false for negative numbers when not allowed', () => {
validator = new NumberValidator(0, 10, true);
expect(validator.validateMinMax('-5')).toBe(false);
});
it('should return true for undefined text', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateMinMax(undefined)).toBe(true);
});

test('validateMinMax returns true for undefined text', () => {
expect(validator.validateMinMax(undefined)).toBe(true);
it('should return true for "-" when negatives are allowed', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateMinMax('-')).toBe(true);
});

it('should return false for "-" when negatives are not allowed', () => {
const validator = new NumberValidator(0, 10, decimal);
expect(validator.validateMinMax('-')).toBe(false);
});
});
};

describe.each([[true], [false]])('Decimal is %s', commonValidator);
describe('Decimal validator', () => {
const decimal = true;

test('validateMinMax returns true for "-" when negatives are allowed', () => {
expect(validator.validateMinMax('-')).toBe(true);
describe('validateFormat', () => {
it('should return true for valid decimal numbers', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateFormat('5.5')).toBe(true);
expect(validator.validateFormat('-5.5')).toBe(true);
expect(validator.validateFormat('-0.1')).toBe(true);
expect(validator.validateFormat('-0.0')).toBe(true);
expect(validator.validateFormat('-5.0')).toBe(true);
});
});
});

test('validateMinMax returns false for "-" when negatives are not allowed', () => {
validator = new NumberValidator(0, 10, true);
expect(validator.validateMinMax('-')).toBe(false);
describe('integer validator', () => {
const decimal = false;
describe('validateFormat', () => {
it('should return false for valid decimal numbers', () => {
const validator = new NumberValidator(-1, 10, decimal);
expect(validator.validateFormat('5.5')).toBe(false);
expect(validator.validateFormat('-5.5')).toBe(false);
expect(validator.validateFormat('-0.1')).toBe(false);
expect(validator.validateFormat('-0.0')).toBe(false);
expect(validator.validateFormat('-5.0')).toBe(false);
});
});
});
});
4 changes: 3 additions & 1 deletion src/components/numberField/NumberValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class NumberValidator {
: /^(0|([1-9]\d*))?([\.]\d?)?$/;

const integerRegex =
minValue !== undefined && minValue < 0 ? /^-?\d+$/ : /^\d+$/;
minValue !== undefined && minValue < 0
Mibou marked this conversation as resolved.
Show resolved Hide resolved
? /^-?(0|([1-9]\d*))?$/
: /^(0|([1-9]\d*))?$/;

this.regex = allowDecimal ? decimalRegex : integerRegex;
}
Expand Down
Loading