| 3 | import * as FiberId from "effect/FiberId" |
| 4 | |
| 5 | export const causesArb = <E>( |
| 6 | n: number, |
| 7 | error: fc.Arbitrary<E>, |
| 8 | defect: fc.Arbitrary<unknown> |
| 9 | ): fc.Arbitrary<Cause.Cause<E>> => { |
| 10 | const fiberId: fc.Arbitrary<FiberId.FiberId> = fc.tuple( |
| 11 | fc.integer(), |
| 12 | fc.integer() |
| 13 | ).map(([a, b]) => FiberId.make(a, b)) |
| 14 | |
| 15 | const empty = fc.constant(Cause.empty) |
| 16 | const failure = error.map(Cause.fail) |
| 17 | const die = defect.map(Cause.die) |
| 18 | const interrupt = fiberId.map(Cause.interrupt) |
| 19 | |
| 20 | const sequential = (n: number): fc.Arbitrary<Cause.Cause<E>> => { |
| 21 | return fc.integer({ min: 1, max: n - 1 }).chain((i) => |
| 22 | causesN(i).chain((left) => causesN(n - i).map((right) => Cause.sequential(left, right))) |
| 23 | ) |
| 24 | } |
| 25 | |
| 26 | const parallel = (n: number): fc.Arbitrary<Cause.Cause<E>> => { |
| 27 | return fc.integer({ min: 1, max: n - 1 }).chain((i) => |
| 28 | causesN(i).chain((left) => causesN(n - i).map((right) => Cause.parallel(left, right))) |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | const causesN = (n: number): fc.Arbitrary<Cause.Cause<E>> => { |
| 33 | if (n === 1) { |
| 34 | return fc.oneof(empty, failure, die, interrupt) |
| 35 | } |
| 36 | return fc.oneof(sequential(n), parallel(n)) |
| 37 | } |
| 38 | |
| 39 | return causesN(n) |
| 40 | } |
| 41 | |
| 42 | export const causes: fc.Arbitrary<Cause.Cause<string>> = causesArb( |
| 43 | 1, |