From 25313762aac8fc313b769f20298bd4e6473cc6dd Mon Sep 17 00:00:00 2001 From: William Boman Date: Tue, 20 Dec 2022 08:42:58 +0100 Subject: feat(functional): add trim_start and assoc (#779) --- tests/mason-core/functional/list_spec.lua | 36 +++++++++++++++++++++++++++++ tests/mason-core/functional/string_spec.lua | 8 +++++++ tests/mason-core/functional/table_spec.lua | 32 +++++++++++++++++++++++++ 3 files changed, 76 insertions(+) (limited to 'tests') diff --git a/tests/mason-core/functional/list_spec.lua b/tests/mason-core/functional/list_spec.lua index 7c3d8cfb..6c14e5a9 100644 --- a/tests/mason-core/functional/list_spec.lua +++ b/tests/mason-core/functional/list_spec.lua @@ -265,3 +265,39 @@ describe("functional: list", function() assert.spy(add).was_called_with(10, 5) end) end) + +describe("list immutability", function() + it("should not mutate lists", function() + local og_list = setmetatable({ "a", "b", "c" }, { + __newindex = function() + error "Tried to newindex" + end, + }) + + _.reverse(og_list) + _.list_copy(og_list) + _.filter(_.F, og_list) + _.map(_.to_upper, og_list) + _.filter_map(_.always(Optional.empty()), og_list) + _.each(_.length, og_list) + _.concat(og_list, { "d", "e" }) + _.append("d", og_list) + _.prepend("0", og_list) + _.zip_table({ "first", "second", "third" }, og_list) + _.nth(1, og_list) + _.head(og_list) + _.last(og_list) + _.length(og_list) + _.flatten(og_list) + _.sort_by(_.identity, og_list) + _.uniq_by(_.identity, og_list) + _.join(".", og_list) + _.partition(_.equals "a", og_list) + _.take(2, og_list) + _.drop(2, og_list) + _.drop_last(2, og_list) + _.reduce(_.concat, "", og_list) + + assert.same({ "a", "b", "c" }, og_list) + end) +end) diff --git a/tests/mason-core/functional/string_spec.lua b/tests/mason-core/functional/string_spec.lua index 7bbe06c5..02062f4c 100644 --- a/tests/mason-core/functional/string_spec.lua +++ b/tests/mason-core/functional/string_spec.lua @@ -50,4 +50,12 @@ Ipsum assert.equals("HELLO!", _.to_upper "Hello!") assert.equals("hello!", _.to_lower "Hello!") end) + + it("trim strings", function() + assert.equals("HELLO!", _.trim " HELLO! ") + end) + + it("trim_starts strings", function() + assert.equals("HELLO! ", _.trim_start("%s", " HELLO! ")) + end) end) diff --git a/tests/mason-core/functional/table_spec.lua b/tests/mason-core/functional/table_spec.lua index 357f7120..8ffedeb1 100644 --- a/tests/mason-core/functional/table_spec.lua +++ b/tests/mason-core/functional/table_spec.lua @@ -111,4 +111,36 @@ describe("functional: table", function() c = "c", }, _.dissoc("b", { a = "a", b = "b", c = "c" })) end) + + it("should assoc keys", function() + assert.same({ + a = "a", + b = "b", + c = "c", + }, _.assoc("b", "b", { a = "a", c = "c" })) + end) +end) + +describe("table immutability", function() + it("should not mutate tables", function() + local og_table = setmetatable({ key = "value", imagination = "poor", hotel = "trivago" }, { + __newindex = function() + error "Tried to newindex" + end, + }) + + _.prop("hotel", og_table) + _.path({ "hotel" }, og_table) + _.pick({ "hotel" }, og_table) + _.keys(og_table) + _.size(og_table) + _.from_pairs(_.to_pairs(og_table)) + _.invert(og_table) + _.evolve({ hotel = _.to_upper }, og_table) + _.merge_left(og_table, {}) + _.assoc("new", "value", og_table) + _.dissoc("hotel", og_table) + + assert.same({ key = "value", imagination = "poor", hotel = "trivago" }, og_table) + end) end) -- cgit v1.2.3-70-g09d2