| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {CompilerError} from '../CompilerError'; |
| 9 | import {Err, Ok, Result} from '../Utils/Result'; |
| 10 | |
| 11 | function addMax10(a: number, b: number): Result<number, string> { |
| 12 | const n = a + b; |
| 13 | return n > 10 ? Err(`${n} is too high`) : Ok(n); |
| 14 | } |
| 15 | |
| 16 | function onlyFoo(foo: string): Result<string, string> { |
| 17 | return foo === 'foo' ? Ok(foo) : Err(foo); |
| 18 | } |
| 19 | |
| 20 | class CustomDummyError extends Error {} |
| 21 | |
| 22 | describe('Result', () => { |
| 23 | test('.map', () => { |
| 24 | expect(addMax10(1, 1).map(n => n * 2)).toEqual(Ok(4)); |
| 25 | expect(addMax10(10, 10).map(n => n * 2)).toEqual(Err('20 is too high')); |
| 26 | }); |
| 27 | |
| 28 | test('.mapErr', () => { |
| 29 | expect(addMax10(1, 1).mapErr(e => `not a number: ${e}`)).toEqual(Ok(2)); |
| 30 | expect(addMax10(10, 10).mapErr(e => `couldn't add: ${e}`)).toEqual( |
| 31 | Err("couldn't add: 20 is too high"), |
| 32 | ); |
| 33 | }); |
| 34 | |
| 35 | test('.mapOr', () => { |
| 36 | expect(onlyFoo('foo').mapOr(42, v => v.length)).toEqual(3); |
| 37 | expect(onlyFoo('bar').mapOr(42, v => v.length)).toEqual(42); |
| 38 | }); |
| 39 | |
| 40 | test('.mapOrElse', () => { |
| 41 | expect( |
| 42 | onlyFoo('foo').mapOrElse( |
| 43 | () => 42, |
| 44 | v => v.length, |
| 45 | ), |
| 46 | ).toEqual(3); |
| 47 | expect( |
| 48 | onlyFoo('bar').mapOrElse( |
| 49 | () => 42, |
| 50 | v => v.length, |
| 51 | ), |
| 52 | ).toEqual(42); |
| 53 | }); |
| 54 | |
| 55 | test('.andThen', () => { |
| 56 | expect(addMax10(1, 1).andThen(n => Ok(n * 2))).toEqual(Ok(4)); |
| 57 | expect(addMax10(10, 10).andThen(n => Ok(n * 2))).toEqual( |
| 58 | Err('20 is too high'), |
| 59 | ); |
| 60 | }); |
| 61 | |
| 62 | test('.and', () => { |
| 63 | expect(addMax10(1, 1).and(Ok(4))).toEqual(Ok(4)); |
| 64 | expect(addMax10(10, 10).and(Ok(4))).toEqual(Err('20 is too high')); |
| 65 | expect(addMax10(1, 1).and(Err('hehe'))).toEqual(Err('hehe')); |
| 66 | expect(addMax10(10, 10).and(Err('hehe'))).toEqual(Err('20 is too high')); |
| 67 | }); |
| 68 | |
| 69 | test('.or', () => { |
| 70 | expect(addMax10(1, 1).or(Ok(4))).toEqual(Ok(2)); |
| 71 | expect(addMax10(10, 10).or(Ok(4))).toEqual(Ok(4)); |
| 72 | expect(addMax10(1, 1).or(Err('hehe'))).toEqual(Ok(2)); |
| 73 | expect(addMax10(10, 10).or(Err('hehe'))).toEqual(Err('hehe')); |
| 74 | }); |
| 75 | |
| 76 | test('.orElse', () => { |
| 77 | expect(addMax10(1, 1).orElse(str => Err(str.toUpperCase()))).toEqual(Ok(2)); |
| 78 | expect(addMax10(10, 10).orElse(str => Err(str.toUpperCase()))).toEqual( |
| 79 | Err('20 IS TOO HIGH'), |
| 80 | ); |
| 81 | }); |
| 82 | |
| 83 | test('.isOk', () => { |
| 84 | expect(addMax10(1, 1).isOk()).toBeTruthy(); |
| 85 | expect(addMax10(10, 10).isOk()).toBeFalsy(); |
| 86 | }); |
| 87 | |
| 88 | test('.isErr', () => { |
| 89 | expect(addMax10(1, 1).isErr()).toBeFalsy(); |
| 90 | expect(addMax10(10, 10).isErr()).toBeTruthy(); |
| 91 | }); |
| 92 | |
| 93 | test('.expect', () => { |
| 94 | expect(addMax10(1, 1).expect('a number under 10')).toEqual(2); |
| 95 | expect(() => { |
| 96 | addMax10(10, 10).expect('a number under 10'); |
| 97 | }).toThrowErrorMatchingInlineSnapshot( |
| 98 | `"a number under 10: 20 is too high"`, |
| 99 | ); |
| 100 | }); |
| 101 | |
| 102 | test('.expectErr', () => { |
| 103 | expect(() => { |
| 104 | addMax10(1, 1).expectErr('a number under 10'); |
| 105 | }).toThrowErrorMatchingInlineSnapshot(`"a number under 10: 2"`); |
| 106 | expect(addMax10(10, 10).expectErr('a number under 10')).toEqual( |
| 107 | '20 is too high', |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | test('.unwrap', () => { |
| 112 | expect(addMax10(1, 1).unwrap()).toEqual(2); |
| 113 | expect(() => { |
| 114 | addMax10(10, 10).unwrap(); |
| 115 | }).toThrowErrorMatchingInlineSnapshot( |
| 116 | `"Can't unwrap \`Err\` to \`Ok\`: 20 is too high"`, |
| 117 | ); |
| 118 | expect(() => { |
| 119 | Err(new CustomDummyError('oops')).unwrap(); |
| 120 | }).toThrowErrorMatchingInlineSnapshot(`"oops"`); |
| 121 | }); |
| 122 | |
| 123 | test('.unwrapOr', () => { |
| 124 | expect(addMax10(1, 1).unwrapOr(4)).toEqual(2); |
| 125 | expect(addMax10(10, 10).unwrapOr(4)).toEqual(4); |
| 126 | }); |
| 127 | |
| 128 | test('.unwrapOrElse', () => { |
| 129 | expect(addMax10(1, 1).unwrapOrElse(() => 4)).toEqual(2); |
| 130 | expect(addMax10(10, 10).unwrapOrElse(s => s.length)).toEqual(14); |
| 131 | }); |
| 132 | |
| 133 | test('.unwrapErr', () => { |
| 134 | expect(() => { |
| 135 | addMax10(1, 1).unwrapErr(); |
| 136 | }).toThrowErrorMatchingInlineSnapshot( |
| 137 | `"Can't unwrap \`Ok\` to \`Err\`: 2"`, |
| 138 | ); |
| 139 | expect(addMax10(10, 10).unwrapErr()).toEqual('20 is too high'); |
| 140 | expect(() => { |
| 141 | Ok(new CustomDummyError('oops')).unwrapErr(); |
| 142 | }).toThrowErrorMatchingInlineSnapshot(`"oops"`); |
| 143 | }); |
| 144 | }); |