Skip to content

Commit

Permalink
Streamline test
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiac committed Apr 3, 2024
1 parent d2b7a70 commit d1683b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 74 deletions.
54 changes: 39 additions & 15 deletions test/result.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, it, expect, expectTypeOf, vi} from "vitest"
import {Panic, Ok, Err, Result, Some, None, ErrorWithTag, ResultPromise} from "../src"
import {Panic, Ok, Err, Result, Some, None, ErrorWithTag, ResultPromise, tryFn} from "../src"

export function TestOk<T, E>(value: T): Result<T, E> {
return Ok(value)
Expand Down Expand Up @@ -133,6 +133,44 @@ describe.concurrent("andThen", () => {
const a = TestErr<number, string>("early error")
expect(a.andThen((value) => Ok(value + 1)).unwrapErr()).toEqual(a.unwrapErr())
})

it("can map primitive error type", () => {
const result = TestOk<number, "foo">(1).andThen((num) => {
if (num === null) {
return Err("bar" as const)
}
if (num !== 1) {
return Err("baz" as const)
}
return Ok(num)
})
expectTypeOf(result).toEqualTypeOf<Result<number, "foo" | "bar" | "baz">>()
})

it("can map non-primitive error type", () => {
class Foo extends ErrorWithTag {
readonly tag = "foo"
}

class Bar extends ErrorWithTag {
readonly tag = "bar"
}

class Baz extends ErrorWithTag {
readonly tag = "baz"
}

const result = TestOk<number, Foo>(1).andThen((num) => {

Check failure on line 163 in test/result.test.ts

View workflow job for this annotation

GitHub Actions / test-node

Argument of type '(num: number) => Ok<number, never> | Err<Bar, never> | Err<Baz, never>' is not assignable to parameter of type '(value: number) => Result<number, Bar>'.
if (num === null) {
return Err(new Bar())
}
if (num !== 1) {
return Err(new Baz())
}
return Ok(num)
})
expectTypeOf(result).toEqualTypeOf<Result<number, Foo | Bar | Baz>>()

Check failure on line 172 in test/result.test.ts

View workflow job for this annotation

GitHub Actions / test-node

Type 'Result<number, Foo | Bar | Baz>' does not satisfy the constraint '{ [inspectSymbol]: () => `Ok(${string})` | `Err(${string})`; [iterator]: "Expected: function, Actual: function"; toString: () => `Ok(${string})` | `Err(${string})`; map: "Expected: function, Actual: function"; ... 29 more ...; toJSON: "Expected: function, Actual: function"; }'.
})
})

describe.concurrent("andThenAsync", () => {
Expand All @@ -147,20 +185,6 @@ describe.concurrent("andThenAsync", () => {
a.unwrapErr(),
)
})

it("can map error type", async () => {
const a = TestErr<number, "foo">("foo")
const b = a.andThenAsync(async (value) => {
if (value < 0) {
return Err("bar" as const)
}
if (value > 1) {
return Ok(value)
}
return Err("baz" as const)
})
expectTypeOf(b).toEqualTypeOf<ResultPromise<number, "foo" | "bar" | "baz">>()
})
})

describe.concurrent("expect", () => {
Expand Down
60 changes: 1 addition & 59 deletions test/result_promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
None,
CaughtError,
tryPromise,
tryFn,
} from "../src"
import {TestErr, TestOk} from "./result.test"
import {vi} from "vitest"
Expand Down Expand Up @@ -84,65 +85,6 @@ describe.concurrent("andThenAsync", () => {
const a = TestErrPromise<number, string>(0)
await expect(a.andThenAsync(async (value) => Ok(value + 1)).unwrapErr()).resolves.toEqual(0)
})

it("can map error type", async () => {
const a = TestErrPromise<"foo", number>("foo")
const b = a.andThenAsync(async (value) => {
if (value < 0) {
return Err("bar" as const)
}
if (value > 1) {
return Ok(value)
}
return Err("baz" as const)
})
expectTypeOf(b).toEqualTypeOf<ResultPromise<number, "foo" | "bar" | "baz">>()
})

it("can map error type with complicated error", async () => {
class PrismaError extends ErrorWithTag {
readonly tag = "prisma"
// readonly message = "Prisma error"

// constructor(public readonly error: CaughtError) {
// super()
// }
}

abstract class NotFoundError extends ErrorWithTag {
// category = "not_found"
}

class AccountNotFoundError extends NotFoundError {
readonly tag = "account"
// readonly message = "Account not found"
}

async function fn(): Promise<{id: number} | null> {
return {id: 1}
}

const maybeExpiredToken = await tryPromise(fn())
.mapErr((error) => new PrismaError(error))
.andThen((maybeAccount) => {
if (maybeAccount) {
return Ok(maybeAccount)
}
return Err(new AccountNotFoundError())
})
.andThenAsync(async (account) => {
if (account.id < 0) {
return Err(new AccountNotFoundError())
}

const ye = await tryPromise(fn()).mapErr((error) => new PrismaError(error))
if (ye.isErr) {
return Err(ye.value)
}

return Ok(account)
})
})
})

describe.concurrent("expect", () => {
Expand Down

0 comments on commit d1683b9

Please sign in to comment.