Skip to content

Commit

Permalink
feat: add resultifyAsync, resultifySync, resultifyPromise
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Jun 7, 2024
1 parent f6ab1a2 commit 3cbbabf
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 33 deletions.
108 changes: 104 additions & 4 deletions src/__tests__/resultify.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it } from '@jest/globals';

import { resultify } from '../resultify';
import { resultify, resultifyAsync, resultifyPromise, resultifySync } from '../resultify';
import { RustlikeResult } from '../RustlikeResult';
import { RustlikeResultAsync } from '../RustlikeResultAsync';

function syncFn(throws: boolean) {
if (throws) {
Expand All @@ -24,6 +26,8 @@ describe(`Test fn \`${resultify.name}\``, () => {

const [result1, result2] = await Promise.all([fn(false), fn(true)]);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
Expand All @@ -36,6 +40,8 @@ describe(`Test fn \`${resultify.name}\``, () => {

const [result1, result2] = await Promise.all([fn(false), fn(true)]);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
Expand All @@ -48,6 +54,8 @@ describe(`Test fn \`${resultify.name}\``, () => {

const [result1, result2] = await Promise.all([fn(false), fn(true)]);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
Expand All @@ -60,6 +68,8 @@ describe(`Test fn \`${resultify.name}\``, () => {

const [result1, result2] = await Promise.all([fn(false), fn(true)]);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
Expand All @@ -68,13 +78,77 @@ describe(`Test fn \`${resultify.name}\``, () => {
});
});

describe(`Test fn \`${resultify.sync.name}\``, () => {
describe(`Test fn \`${resultifyAsync.name}\``, () => {
it('should resultify a sync function', async () => {
const fn = resultifyAsync(syncFn);

const result1 = fn(false);
const result2 = fn(true);

expect(result1).toBeInstanceOf(RustlikeResultAsync);
expect(result2).toBeInstanceOf(RustlikeResultAsync);
await expect(result1.isOk()).resolves.toBe(true);
await expect(result1.unwrap()).resolves.toBe('hello world');
await expect(result2.isErr()).resolves.toBe(true);
await expect(result2.unwrapErr()).resolves.toBeInstanceOf(Error);
expect(((await result2.unwrapErr()) as Error).message).toBe('Some error message');
});

it('should resultify an async function', async () => {
const fn = resultifyAsync(asyncFn);

const result1 = fn(false);
const result2 = fn(true);

expect(result1).toBeInstanceOf(RustlikeResultAsync);
expect(result2).toBeInstanceOf(RustlikeResultAsync);
await expect(result1.isOk()).resolves.toBe(true);
await expect(result1.unwrap()).resolves.toBe('hello world');
await expect(result2.isErr()).resolves.toBe(true);
await expect(result2.unwrapErr()).resolves.toBeInstanceOf(Error);
expect(((await result2.unwrapErr()) as Error).message).toBe('Some error message');
});

it('should resultify a sync function with error type specified', async () => {
const fn = resultifyAsync<Error>()(syncFn);

const result1 = fn(false);
const result2 = fn(true);

expect(result1).toBeInstanceOf(RustlikeResultAsync);
expect(result2).toBeInstanceOf(RustlikeResultAsync);
await expect(result1.isOk()).resolves.toBe(true);
await expect(result1.unwrap()).resolves.toBe('hello world');
await expect(result2.isErr()).resolves.toBe(true);
await expect(result2.unwrapErr()).resolves.toBeInstanceOf(Error);
expect((await result2.unwrapErr()).message).toBe('Some error message');
});

it('should resultify an async function with error type specified', async () => {
const fn = resultifyAsync<Error>()(asyncFn);

const result1 = fn(false);
const result2 = fn(true);

expect(result1).toBeInstanceOf(RustlikeResultAsync);
expect(result2).toBeInstanceOf(RustlikeResultAsync);
await expect(result1.isOk()).resolves.toBe(true);
await expect(result1.unwrap()).resolves.toBe('hello world');
await expect(result2.isErr()).resolves.toBe(true);
await expect(result2.unwrapErr()).resolves.toBeInstanceOf(Error);
expect((await result2.unwrapErr()).message).toBe('Some error message');
});
});

describe(`Test fn \`${resultifySync.name}\``, () => {
it('should resultify a sync function', () => {
const fn = resultify.sync(syncFn);
const fn = resultifySync(syncFn);

const result1 = fn(false);
const result2 = fn(true);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
Expand All @@ -83,17 +157,23 @@ describe(`Test fn \`${resultify.sync.name}\``, () => {
});

it('should resultify a sync function with error type specified', () => {
const fn = resultify.sync<Error>()(syncFn);
const fn = resultifySync<Error>()(syncFn);

const result1 = fn(false);
const result2 = fn(true);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
expect(result2.unwrapErr()).toBeInstanceOf(Error);
expect(result2.unwrapErr().message).toBe('Some error message');
});

it('should be the same as resultify.sync', () => {
expect(resultifySync).toBe(resultify.sync);
});
});

describe(`Test fn \`${resultify.promise.name}\``, () => {
Expand All @@ -106,10 +186,30 @@ describe(`Test fn \`${resultify.promise.name}\``, () => {
resultify.promise<string, Error>(promise2),
]);

expect(result1).toBeInstanceOf(RustlikeResult);
expect(result2).toBeInstanceOf(RustlikeResult);
expect(result1.isOk()).toBe(true);
expect(result1.unwrap()).toBe('hello world');
expect(result2.isErr()).toBe(true);
expect(result2.unwrapErr()).toBeInstanceOf(Error);
expect(result2.unwrapErr().message).toBe('Some error message');
});
});

describe(`Test fn \`${resultifyPromise.name}\``, () => {
it('should resultify a promise', async () => {
const promise1 = asyncFn(false);
const promise2 = asyncFn(true);

const result1 = resultifyPromise<string, Error>(promise1);
const result2 = resultifyPromise<string, Error>(promise2);

expect(result1).toBeInstanceOf(RustlikeResultAsync);
expect(result2).toBeInstanceOf(RustlikeResultAsync);
await expect(result1.isOk()).resolves.toBe(true);
await expect(result1.unwrap()).resolves.toBe('hello world');
await expect(result2.isErr()).resolves.toBe(true);
await expect(result2.unwrapErr()).resolves.toBeInstanceOf(Error);
expect((await result2.unwrapErr()).message).toBe('Some error message');
});
});
Loading

0 comments on commit 3cbbabf

Please sign in to comment.