summaryrefslogtreecommitdiffstats
path: root/tests/mason-core/functional
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-07-08 18:34:38 +0200
committerGitHub <noreply@github.com>2022-07-08 18:34:38 +0200
commit976aa4fbee8a070f362cab6f6ec84e9251a90cf9 (patch)
tree5e8d9c9c59444a25c7801b8f39763c4ba6e1f76d /tests/mason-core/functional
parentfeat: add gotests, gomodifytags, impl (#28) (diff)
downloadmason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.gz
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.bz2
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.lz
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.xz
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.zst
mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.zip
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
Diffstat (limited to 'tests/mason-core/functional')
-rw-r--r--tests/mason-core/functional/data_spec.lua28
-rw-r--r--tests/mason-core/functional/function_spec.lua142
-rw-r--r--tests/mason-core/functional/list_spec.lua193
-rw-r--r--tests/mason-core/functional/logic_spec.lua57
-rw-r--r--tests/mason-core/functional/number_spec.lua50
-rw-r--r--tests/mason-core/functional/relation_spec.lua36
-rw-r--r--tests/mason-core/functional/string_spec.lua43
-rw-r--r--tests/mason-core/functional/table_spec.lua51
-rw-r--r--tests/mason-core/functional/type_spec.lua26
9 files changed, 626 insertions, 0 deletions
diff --git a/tests/mason-core/functional/data_spec.lua b/tests/mason-core/functional/data_spec.lua
new file mode 100644
index 00000000..e2f8f7ee
--- /dev/null
+++ b/tests/mason-core/functional/data_spec.lua
@@ -0,0 +1,28 @@
+local _ = require "mason-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/mason-core/functional/function_spec.lua b/tests/mason-core/functional/function_spec.lua
new file mode 100644
index 00000000..8c4b41ef
--- /dev/null
+++ b/tests/mason-core/functional/function_spec.lua
@@ -0,0 +1,142 @@
+local spy = require "luassert.spy"
+local match = require "luassert.match"
+local _ = require "mason-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/mason-core/functional/list_spec.lua b/tests/mason-core/functional/list_spec.lua
new file mode 100644
index 00000000..999b3625
--- /dev/null
+++ b/tests/mason-core/functional/list_spec.lua
@@ -0,0 +1,193 @@
+local spy = require "luassert.spy"
+local _ = require "mason-core.functional"
+local Optional = require "mason-core.optional"
+
+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("filter_map over list", function()
+ local colors = { "BROWN", "BLUE", "YELLOW", "GREEN", "CYAN" }
+ assert.same(
+ {
+ "BROWN EYES",
+ "BLUE EYES",
+ "GREEN EYES",
+ },
+ _.filter_map(function(color)
+ if _.any_pass({ _.equals "BROWN", _.equals "BLUE", _.equals "GREEN" }, color) then
+ return Optional.of(("%s EYES"):format(color))
+ else
+ return Optional.empty()
+ end
+ end, 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)
+
+ it("should concat list tables", function()
+ local list = { "monstera", "tulipa", "carnation" }
+ assert.same({ "monstera", "tulipa", "carnation", "rose", "daisy" }, _.concat(list, { "rose", "daisy" }))
+ assert.same({ "monstera", "tulipa", "carnation" }, list) -- does not mutate list
+ end)
+
+ it("should concat strings", function()
+ assert.equals("FooBar", _.concat("Foo", "Bar"))
+ end)
+
+ it("should zip list into table", function()
+ local fnkey = function() end
+ assert.same({
+ a = "a",
+ [fnkey] = 1,
+ }, _.zip_table({ "a", fnkey }, { "a", 1 }))
+ end)
+
+ it("should get nth item", function()
+ assert.equals("first", _.nth(1, { "first", "middle", "last" }))
+ assert.equals("last", _.nth(-1, { "first", "middle", "last" }))
+ assert.equals("middle", _.nth(-2, { "first", "middle", "last" }))
+ assert.equals("a", _.nth(1, "abc"))
+ assert.equals("c", _.nth(-1, "abc"))
+ assert.equals("b", _.nth(-2, "abc"))
+ assert.is_nil(_.nth(0, { "value" }))
+ assert.equals("", _.nth(0, "abc"))
+ end)
+
+ it("should get length", function()
+ assert.equals(0, _.length {})
+ assert.equals(0, _.length { nil })
+ assert.equals(0, _.length { obj = "doesnt count" })
+ assert.equals(0, _.length "")
+ assert.equals(1, _.length { "" })
+ assert.equals(4, _.length "fire")
+ end)
+
+ it("should sort by comparator", function()
+ local list = {
+ {
+ name = "William",
+ },
+ {
+ name = "Boman",
+ },
+ }
+ assert.same({
+ {
+ name = "Boman",
+ },
+ {
+ name = "William",
+ },
+ }, _.sort_by(_.prop "name", list))
+
+ -- Should not mutate original list
+ assert.same({
+ {
+ name = "William",
+ },
+ {
+ name = "Boman",
+ },
+ }, list)
+ end)
+
+ it("should append to list", function()
+ local list = { "Earth", "Wind" }
+ assert.same({ "Earth", "Wind", { "Fire" } }, _.append({ "Fire" }, list))
+
+ -- Does not mutate original list
+ assert.same({ "Earth", "Wind" }, list)
+ end)
+
+ it("should prepend to list", function()
+ local list = { "Fire" }
+ assert.same({ { "Earth", "Wind" }, "Fire" }, _.prepend({ "Earth", "Wind" }, list))
+
+ -- Does not mutate original list
+ assert.same({ "Fire" }, list)
+ end)
+
+ it("joins lists", function()
+ assert.equals("Hello, John", _.join(", ", { "Hello", "John" }))
+ end)
+end)
diff --git a/tests/mason-core/functional/logic_spec.lua b/tests/mason-core/functional/logic_spec.lua
new file mode 100644
index 00000000..7c795443
--- /dev/null
+++ b/tests/mason-core/functional/logic_spec.lua
@@ -0,0 +1,57 @@
+local spy = require "luassert.spy"
+local _ = require "mason-core.functional"
+
+describe("functional: logic", function()
+ it("should check that all_pass checks that all predicates pass", function()
+ local is_waldo = _.equals "waldo"
+ 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 check that any_pass checks that any predicates pass", function()
+ local is_waldo = _.equals "waldo"
+ local is_waldina = _.equals "waldina"
+ local is_luigi = _.equals "luigi"
+
+ assert.is_true(_.any_pass { is_waldo, is_waldina } "waldo")
+ assert.is_false(_.any_pass { is_waldina, is_luigi } "waldo")
+ assert.is_true(_.any_pass { is_waldina, is_luigi } "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)
+
+ it("should give complements", function()
+ assert.is_true(_.complement(_.is_nil, "not nil"))
+ assert.is_false(_.complement(_.is_nil, nil))
+ end)
+end)
diff --git a/tests/mason-core/functional/number_spec.lua b/tests/mason-core/functional/number_spec.lua
new file mode 100644
index 00000000..db523c2d
--- /dev/null
+++ b/tests/mason-core/functional/number_spec.lua
@@ -0,0 +1,50 @@
+local _ = require "mason-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/mason-core/functional/relation_spec.lua b/tests/mason-core/functional/relation_spec.lua
new file mode 100644
index 00000000..a3eee722
--- /dev/null
+++ b/tests/mason-core/functional/relation_spec.lua
@@ -0,0 +1,36 @@
+local _ = require "mason-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/mason-core/functional/string_spec.lua b/tests/mason-core/functional/string_spec.lua
new file mode 100644
index 00000000..25409f64
--- /dev/null
+++ b/tests/mason-core/functional/string_spec.lua
@@ -0,0 +1,43 @@
+local _ = require "mason-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)
+
+ it("should split strings", function()
+ assert.same({ "This", "is", "a", "sentence" }, _.split("%s", "This is a sentence"))
+ assert.same({ "This", "is", "a", "sentence" }, _.split("|", "This|is|a|sentence"))
+ end)
+
+ it("should gsub strings", function()
+ assert.same("predator", _.gsub("^apex%s*", "", "apex predator"))
+ end)
+
+ it("should dedent strings", function()
+ assert.equals(
+ [[Lorem
+Ipsum
+ Dolor
+ Sit
+ Amet]],
+ _.dedent [[
+ Lorem
+ Ipsum
+ Dolor
+ Sit
+ Amet
+]]
+ )
+ end)
+end)
diff --git a/tests/mason-core/functional/table_spec.lua b/tests/mason-core/functional/table_spec.lua
new file mode 100644
index 00000000..012c981c
--- /dev/null
+++ b/tests/mason-core/functional/table_spec.lua
@@ -0,0 +1,51 @@
+local _ = require "mason-core.functional"
+
+describe("functional: table", function()
+ it("retrieves property of table", function()
+ assert.equals("hello", _.prop("a", { a = "hello" }))
+ end)
+
+ it("picks properties of table", function()
+ local function fn() end
+ assert.same(
+ {
+ ["key1"] = 1,
+ [fn] = 2,
+ },
+ _.pick({ "key1", fn }, {
+ ["key1"] = 1,
+ [fn] = 2,
+ [3] = 3,
+ })
+ )
+ end)
+
+ it("converts table to pairs", function()
+ assert.same(
+ _.sort_by(_.nth(1), {
+ {
+ "skies",
+ "cloudy",
+ },
+ {
+ "temperature",
+ "20°",
+ },
+ }),
+ _.sort_by(_.nth(1), _.to_pairs { skies = "cloudy", temperature = "20°" })
+ )
+ end)
+
+ it("should invert tables", function()
+ assert.same(
+ {
+ val1 = "key1",
+ val2 = "key2",
+ },
+ _.invert {
+ key1 = "val1",
+ key2 = "val2",
+ }
+ )
+ end)
+end)
diff --git a/tests/mason-core/functional/type_spec.lua b/tests/mason-core/functional/type_spec.lua
new file mode 100644
index 00000000..e75a5647
--- /dev/null
+++ b/tests/mason-core/functional/type_spec.lua
@@ -0,0 +1,26 @@
+local _ = require "mason-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)