import { expectType, Result } from 'ts-data-forge'; import { type TypeOf } from '../type.mjs'; import { validationErrorsToMessages } from '../utils/index.mjs'; import { boolean } from './boolean.mjs'; describe(boolean, () => { const targetType = boolean(false); type TargetType = TypeOf; expectType('='); expectType('='); describe('is', () => { test('truthy case', () => { const x: unknown = true; if (targetType.is(x)) { expectType('='); } else { expectType('='); } assert.isTrue(targetType.is(x)); }); test('falsy case', () => { const x: unknown = 'not a boolean'; if (targetType.is(x)) { expectType('='); } else { expectType('='); } assert.isFalse(targetType.is(x)); }); }); describe('validate', () => { test('truthy case', () => { const result = targetType.validate(true); assert.isTrue(Result.isOk(result)); const resultValue = Result.unwrapThrow(result); assert.isTrue(resultValue); }); test('falsy case', () => { const result = targetType.validate('not a boolean'); assert.isTrue(Result.isErr(result)); const resultError = Result.unwrapErrThrow(result); assert.deepStrictEqual(resultError, [ { path: [], actualValue: 'not a boolean', expectedType: 'boolean', typeName: 'boolean', details: undefined, }, ]); assert.deepStrictEqual(validationErrorsToMessages(resultError), [ 'Error: expected type but type value "not a boolean" was passed.', ]); }); test('validate returns input as-is for OK cases', () => { const input = false; const result = targetType.validate(input); assert.isTrue(Result.isOk(result)); const resultValue1 = Result.unwrapThrow(result); expect(resultValue1).toBe(input); // ✅ same reference }); }); describe('assertIs', () => { test('truthy case', () => { const x: unknown = false; const assertIs: (a: unknown) => asserts a is boolean = targetType.assertIs; expect(() => { assertIs(x); }).not.toThrow(); }); test('falsy case', () => { const x: unknown = 'not a boolean'; const assertIs: (a: unknown) => asserts a is boolean = targetType.assertIs; expect(() => { assertIs(x); }).toThrow('Error'); }); }); describe('cast', () => { test('truthy case', () => { const x: unknown = true; assert.isTrue(targetType.cast(x)); }); test('falsy case', () => { const x: unknown = 'invalid'; expect(() => targetType.cast(x)).toThrow('Error'); }); }); describe('fill', () => { test('noop', () => { const x: unknown = true; assert.isTrue(targetType.fill(x)); }); test('fill with the default value', () => { const x: unknown = 'not a boolean'; assert.isFalse(targetType.fill(x)); }); }); });