diff options
| author | William Boman <william@redwill.se> | 2022-03-26 13:41:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-26 13:41:50 +0100 |
| commit | 212d17a039da449043b67529c29851db37acc236 (patch) | |
| tree | 38411b14487895cef0d7648e198b79fd28793fe6 /tests/core/optional_spec.lua | |
| parent | run autogen_metadata.lua (diff) | |
| download | mason-212d17a039da449043b67529c29851db37acc236.tar mason-212d17a039da449043b67529c29851db37acc236.tar.gz mason-212d17a039da449043b67529c29851db37acc236.tar.bz2 mason-212d17a039da449043b67529c29851db37acc236.tar.lz mason-212d17a039da449043b67529c29851db37acc236.tar.xz mason-212d17a039da449043b67529c29851db37acc236.tar.zst mason-212d17a039da449043b67529c29851db37acc236.zip | |
add async managers (#536)
Diffstat (limited to 'tests/core/optional_spec.lua')
| -rw-r--r-- | tests/core/optional_spec.lua | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/core/optional_spec.lua b/tests/core/optional_spec.lua new file mode 100644 index 00000000..02f8800e --- /dev/null +++ b/tests/core/optional_spec.lua @@ -0,0 +1,63 @@ +local Optional = require "nvim-lsp-installer.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) |
