All files / src/laws/typeclass/monomorphic props.ts

100% Statements 45/45
100% Branches 4/4
100% Functions 2/2
100% Lines 45/45

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140  1x             1x 1x                 1x   1x 1x                                   1x 1x 1x 1x             1x 1x           1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x           1x   1x             1x           1x 1x   1x 1x 1x 1x 1x 1x 1x   1x             1x                   1x 1x 1x 1x           1x           1x 1x   1x  
import type {LiftArbitrary} from '#arbitrary'
import {
  predicate,
  testPredicateEquivalence,
  tinyPositive,
  tinyString,
} from '#arbitrary'
import type {LiftEquivalence} from '#law'
import {Monoid as MO, Semigroup as SE} from '@effect/typeclass'
import {
  Equivalence as EQ,
  Function as FN,
  Number as NU,
  Predicate as PR,
  Struct as ST,
  String as STR,
} from 'effect'
import type {Kind, TypeLambda} from 'effect/HKT'
import fc from 'fast-check'
import type {GivenConcerns, ParameterizedGiven} from '../parameterized/given.js'
import {unfoldMonomorphicGiven} from './given.js'
import {monoPredicateArbitrary} from './mono.js'
 
/**
 * An underlying type for monomorphic typeclass tests that is an alternative to
 * the `Mono` type, this type is an object vs. `Mono` which is an array.  This
 * lets you test laws on higher order datatypes that require an object as only
 * parameter, for example React functional components.
 * @category monomorphic
 */
export interface MonoProps {
  x: number
  y: string
}
 
/**
 * An arbitrary for the alternative monomorphic underlying type `MonoProps`.
 * @category monomorphic
 */
export const propsArbitrary: fc.Arbitrary<MonoProps> = fc.record({
  x: tinyPositive,
  y: tinyString,
})
 
/**
 * An arbitrary predicate the alternative monomorphic underlying type
 * `MonoProps`.
 * @category monomorphic
 */
export const propsPredicateArbitrary: fc.Arbitrary<PR.Predicate<MonoProps>> =
  predicate<MonoProps>()
 
/**
 * Monoid instance for the `MonoProps` type.
 * @category monomorphic
 */
export const propsMonoid: MO.Monoid<MonoProps> = MO.fromSemigroup(
  SE.struct({
    x: SE.min(NU.Order),
    y: SE.make(FN.dual(2, STR.concat)),
  }),
  {x: 0, y: ''},
)
 
/**
 * An equivalence for the alternative monomorphic underlying type `MonoProps`.
 * @category monomorphic
 */
export const propsEquivalence: EQ.Equivalence<MonoProps> = ST.getEquivalence({
  x: NU.Equivalence,
  y: STR.Equivalence,
})
 
/**
 * Build a sampling equivalence for predicates of the underlying `Mono` type.
 * @category monomorphic
 */
export const propsPredicateEquivalence: EQ.Equivalence<
  PR.Predicate<MonoProps>
> = testPredicateEquivalence(propsArbitrary)
 
/**
 * Unfold options for monomorphic typeclass law tests. This is a version of
 * {@link unfoldMonoGiven} on the underlying type `{x: number; y: string}`.
 * @category monomorphic
 */
export const unfoldPropsGiven = <
  F extends TypeLambda,
  R = never,
  O = unknown,
  E = unknown,
>(
  getEquivalence: LiftEquivalence<F, R, O, E>,
  getArbitrary: LiftArbitrary<F, R, O, E>,
): GivenConcerns<F, MonoProps, MonoProps, MonoProps, R, O, E> =>
  unfoldMonomorphicGiven<F, MonoProps, R, O, E>({
    a: propsArbitrary,
    equalsA: propsEquivalence,
    Monoid: propsMonoid,
    getEquivalence,
    getArbitrary,
  })
 
const contravariant = <
  Typeclass extends TypeLambda,
  F extends TypeLambda,
  R = never,
  O = unknown,
  E = unknown,
>(
  F: Kind<Typeclass, R, O, E, F>,
): ParameterizedGiven<
  Typeclass,
  F,
  MonoProps,
  MonoProps,
  MonoProps,
  R,
  O,
  E
> => ({
  F,
  ...unfoldPropsGiven(
    (<T>(_: EQ.Equivalence<T>) => propsPredicateEquivalence) as LiftEquivalence<
      F,
      R,
      O,
      E
    >,
    (<T>(_: fc.Arbitrary<T>) => monoPredicateArbitrary) as LiftArbitrary<
      F,
      R,
      O,
      E
    >,
  ),
})
 
unfoldPropsGiven.contravariant = contravariant