/* eslint-disable no-empty-function */ import { t, typeAsserts } from '../../index'; describe('number', () => { it('number', () => { expect.hasAssertions(); const schema = t.number(); expect(schema.validate(-1)).toBe(true); expect(() => typeAsserts(schema, -1)).not.toThrow(); expect(schema.validate(+0)).toBe(true); expect(() => typeAsserts(schema, +0)).not.toThrow(); expect(schema.validate(-0)).toBe(true); expect(() => typeAsserts(schema, -0)).not.toThrow(); expect(schema.validate(1)).toBe(true); expect(() => typeAsserts(schema, 1)).not.toThrow(); expect(schema.validate(NaN)).toBe(true); expect(() => typeAsserts(schema, NaN)).not.toThrow(); expect(schema.validate('str')).toBe(false); expect(() => typeAsserts(schema, 'str')).toThrow(`param should be number, but got "str"`); expect(schema.validate(Symbol(1))).toBe(false); expect(() => typeAsserts(schema, Symbol(1))).toThrow(`param should be number, but got a(n) symbol`); expect(schema.validate(null)).toBe(false); expect(() => typeAsserts(schema, null)).toThrow(`param should be number, but got null`); expect(schema.validate(undefined)).toBe(false); expect(() => typeAsserts(schema, undefined)).toThrow(`param should be number, but got undefined`); expect(schema.validate({})).toBe(false); expect(() => typeAsserts(schema, {})).toThrow(`param should be number, but got a(n) object`); expect(schema.validate(() => {})).toBe(false); expect(() => typeAsserts(schema, () => {})).toThrow(`param should be number, but got a(n) function`); expect(schema.validate(BigInt(1))).toBe(false); expect(() => typeAsserts(schema, BigInt(1))).toThrow(`param should be number, but got a(n) bigint`); }); it('optional number', () => { expect.hasAssertions(); const schema = t.number().optional(); expect(schema.validate(-1)).toBe(true); expect(() => typeAsserts(schema, -1)).not.toThrow(); expect(schema.validate(+0)).toBe(true); expect(() => typeAsserts(schema, +0)).not.toThrow(); expect(schema.validate(-0)).toBe(true); expect(() => typeAsserts(schema, -0)).not.toThrow(); expect(schema.validate(1)).toBe(true); expect(() => typeAsserts(schema, 1)).not.toThrow(); expect(schema.validate(NaN)).toBe(true); expect(() => typeAsserts(schema, NaN)).not.toThrow(); expect(schema.validate(undefined)).toBe(true); expect(() => typeAsserts(schema, undefined)).not.toThrow(); expect(schema.validate('str')).toBe(false); expect(() => typeAsserts(schema, 'str')).toThrow(`param should be number or undefined, but got "str"`); expect(schema.validate(Symbol(1))).toBe(false); expect(() => typeAsserts(schema, Symbol(1))).toThrow( `param should be number or undefined, but got a(n) symbol` ); expect(schema.validate(null)).toBe(false); expect(() => typeAsserts(schema, null)).toThrow(`param should be number or undefined, but got null`); expect(schema.validate({})).toBe(false); expect(() => typeAsserts(schema, {})).toThrow(`param should be number or undefined, but got a(n) object`); expect(schema.validate(() => {})).toBe(false); expect(() => typeAsserts(schema, () => {})).toThrow( `param should be number or undefined, but got a(n) function` ); expect(schema.validate(BigInt(1))).toBe(false); expect(() => typeAsserts(schema, BigInt(1))).toThrow( `param should be number or undefined, but got a(n) bigint` ); }); it('max', () => { expect.hasAssertions(); const schema = t.number().max(100); expect(schema.validate(1)).toBe(true); expect(() => { typeAsserts(schema, 1); }).not.toThrow(); expect(schema.validate(100)).toBe(true); expect(() => { typeAsserts(schema, 100); }).not.toThrow(); expect(schema.validate(-101)).toBe(true); expect(() => { typeAsserts(schema, -101); }).not.toThrow(); expect(schema.validate(-0)).toBe(true); expect(() => { typeAsserts(schema, -0); }).not.toThrow(); expect(schema.validate(101)).toBe(false); expect(() => { typeAsserts(schema, 101); }).toThrow('param should be less than or equal to 100'); }); it('min', () => { expect.hasAssertions(); const schema = t.number().min(100); expect(schema.validate(100)).toBe(true); expect(() => { typeAsserts(schema, 100); }).not.toThrow(); expect(schema.validate(101)).toBe(true); expect(() => { typeAsserts(schema, 101); }).not.toThrow(); expect(schema.validate(99)).toBe(false); expect(() => { typeAsserts(schema, 99); }).toThrow(); expect(schema.validate(-99)).toBe(false); expect(() => { typeAsserts(schema, -99); }).toThrow('param should be greater than or equal to 100'); }); });