Skip to content

Commit

Permalink
feat(result): expect impl
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsasharegan committed Feb 13, 2018
1 parent 55d873b commit 8719c5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/result.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ export class Result<T, E> {
});
}

public expect(err_msg: string): T {
return this.match({
ok: (t: T) => t,
err: (_: E) => {
throw new Error(err_msg);
},
});
}

public static Ok<T>(val: T): Result<T, any> {
return new Result(Ok(val));
}
Expand Down
10 changes: 10 additions & 0 deletions src/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,13 @@ describe("Result.unwrap", async () => {
expect(() => Err("error").unwrap()).toThrow(Error);
});
});

describe("Result.expect", async () => {
it("should not throw with Ok", async () => {
expect(Ok(2).expect("my error")).toBe(2);
});

it("should throw with Err", async () => {
expect(() => Err("error").expect("my error")).toThrow("my error");
});
});

0 comments on commit 8719c5f

Please sign in to comment.