From 2a3ea96af1793b1635c172f34761a8512e2dbf12 Mon Sep 17 00:00:00 2001 From: William Boman Date: Fri, 28 Apr 2023 12:46:31 +0200 Subject: chore(Optional): add :and_then() (#1270) --- tests/mason-core/optional_spec.lua | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tests') diff --git a/tests/mason-core/optional_spec.lua b/tests/mason-core/optional_spec.lua index b1a4f41a..fed6deba 100644 --- a/tests/mason-core/optional_spec.lua +++ b/tests/mason-core/optional_spec.lua @@ -92,3 +92,48 @@ describe("Optional.ok_or()", function() assert.equals("I'm empty.", result:err_or_nil()) end) end) + +describe("Optional.or_()", function() + it("should run supplier if value is not present", function() + local spy = spy.new(function() + return Optional.of "Hello world!" + end) + assert.same(Optional.of "Hello world!", Optional.empty():or_(spy)) + assert.spy(spy).was_called(1) + + assert.same(Optional.empty(), Optional.empty():or_(Optional.empty)) + end) + + it("should not run supplier if value is present", function() + local spy = spy.new(function() + return Optional.of "Hello world!" + end) + assert.same(Optional.of "Hello world!", Optional.of("Hello world!"):or_(spy)) + assert.spy(spy).was_called(0) + end) +end) + +describe("Optional.and_then()", function() + it("should run supplier if value is present", function() + local spy = spy.new(function(value) + return Optional.of(("%s world!"):format(value)) + end) + assert.same(Optional.of "Hello world!", Optional.of("Hello"):and_then(spy)) + assert.spy(spy).was_called(1) + + assert.same( + Optional.empty(), + Optional.empty():and_then(function() + return Optional.of "Nothing." + end) + ) + end) + + it("should not run supplier if value is not present", function() + local spy = spy.new(function() + return Optional.of "Hello world!" + end) + assert.same(Optional.empty(), Optional.empty():and_then(spy)) + assert.spy(spy).was_called(0) + end) +end) -- cgit v1.2.3-70-g09d2