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

100% Statements 42/42
100% Branches 4/4
100% Functions 3/3
100% Lines 42/42

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           9x 9x   9x 9x 9x 9x 9x 9x 9x   1x             4x 4x 4x 4x 4x 4x           4x 4x   1x  
import type {LiftArbitrary} from '#arbitrary'
import {
  option,
  predicate,
  stringKeyRecord,
  testPredicateEquivalence,
  testUnaryEquivalence,
  tinyInteger,
} from '#arbitrary'
import type {LiftEquivalence} from '#law'
import {Monoid} from '@effect/typeclass'
import {MonoidSum} from '@effect/typeclass/data/Number'
import {getOptionalMonoid} from '@effect/typeclass/data/Option'
import {Number as NU, Order as OD, Predicate as PR, Record as RC} from 'effect'
import {type Equivalence} from 'effect/Equivalence'
import {constant} from 'effect/Function'
import type {Kind, TypeLambda} from 'effect/HKT'
import {getEquivalence, getOrder, type Option} from 'effect/Option'
import fc from 'fast-check'
import type {GivenConcerns, ParameterizedGiven} from '../parameterized/given.js'
import {unfoldMonomorphicGiven} from './given.js'
 
/**
 * The underlying type used for monomorphic typeclass law tests.
 * This means that, for example, if we are testing the `Array`
 * datatype, the actual type used in the tests will be
 * `Array<Mono> ≡ Option<Option<number>>`.
 * @category monomorphic
 */
export type Mono = Option<number>
 
/**
 * An arbitrary for the underlying type of the
 * {@link vitest.testTypeclassLaws} unit under test.
 * @category monomorphic
 */
export const monoArbitrary: fc.Arbitrary<Mono> = option(tinyInteger)
 
/**
 * Arbitrary for a record with string keys and `Mono` values.
 * @category monomorphic
 */
export const monoRecordArbitrary: fc.Arbitrary<
  RC.ReadonlyRecord<string, Mono>
> = stringKeyRecord(monoArbitrary)
 
/** @category monomorphic */
export const monoPredicateArbitrary: fc.Arbitrary<PR.Predicate<Mono>> =
  predicate<Mono>()
 
/**
 * The equivalence used for {@link vitest.testTypeclassLaws}.
 * @category monomorphic
 */
export const monoEquivalence: Equivalence<Mono> = getEquivalence(NU.Equivalence)
 
/**
 * Equivalence for a record with string keys and `Mono` values.
 * @category monomorphic
 */
export const monoRecordEquivalence: Equivalence<
  RC.ReadonlyRecord<string, Mono>
> = RC.getEquivalence(monoEquivalence)
 
/**
 * The order used for {@link vitest.testTypeclassLaws}.
 * @category monomorphic
 */
export const monoOrder: OD.Order<Mono> = getOrder(NU.Order)
 
/**
 * Monoid instance for the `Mono` type.
 * @category monomorphic
 */
export const monoMonoid: Monoid.Monoid<Mono> = getOptionalMonoid(MonoidSum)
 
/**
 * Build a sampling equivalence between functions of type
 * `(numArray: Mono) ⇒ A`.
 * @category monomorphic
 */
export const getMonoUnaryEquivalence = <A>(
  equalsA: Equivalence<A>,
): Equivalence<(numArray: Mono) => A> =>
  testUnaryEquivalence(monoArbitrary, equalsA)
 
/**
 * Build a sampling equivalence for predicates of the underlying `Mono` type.
 * @category monomorphic
 */
export const monoPredicateEquivalence: Equivalence<PR.Predicate<Mono>> =
  testPredicateEquivalence(monoArbitrary)
 
/**
 * Unfold the options for monomorphic typeclass law tests on the underlying type
 * `Mono` from a function that will lift equivalence in the higher-kinded type
 * under test, and one that will lift an arbitrary. This function is used inside
 * typeclass law test code to unfold the requirements of their law predicates.
 * @category monomorphic
 */
export const unfoldMonoGiven = <
  F extends TypeLambda,
  R = never,
  O = unknown,
  E = unknown,
>(
  getEquivalence: LiftEquivalence<F, R, O, E>,
  getArbitrary: LiftArbitrary<F, R, O, E>,
): GivenConcerns<F, Mono, Mono, Mono, R, O, E> =>
  unfoldMonomorphicGiven<F, Mono, R, O, E>({
    a: monoArbitrary,
    equalsA: monoEquivalence,
    Monoid: monoMonoid,
    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, Mono, Mono, Mono, R, O, E> => ({
  F,
  ...unfoldMonoGiven(
    constant(monoPredicateEquivalence) as LiftEquivalence<F, R, O, E>,
    (<T>(_: fc.Arbitrary<T>) => monoPredicateArbitrary) as LiftArbitrary<
      F,
      R,
      O,
      E
    >,
  ),
})
 
unfoldMonoGiven.contravariant = contravariant