const empty = () => fc.constantFrom(null, undefined)
describe('inheritance', () => { test('instance of Option', () => { fc.assert( fc.property(fc.anything(), value => Option.of(value) instanceof Option), ) }) test('instance of None or Some', () => { fc.assert( fc.property(fc.anything(), value => { const isSome = Option.of(value) instanceof Some const isNone = Option.of(value) instanceof None return isSome || isNone }), ) }) })
describe('capitalize', () => { it('returns a string with its first letter capitalized', () => { expect(capitalize('margarita')).toBe('Margarita'); expect(capitalize(capitalize('margarita'))).toBe('Margarita'); expect(capitalize('Margarita')).toBe('Margarita'); }); it('returns a string of the same length', () => { fc.assert( fc.property(fc.string(), text => { expect(typeof capitalize(text)).toBe('string'); expect(capitalize(text)).toHaveLength(text.length); }), ); }); it('returns the same result if applied twice', () => { fc.assert( fc.property(fc.string(), text => { expect(capitalize(capitalize(text))).toBe(capitalize(text)); }), ); }); it('handles null and undefined values without crashing', () => { expect(capitalize(null)).toBeNull(); expect(capitalize(undefined)).toBeNull(); }); });
describe('nonEmpty values', () => { test('Option must be non-empty', () => { fc.assert(fc.property(nonEmpty(), value => Option.of(value).nonEmpty())) }) test('Option must have a same value', () => { fc.assert( fc.property(nonEmpty(), value => Option.of(value).get() === value), ) }) test('Option must skip getOrElse', () => { fc.assert( fc.property( nonEmpty(), fc.anything(), (value, or) => Option.of(value).getOrElse(or) === value, ), ) }) })
describe('isEmpty values', () => { test('Option must be epmty', () => { fc.assert(fc.property(empty(), value => Option.of(value).isEmpty())) }) test('Option must have no value', () => { fc.assert( fc.property( empty(), value => typeof Option.of(value).get === 'undefined', ), ) }) test('Option must return `or` from getOrElse', () => { fc.assert( fc.property( empty(), nonEmpty(), (value, or) => Option.of(value).getOrElse(or) === or, ), ) }) })