Skip to content

Commit

Permalink
add asserts return types
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 1, 2025
1 parent ca1cff1 commit b8a86fa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
17 changes: 17 additions & 0 deletions packages/effect/test/assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,21 @@ a`
)
})
})

it("assertMatch", () => {
Util.assertMatch("abc", /b/)
Util.throws(() => Util.assertMatch("a", /b/), (err) => {
Util.assertTrue(err instanceof Error)
Util.strictEqual(
err.message,
`Expected
a
to match
/b/`
)
})
})
})
32 changes: 26 additions & 6 deletions packages/effect/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,54 @@ export function throws(thunk: () => void, error?: Error | ((u: unknown) => undef
// Option
// ----------------------------

export function assertNone<A>(option: Option.Option<A>, ..._: Array<never>) {
export function assertNone<A>(option: Option.Option<A>, ..._: Array<never>): asserts option is Option.None<never> {
deepStrictEqual(option, Option.none())
}

export function assertSome<A>(option: Option.Option<A>, expected: A, ..._: Array<never>) {
export function assertSome<A>(
option: Option.Option<A>,
expected: A,
..._: Array<never>
): asserts option is Option.Some<A> {
deepStrictEqual(option, Option.some(expected))
}

// ----------------------------
// Either
// ----------------------------

export function assertLeft<R, L>(either: Either.Either<R, L>, expected: L, ..._: Array<never>) {
export function assertLeft<R, L>(
either: Either.Either<R, L>,
expected: L,
..._: Array<never>
): asserts either is Either.Left<L, never> {
deepStrictEqual(either, Either.left(expected))
}

export function assertRight<R, L>(either: Either.Either<R, L>, expected: R, ..._: Array<never>) {
export function assertRight<R, L>(
either: Either.Either<R, L>,
expected: R,
..._: Array<never>
): asserts either is Either.Right<never, R> {
deepStrictEqual(either, Either.right(expected))
}

// ----------------------------
// Exit
// ----------------------------

export function assertFailure<A, E>(exit: Exit.Exit<A, E>, expected: Cause.Cause<E>, ..._: Array<never>) {
export function assertFailure<A, E>(
exit: Exit.Exit<A, E>,
expected: Cause.Cause<E>,
..._: Array<never>
): asserts exit is Exit.Failure<never, E> {
deepStrictEqual(exit, Exit.failCause(expected))
}

export function assertSuccess<A, E>(exit: Exit.Exit<A, E>, expected: A, ..._: Array<never>) {
export function assertSuccess<A, E>(
exit: Exit.Exit<A, E>,
expected: A,
..._: Array<never>
): asserts exit is Exit.Success<A, never> {
deepStrictEqual(exit, Exit.succeed(expected))
}

0 comments on commit b8a86fa

Please sign in to comment.