|
1 | 1 | import { pipe } from "rambda"; |
2 | 2 | import { chainE, fromAsync, fromPromise, Left, mapE, mapLeftE, matchE, Right, tryCatch, unwrapOrE } from "../src/either"; |
| 3 | +import { equalsE } from "../src/either/Either"; |
3 | 4 |
|
4 | 5 | describe("Either", () => { |
5 | 6 | it("Right.map applies function to value", () => { |
@@ -88,6 +89,30 @@ describe("Either", () => { |
88 | 89 | expect(new Left("x").isLeft()).toBe(true); |
89 | 90 | }); |
90 | 91 |
|
| 92 | + it("Right.equals returns true for equal Right values", () => { |
| 93 | + const a = new Right(42); |
| 94 | + const b = new Right(42); |
| 95 | + expect(a.equals(b)).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it("Left.equals returns true for equal Left values", () => { |
| 99 | + const a = new Left("error"); |
| 100 | + const b = new Left("error"); |
| 101 | + expect(a.equals(b)).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it("Right.equals returns false for different Right values", () => { |
| 105 | + const a = new Right(42); |
| 106 | + const b = new Right(43); |
| 107 | + expect(a.equals(b)).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + it("Left.equals returns false for different Left values", () => { |
| 111 | + const a = new Left("error"); |
| 112 | + const b = new Left("different error"); |
| 113 | + expect(a.equals(b)).toBe(false); |
| 114 | + }); |
| 115 | + |
91 | 116 | it("tryCatch should return Right when function succeeds", () => { |
92 | 117 | const result = tryCatch(() => JSON.parse('{"a":1}')); |
93 | 118 | expect(result.isRight()).toBe(true); |
@@ -294,4 +319,53 @@ describe("Either - Curried Helpers", () => { |
294 | 319 | ); |
295 | 320 | expect(result).toBe("error: Bad Request"); |
296 | 321 | }); |
| 322 | + |
| 323 | + it("should return true for equal Right values with curried equals", () => { |
| 324 | + const result = pipe( |
| 325 | + new Right(42), |
| 326 | + equalsE(new Right(42)) |
| 327 | + ); |
| 328 | + expect(result).toBe(true); |
| 329 | + }); |
| 330 | + |
| 331 | + it("should return false for different Right values with curried equals", () => { |
| 332 | + const result = pipe( |
| 333 | + new Right(42), |
| 334 | + equalsE(new Right(43)) |
| 335 | + ); |
| 336 | + expect(result).toBe(false); |
| 337 | + }); |
| 338 | + |
| 339 | + it("should return true for equal Left values with curried equals", () => { |
| 340 | + const result = pipe( |
| 341 | + new Left("error"), |
| 342 | + equalsE(new Left("error")) |
| 343 | + ); |
| 344 | + expect(result).toBe(true); |
| 345 | + }); |
| 346 | + |
| 347 | + it("should return false for different Left values with curried equals", () => { |
| 348 | + const result = pipe( |
| 349 | + new Left("error"), |
| 350 | + equalsE(new Left("different error")) |
| 351 | + ); |
| 352 | + expect(result).toBe(false); |
| 353 | + }); |
| 354 | + |
| 355 | + it("should return false for different Left and Right values with curried equals", () => { |
| 356 | + const result = pipe( |
| 357 | + new Left<string, number>("error"), |
| 358 | + equalsE(new Right(42)) |
| 359 | + ); |
| 360 | + expect(result).toBe(false); |
| 361 | + }); |
| 362 | + |
| 363 | + it("should return false for different Right and Left values with curried equals", () => { |
| 364 | + const result = pipe( |
| 365 | + new Right<string, number>(42), |
| 366 | + equalsE(new Left("error")) |
| 367 | + ); |
| 368 | + expect(result).toBe(false); |
| 369 | + }); |
| 370 | + |
297 | 371 | }); |
0 commit comments