From 976aa4fbee8a070f362cab6f6ec84e9251a90cf9 Mon Sep 17 00:00:00 2001 From: William Boman Date: Fri, 8 Jul 2022 18:34:38 +0200 Subject: refactor: add mason-schemas and mason-core modules (#29) * refactor: add mason-schemas and move generated filetype map to mason-lspconfig * refactor: add mason-core module --- tests/mason-core/optional_spec.lua | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/mason-core/optional_spec.lua (limited to 'tests/mason-core/optional_spec.lua') diff --git a/tests/mason-core/optional_spec.lua b/tests/mason-core/optional_spec.lua new file mode 100644 index 00000000..e0068b10 --- /dev/null +++ b/tests/mason-core/optional_spec.lua @@ -0,0 +1,63 @@ +local Optional = require "mason-core.optional" +local spy = require "luassert.spy" + +describe("Optional.of_nilable", function() + it("should create empty optionals", function() + local empty = Optional.empty() + assert.is_false(empty:is_present()) + end) + + it("should create non-empty optionals", function() + local empty = Optional.of_nilable "value" + assert.is_true(empty:is_present()) + end) + + it("should use memoized empty value", function() + assert.is_true(Optional.empty() == Optional.empty()) + end) +end) + +describe("Optional.get()", function() + it("should map non-empty values", function() + local str = Optional.of_nilable("world!") + :map(function(val) + return "Hello " .. val + end) + :get() + assert.equals("Hello world!", str) + end) + + it("should raise error when getting empty value", function() + local err = assert.has_error(function() + Optional.empty():get() + end) + assert.equals("No value present.", err) + end) +end) + +describe("Optional.or_else()", function() + it("should use .or_else() value if empty", function() + local value = Optional.empty():or_else "Hello!" + assert.equals("Hello!", value) + end) + + it("should not use .or_else() value if not empty", function() + local value = Optional.of_nilable("Good bye!"):or_else "Hello!" + assert.equals("Good bye!", value) + end) +end) + +describe("Optional.if_present()", function() + it("should not call .if_present() if value is empty", function() + local present = spy.new() + Optional.empty():if_present(present) + assert.spy(present).was_not_called() + end) + + it("should call .if_present() if value is not empty", function() + local present = spy.new() + Optional.of_nilable("value"):if_present(present) + assert.spy(present).was_called(1) + assert.spy(present).was_called_with "value" + end) +end) -- cgit v1.2.3-70-g09d2