All files / src/arbitrary effect.ts

100% Statements 15/15
100% Branches 5/5
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40        1x 1x 1x 1x     1x           1x 2x           1x   1x             1x 1x 1x   1x 1x 1x  
/**
 * Arbitraries for effectful datatypes.
 * @module
 */
import {pipe} from 'effect'
import * as EF from 'effect/Effect'
import fc from 'fast-check'
import {Monad as arbitraryMonad} from './instances.js'
import type {LiftArbitrary} from './types.js'
 
const {map} = arbitraryMonad
 
/**
 * Convert an arbitrary of `T` into a successful effect of `T`.
 * @category arbitraries
 */
export const succeed: LiftArbitrary<EF.EffectTypeLambda, never, never> = a =>
  pipe(a, map(EF.succeed))
 
/**
 * Convert an arbitrary of a string error message into a fail effect.
 * @category arbitraries
 */
export const fail: (
  message: fc.Arbitrary<string>,
) => fc.Arbitrary<EF.Effect<never, Error>> = map(m => EF.fail(new Error(m)))
 
/**
 * Convert an arbitrary of a string error message and an arbitrary of `T`
 * into a sync effect, possibly suspended.
 * @category arbitraries
 */
export const sync = <T>(
  a: fc.Arbitrary<T>,
  message: fc.Arbitrary<string>,
): fc.Arbitrary<EF.Effect<T, Error>> =>
  fc
    .oneof(succeed(a), fail(message))
    .map(sync => EF.suspend(() => sync as EF.Effect<T, Error>))