Implement Result
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import {expect, test} from 'bun:test'
|
||||
|
||||
import {Result, State} from 'result'
|
||||
|
||||
test('base case Ok', () => {
|
||||
const value = 12
|
||||
|
||||
const res = Result.ok<number, never>(value)
|
||||
|
||||
expect(res.is_err()).toBeFalse()
|
||||
expect(res.is_ok()).toBeTrue()
|
||||
expect(res.unwrap()).toBe(value)
|
||||
expect(res.expect("Shouldn't happen")).toBe(value)
|
||||
expect(() => res.unwrap_err()).toThrow()
|
||||
expect(() => res.expect_err("Error")).toThrow()
|
||||
expect(res.state()).toBe(State.OK)
|
||||
expect(() => res.as_error()).toThrow()
|
||||
|
||||
expect(res.is_ok_and(v => v === value)).toBeTrue()
|
||||
expect(res.map(v => v + 1).unwrap()).toBe(value + 1)
|
||||
expect(res.ok().unwrap()).toBe(value)
|
||||
expect(res.map_or(0, v => v + 1)).toBe(value + 1)
|
||||
expect(res.map_or_else(() => 0, v => v + 1)).toBe(value + 1)
|
||||
|
||||
const handler = (v: number) => expect(v).toBe(value)
|
||||
res.inspect(handler)
|
||||
|
||||
expect(res.unwrap_or(0)).toBe(value)
|
||||
expect(res.unwrap_or_else(() => 0)).toBe(value)
|
||||
|
||||
expect(res.is_err_and(() => true)).toBeFalse()
|
||||
expect(res.err().is_none()).toBeTrue()
|
||||
|
||||
const handler_err = (_v: never) => {
|
||||
throw "Shouldn't happen"
|
||||
}
|
||||
expect(res.map_err(handler_err).unwrap()).toBe(value)
|
||||
expect(res.inspect_err(handler_err).unwrap()).toBe(value)
|
||||
|
||||
const value2 = value + 1
|
||||
const res2 = Result.ok<number, never>(value2)
|
||||
expect(res.and(res2).unwrap()).toBe(value2)
|
||||
expect(res.and_then(v => Result.ok(v + 2)).unwrap()).toBe(value + 2)
|
||||
expect(res.or(res2).unwrap()).toBe(value)
|
||||
expect(res.or_else(handler_err).unwrap()).toBe(value)
|
||||
})
|
||||
|
||||
test('base case Error', () => {
|
||||
const value = 12
|
||||
|
||||
const res = Result.error<string, number>(value)
|
||||
|
||||
expect(res.is_err()).toBeTrue()
|
||||
expect(res.is_ok()).toBeFalse()
|
||||
expect(res.unwrap_err()).toBe(value)
|
||||
expect(res.expect_err("Shouldn't happen")).toBe(value)
|
||||
expect(() => res.unwrap()).toThrow()
|
||||
expect(() => res.expect("Error")).toThrow()
|
||||
expect(res.state()).toBe(State.ERROR)
|
||||
expect(res.as_error().unwrap_err()).toBe(value)
|
||||
|
||||
const handle_throw = (_v: string) => {
|
||||
throw "Shouldn't happen"
|
||||
}
|
||||
expect(res.is_ok_and(handle_throw)).toBeFalse()
|
||||
expect(res.map(handle_throw).unwrap_err()).toBe(value)
|
||||
expect(res.err().unwrap()).toBe(value)
|
||||
expect(res.map_or(0, handle_throw)).toBe(0)
|
||||
expect(res.map_or_else(() => 0, handle_throw)).toBe(0)
|
||||
|
||||
const handler = (v: number) => expect(v).toBe(value)
|
||||
res.inspect_err(handler)
|
||||
|
||||
expect(res.unwrap_or("yes")).toBe("yes")
|
||||
expect(res.unwrap_or_else(() => "yes")).toBe("yes")
|
||||
|
||||
expect(res.is_err_and(() => true)).toBeTrue()
|
||||
expect(res.ok().is_none()).toBeTrue()
|
||||
|
||||
expect(res.map_err(v => v + 1).unwrap_err()).toBe(value + 1)
|
||||
res.inspect_err(v => expect(v).toBe(value))
|
||||
|
||||
const value2 = "yeet"
|
||||
const res2 = Result.ok<string, number>(value2)
|
||||
expect(res.and(res2).unwrap_err()).toBe(value)
|
||||
expect(res.and_then(handle_throw).unwrap_err()).toBe(value)
|
||||
expect(res.or(res2).unwrap()).toBe(value2)
|
||||
expect(res.or_else(v => Result.ok(`${v}`)).unwrap()).toBe(`${value}`)
|
||||
})
|
||||
Reference in New Issue
Block a user