diff options
| author | William Boman <william@redwill.se> | 2022-05-17 13:49:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-17 13:49:18 +0200 |
| commit | 9fa0bb2822af391db96687ad6ddb66ddb3529c58 (patch) | |
| tree | 82d5838c98e5c0b3f273f23326099383be6b8c0e /tests | |
| parent | run autogen_metadata.lua (diff) | |
| download | mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.tar mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.tar.gz mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.tar.bz2 mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.tar.lz mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.tar.xz mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.tar.zst mason-9fa0bb2822af391db96687ad6ddb66ddb3529c58.zip | |
chore(functional): restructure and extend functional modules (#703)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/core/functional/data_spec.lua | 28 | ||||
| -rw-r--r-- | tests/core/functional/function_spec.lua | 142 | ||||
| -rw-r--r-- | tests/core/functional/list_spec.lua | 87 | ||||
| -rw-r--r-- | tests/core/functional/logic_spec.lua | 44 | ||||
| -rw-r--r-- | tests/core/functional/number_spec.lua | 50 | ||||
| -rw-r--r-- | tests/core/functional/relation_spec.lua | 36 | ||||
| -rw-r--r-- | tests/core/functional/string_spec.lua | 17 | ||||
| -rw-r--r-- | tests/core/functional/table_spec.lua | 7 | ||||
| -rw-r--r-- | tests/core/functional/type_spec.lua | 26 | ||||
| -rw-r--r-- | tests/core/functional_spec.lua | 247 |
10 files changed, 437 insertions, 247 deletions
diff --git a/tests/core/functional/data_spec.lua b/tests/core/functional/data_spec.lua new file mode 100644 index 00000000..b89176e2 --- /dev/null +++ b/tests/core/functional/data_spec.lua @@ -0,0 +1,28 @@ +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: data", function() + it("creates enums", function() + local colors = _.enum { + "BLUE", + "YELLOW", + } + assert.same({ + ["BLUE"] = "BLUE", + ["YELLOW"] = "YELLOW", + }, colors) + end) + + it("creates sets", function() + local colors = _.set_of { + "BLUE", + "YELLOW", + "BLUE", + "RED", + } + assert.same({ + ["BLUE"] = true, + ["YELLOW"] = true, + ["RED"] = true, + }, colors) + end) +end) diff --git a/tests/core/functional/function_spec.lua b/tests/core/functional/function_spec.lua new file mode 100644 index 00000000..4b9cbba6 --- /dev/null +++ b/tests/core/functional/function_spec.lua @@ -0,0 +1,142 @@ +local spy = require "luassert.spy" +local match = require "luassert.match" +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: function", function() + it("curries functions", function() + local function sum(...) + local res = 0 + for i = 1, select("#", ...) do + res = res + select(i, ...) + end + return res + end + local arity0 = _.curryN(sum, 0) + local arity1 = _.curryN(sum, 1) + local arity2 = _.curryN(sum, 2) + local arity3 = _.curryN(sum, 3) + + assert.equals(0, arity0(42)) + assert.equals(42, arity1(42)) + assert.equals(3, arity2(1)(2)) + assert.equals(3, arity2(1, 2)) + assert.equals(6, arity3(1)(2)(3)) + assert.equals(6, arity3(1, 2, 3)) + + -- should discard superfluous args + assert.equals(0, arity1(0, 10, 20, 30)) + end) + + it("coalesces first non-nil value", function() + assert.equal("Hello World!", _.coalesce(nil, nil, "Hello World!", "")) + end) + + it("should compose functions", function() + local function add(x) + return function(y) + return y + x + end + end + local function subtract(x) + return function(y) + return y - x + end + end + local function multiply(x) + return function(y) + return y * x + end + end + + local big_maths = _.compose(add(1), subtract(3), multiply(5)) + + assert.equals(23, big_maths(5)) + end) + + it("should not allow composing no functions", function() + local e = assert.error(function() + _.compose() + end) + assert.equals("compose requires at least one function", e) + end) + + it("should partially apply functions", function() + local funcy = spy.new() + local partially_funcy = _.partial(funcy, "a", "b", "c") + partially_funcy("d", "e", "f") + assert.spy(funcy).was_called_with("a", "b", "c", "d", "e", "f") + end) + + it("should partially apply functions with nil arguments", function() + local funcy = spy.new() + local partially_funcy = _.partial(funcy, "a", nil, "c") + partially_funcy("d", nil, "f") + assert.spy(funcy).was_called_with("a", nil, "c", "d", nil, "f") + end) + + it("memoizes functions with default cache mechanism", function() + local expensive_function = spy.new(function(s) + return s + end) + local memoized_fn = _.memoize(expensive_function) + assert.equal("key", memoized_fn "key") + assert.equal("key", memoized_fn "key") + assert.equal("new_key", memoized_fn "new_key") + assert.spy(expensive_function).was_called(2) + end) + + it("memoizes function with custom cache mechanism", function() + local expensive_function = spy.new(function(arg1, arg2) + return arg1 .. arg2 + end) + local memoized_fn = _.memoize(expensive_function, function(arg1, arg2) + return arg1 .. arg2 + end) + assert.equal("key1key2", memoized_fn("key1", "key2")) + assert.equal("key1key2", memoized_fn("key1", "key2")) + assert.equal("key1key3", memoized_fn("key1", "key3")) + assert.spy(expensive_function).was_called(2) + end) + + it("should evaluate functions lazily", function() + local impl = spy.new(function() + return {}, {} + end) + local lazy_fn = _.lazy(impl) + assert.spy(impl).was_called(0) + local a, b = lazy_fn() + assert.spy(impl).was_called(1) + assert.is_true(match.is_table()(a)) + assert.is_true(match.is_table()(b)) + local new_a, new_b = lazy_fn() + assert.spy(impl).was_called(1) + assert.is_true(match.is_ref(a)(new_a)) + assert.is_true(match.is_ref(b)(new_b)) + end) + + it("should support nil return values in lazy functions", function() + local lazy_fn = _.lazy(function() + return nil, 2 + end) + local a, b = lazy_fn() + assert.is_nil(a) + assert.equal(2, b) + end) + + it("should provide identity value", function() + local obj = {} + assert.equals(2, _.identity(2)) + assert.equals(obj, _.identity(obj)) + end) + + it("should always return bound value", function() + local obj = {} + assert.equals(2, _.always(2)()) + assert.equals(obj, _.always(obj)()) + end) + + it("true is true and false is false", function() + assert.is_true(_.T()) + assert.is_false(_.F()) + end) +end) diff --git a/tests/core/functional/list_spec.lua b/tests/core/functional/list_spec.lua new file mode 100644 index 00000000..c553a7d0 --- /dev/null +++ b/tests/core/functional/list_spec.lua @@ -0,0 +1,87 @@ +local spy = require "luassert.spy" +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: list", function() + it("should produce list without nils", function() + assert.same({ 1, 2, 3, 4 }, _.list_not_nil(nil, 1, 2, nil, 3, nil, 4, nil)) + end) + + it("makes a shallow copy of a list", function() + local list = { "BLUE", { nested = "TABLE" }, "RED" } + local list_copy = _.list_copy(list) + assert.same({ "BLUE", { nested = "TABLE" }, "RED" }, list_copy) + assert.is_not.is_true(list == list_copy) + assert.is_true(list[2] == list_copy[2]) + end) + + it("reverses lists", function() + local colors = { "BLUE", "YELLOW", "RED" } + assert.same({ + "RED", + "YELLOW", + "BLUE", + }, _.reverse(colors)) + -- should not modify in-place + assert.same({ "BLUE", "YELLOW", "RED" }, colors) + end) + + it("maps over list", function() + local colors = { "BLUE", "YELLOW", "RED" } + assert.same( + { + "LIGHT_BLUE", + "LIGHT_YELLOW", + "LIGHT_RED", + }, + _.map(function(color) + return "LIGHT_" .. color + end, colors) + ) + -- should not modify in-place + assert.same({ "BLUE", "YELLOW", "RED" }, colors) + end) + + it("finds first item that fulfills predicate", function() + local predicate = spy.new(function(item) + return item == "Waldo" + end) + + assert.equal( + "Waldo", + _.find_first(predicate, { + "Where", + "On Earth", + "Is", + "Waldo", + "?", + }) + ) + assert.spy(predicate).was.called(4) + end) + + it("determines whether any item in the list fulfills predicate", function() + local predicate = spy.new(function(item) + return item == "On Earth" + end) + + assert.is_true(_.any(predicate, { + "Where", + "On Earth", + "Is", + "Waldo", + "?", + })) + + assert.spy(predicate).was.called(2) + end) + + it("should iterate list in .each", function() + local list = { "BLUE", "YELLOW", "RED" } + local iterate_fn = spy.new() + _.each(iterate_fn, list) + assert.spy(iterate_fn).was_called(3) + assert.spy(iterate_fn).was_called_with("BLUE", 1) + assert.spy(iterate_fn).was_called_with("YELLOW", 2) + assert.spy(iterate_fn).was_called_with("RED", 3) + end) +end) diff --git a/tests/core/functional/logic_spec.lua b/tests/core/functional/logic_spec.lua new file mode 100644 index 00000000..19ac8bb7 --- /dev/null +++ b/tests/core/functional/logic_spec.lua @@ -0,0 +1,44 @@ +local spy = require "luassert.spy" +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: logic", function() + it("should check that all_pass checks that all predicates pass", function() + local is_waldo = function(i) + return i == "waldo" + end + assert.is_true(_.all_pass { _.T, _.T, is_waldo, _.T } "waldo") + assert.is_false(_.all_pass { _.T, _.T, is_waldo, _.F } "waldo") + assert.is_false(_.all_pass { _.T, _.T, is_waldo, _.T } "waldina") + end) + + it("should branch if_else", function() + local a = spy.new() + local b = spy.new() + _.if_else(_.T, a, b) "a" + _.if_else(_.F, a, b) "b" + assert.spy(a).was_called(1) + assert.spy(a).was_called_with "a" + assert.spy(b).was_called(1) + assert.spy(b).was_called_with "b" + end) + + it("should flip booleans", function() + assert.is_true(_.is_not(false)) + assert.is_false(_.is_not(true)) + end) + + it("should resolve correct cond", function() + local planetary_object = _.cond { + { + _.equals "Moon!", + _.format "to the %s", + }, + { + _.equals "World!", + _.format "Hello %s", + }, + } + assert.equals("Hello World!", planetary_object "World!") + assert.equals("to the Moon!", planetary_object "Moon!") + end) +end) diff --git a/tests/core/functional/number_spec.lua b/tests/core/functional/number_spec.lua new file mode 100644 index 00000000..644547fb --- /dev/null +++ b/tests/core/functional/number_spec.lua @@ -0,0 +1,50 @@ +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: number", function() + it("should negate numbers", function() + assert.equals(-42, _.negate(42)) + assert.equals(42, _.negate(-42)) + end) + + it("should check numbers greater than value", function() + local greater_than_life = _.gt(42) + assert.is_false(greater_than_life(0)) + assert.is_false(greater_than_life(42)) + assert.is_true(greater_than_life(43)) + end) + + it("should check numbers greater or equal than value", function() + local greater_or_equal_to_life = _.gte(42) + assert.is_false(greater_or_equal_to_life(0)) + assert.is_true(greater_or_equal_to_life(42)) + assert.is_true(greater_or_equal_to_life(43)) + end) + + it("should check numbers lower than value", function() + local lesser_than_life = _.lt(42) + assert.is_true(lesser_than_life(0)) + assert.is_false(lesser_than_life(42)) + assert.is_false(lesser_than_life(43)) + end) + + it("should check numbers lower or equal than value", function() + local lesser_or_equal_to_life = _.lte(42) + assert.is_true(lesser_or_equal_to_life(0)) + assert.is_true(lesser_or_equal_to_life(42)) + assert.is_false(lesser_or_equal_to_life(43)) + end) + + it("should increment numbers", function() + local add_5 = _.inc(5) + assert.equals(0, add_5(-5)) + assert.equals(5, add_5(0)) + assert.equals(7, add_5(2)) + end) + + it("should decrement numbers", function() + local subtract_5 = _.dec(5) + assert.equals(5, subtract_5(10)) + assert.equals(-5, subtract_5(0)) + assert.equals(-3, subtract_5(2)) + end) +end) diff --git a/tests/core/functional/relation_spec.lua b/tests/core/functional/relation_spec.lua new file mode 100644 index 00000000..41325cf2 --- /dev/null +++ b/tests/core/functional/relation_spec.lua @@ -0,0 +1,36 @@ +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: relation", function() + it("should check equality", function() + local tbl = {} + local is_tbl = _.equals(tbl) + local is_a = _.equals "a" + local is_42 = _.equals(42) + + assert.is_true(is_tbl(tbl)) + assert.is_true(is_a "a") + assert.is_true(is_42(42)) + assert.is_false(is_a "b") + assert.is_false(is_42(32)) + end) + + it("should check property equality", function() + local fn_key = function() end + local tbl = { a = "a", b = "b", number = 42, [fn_key] = "fun" } + assert.is_true(_.prop_eq("a", "a", tbl)) + assert.is_true(_.prop_eq(fn_key, "fun", tbl)) + assert.is_true(_.prop_eq(fn_key) "fun"(tbl)) + end) + + it("should check whether property satisfies predicate", function() + local obj = { + low = 0, + med = 10, + high = 15, + } + + assert.is_false(_.prop_satisfies(_.gt(10), "low", obj)) + assert.is_false(_.prop_satisfies(_.gt(10), "med")(obj)) + assert.is_true(_.prop_satisfies(_.gt(10)) "high"(obj)) + end) +end) diff --git a/tests/core/functional/string_spec.lua b/tests/core/functional/string_spec.lua new file mode 100644 index 00000000..53575ce4 --- /dev/null +++ b/tests/core/functional/string_spec.lua @@ -0,0 +1,17 @@ +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: string", function() + it("matches string patterns", function() + assert.is_true(_.matches("foo", "foo")) + assert.is_true(_.matches("bar", "foobarbaz")) + assert.is_true(_.matches("ba+r", "foobaaaaaaarbaz")) + + assert.is_false(_.matches("ba+r", "foobharbaz")) + assert.is_false(_.matches("bar", "foobaz")) + end) + + it("should format strings", function() + assert.equals("Hello World!", _.format("%s", "Hello World!")) + assert.equals("special manouvers", _.format("%s manouvers", "special")) + end) +end) diff --git a/tests/core/functional/table_spec.lua b/tests/core/functional/table_spec.lua new file mode 100644 index 00000000..25adff59 --- /dev/null +++ b/tests/core/functional/table_spec.lua @@ -0,0 +1,7 @@ +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: table", function() + it("retrieves property of table", function() + assert.equals("hello", _.prop("a", { a = "hello" })) + end) +end) diff --git a/tests/core/functional/type_spec.lua b/tests/core/functional/type_spec.lua new file mode 100644 index 00000000..b99262b5 --- /dev/null +++ b/tests/core/functional/type_spec.lua @@ -0,0 +1,26 @@ +local _ = require "nvim-lsp-installer.core.functional" + +describe("functional: type", function() + it("should check nil value", function() + assert.is_true(_.is_nil(nil)) + assert.is_false(_.is_nil(1)) + assert.is_false(_.is_nil {}) + assert.is_false(_.is_nil(function() end)) + end) + + it("should check types", function() + local is_fun = _.is "function" + local is_string = _.is "string" + local is_number = _.is "number" + local is_boolean = _.is "boolean" + + assert.is_true(is_fun(function() end)) + assert.is_false(is_fun(1)) + assert.is_true(is_string "") + assert.is_false(is_string(1)) + assert.is_true(is_number(1)) + assert.is_false(is_number "") + assert.is_true(is_boolean(true)) + assert.is_false(is_boolean(1)) + end) +end) diff --git a/tests/core/functional_spec.lua b/tests/core/functional_spec.lua deleted file mode 100644 index 54433629..00000000 --- a/tests/core/functional_spec.lua +++ /dev/null @@ -1,247 +0,0 @@ -local functional = require "nvim-lsp-installer.core.functional" -local spy = require "luassert.spy" -local match = require "luassert.match" - -describe("functional", function() - it("creates enums", function() - local colors = functional.enum { - "BLUE", - "YELLOW", - } - assert.same({ - ["BLUE"] = "BLUE", - ["YELLOW"] = "YELLOW", - }, colors) - end) - - it("creates sets", function() - local colors = functional.set_of { - "BLUE", - "YELLOW", - "BLUE", - "RED", - } - assert.same({ - ["BLUE"] = true, - ["YELLOW"] = true, - ["RED"] = true, - }, colors) - end) - - it("reverses lists", function() - local colors = { "BLUE", "YELLOW", "RED" } - assert.same({ - "RED", - "YELLOW", - "BLUE", - }, functional.list_reverse(colors)) - -- should not modify in-place - assert.same({ "BLUE", "YELLOW", "RED" }, colors) - end) - - it("maps over list", function() - local colors = { "BLUE", "YELLOW", "RED" } - assert.same( - { - "LIGHT_BLUE1", - "LIGHT_YELLOW2", - "LIGHT_RED3", - }, - functional.list_map(function(color, i) - return "LIGHT_" .. color .. i - end, colors) - ) - -- should not modify in-place - assert.same({ "BLUE", "YELLOW", "RED" }, colors) - end) - - it("coalesces first non-nil value", function() - assert.equal("Hello World!", functional.coalesce(nil, nil, "Hello World!", "")) - end) - - it("makes a shallow copy of a list", function() - local list = { "BLUE", { nested = "TABLE" }, "RED" } - local list_copy = functional.list_copy(list) - assert.same({ "BLUE", { nested = "TABLE" }, "RED" }, list_copy) - assert.is_not.is_true(list == list_copy) - assert.is_true(list[2] == list_copy[2]) - end) - - it("finds first item that fulfills predicate", function() - local predicate = spy.new(function(item) - return item == "Waldo" - end) - - assert.equal( - "Waldo", - functional.list_find_first(predicate, { - "Where", - "On Earth", - "Is", - "Waldo", - "?", - }) - ) - assert.spy(predicate).was.called(4) - end) - - it("determines whether any item in the list fulfills predicate", function() - local predicate = spy.new(function(item) - return item == "On Earth" - end) - - assert.is_true(functional.list_any(predicate, { - "Where", - "On Earth", - "Is", - "Waldo", - "?", - })) - - assert.spy(predicate).was.called(2) - end) - - it("memoizes functions with default cache mechanism", function() - local expensive_function = spy.new(function(s) - return s - end) - local memoized_fn = functional.memoize(expensive_function) - assert.equal("key", memoized_fn "key") - assert.equal("key", memoized_fn "key") - assert.equal("new_key", memoized_fn "new_key") - assert.spy(expensive_function).was_called(2) - end) - - it("memoizes function with custom cache mechanism", function() - local expensive_function = spy.new(function(arg1, arg2) - return arg1 .. arg2 - end) - local memoized_fn = functional.memoize(expensive_function, function(arg1, arg2) - return arg1 .. arg2 - end) - assert.equal("key1key2", memoized_fn("key1", "key2")) - assert.equal("key1key2", memoized_fn("key1", "key2")) - assert.equal("key1key3", memoized_fn("key1", "key3")) - assert.spy(expensive_function).was_called(2) - end) - - it("should evaluate functions lazily", function() - local impl = spy.new(function() - return {}, {} - end) - local lazy_fn = functional.lazy(impl) - assert.spy(impl).was_called(0) - local a, b = lazy_fn() - assert.spy(impl).was_called(1) - assert.is_true(match.is_table()(a)) - assert.is_true(match.is_table()(b)) - local new_a, new_b = lazy_fn() - assert.spy(impl).was_called(1) - assert.is_true(match.is_ref(a)(new_a)) - assert.is_true(match.is_ref(b)(new_b)) - end) - - it("should support nil return values in lazy functions", function() - local lazy_fn = functional.lazy(function() - return nil, 2 - end) - local a, b = lazy_fn() - assert.is_nil(a) - assert.equal(2, b) - end) - - it("should partially apply functions", function() - local funcy = spy.new() - local partially_funcy = functional.partial(funcy, "a", "b", "c") - partially_funcy("d", "e", "f") - assert.spy(funcy).was_called_with("a", "b", "c", "d", "e", "f") - end) - - it("should partially apply functions with nil arguments", function() - local funcy = spy.new() - local partially_funcy = functional.partial(funcy, "a", nil, "c") - partially_funcy("d", nil, "f") - assert.spy(funcy).was_called_with("a", nil, "c", "d", nil, "f") - end) - - it("should compose functions", function() - local function add(x) - return function(y) - return y + x - end - end - local function subtract(x) - return function(y) - return y - x - end - end - local function multiply(x) - return function(y) - return y * x - end - end - - local big_maths = functional.compose(add(1), subtract(3), multiply(5)) - - assert.equals(23, big_maths(5)) - end) - - it("should not allow composing no functions", function() - local e = assert.error(function() - functional.compose() - end) - assert.equals("compose requires at least one function", e) - end) - - it("should iterate list in .each", function() - local list = { "BLUE", "YELLOW", "RED" } - local iterate_fn = spy.new() - functional.each(iterate_fn, list) - assert.spy(iterate_fn).was_called(3) - assert.spy(iterate_fn).was_called_with("BLUE", 1) - assert.spy(iterate_fn).was_called_with("YELLOW", 2) - assert.spy(iterate_fn).was_called_with("RED", 3) - end) - - it("should negate predicates", function() - local predicate = spy.new(function(item) - return item == "Waldo" - end) - local negated_predicate = functional.negate(predicate) - assert.is_false(negated_predicate "Waldo") - assert.is_true(negated_predicate "Where") - assert.spy(predicate).was_called(2) - end) - - it("should check that all_pass checks that all predicates pass", function() - local t = functional.always(true) - local f = functional.always(false) - local is_waldo = function(i) - return i == "waldo" - end - assert.is_true(functional.all_pass { t, t, is_waldo, t } "waldo") - assert.is_false(functional.all_pass { t, t, is_waldo, f } "waldo") - assert.is_false(functional.all_pass { t, t, is_waldo, t } "waldina") - end) - - it("should index object by prop", function() - local waldo = functional.prop "where is he" - assert.equals("nowhere to be found", waldo { ["where is he"] = "nowhere to be found" }) - end) - - it("should branch if_else", function() - local a = spy.new() - local b = spy.new() - functional.if_else(functional.T, a, b)("a", 1) - functional.if_else(functional.F, a, b)("b", 2) - assert.spy(a).was_called(1) - assert.spy(a).was_called_with("a", 1) - assert.spy(b).was_called(1) - assert.spy(b).was_called_with("b", 2) - end) - - it("should check if string matches", function() - assert.is_false(functional.matches "a" "b") - assert.is_true(functional.matches "a" "a") - end) -end) |
