Diff

spec/util_array_spec.lua @ 11787:3ae6fa901a8b

util.array: Add :slice() method + tests Behaviour follows the same logic as string.sub (so yes, 1-indexed).
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Sep 2021 10:50:20 +0100
parent 10590:257dc26e8e65
child 13245:ffe4adbd2af9
line wrap: on
line diff
--- a/spec/util_array_spec.lua	Sun Sep 12 10:31:02 2021 +0100
+++ b/spec/util_array_spec.lua	Sun Sep 12 10:50:20 2021 +0100
@@ -148,6 +148,25 @@
 			end);
 		end);
 
+		describe("slice", function ()
+			it("works", function ()
+				local a = array({ "a", "b", "c" });
+				assert.equal(array.slice(a, 1, 2), array{ "a", "b" });
+				assert.equal(array.slice(a, 1, 3), array{ "a", "b", "c" });
+				assert.equal(array.slice(a, 2, 3), array{ "b", "c" });
+				assert.equal(array.slice(a, 2), array{ "b", "c" });
+				assert.equal(array.slice(a, -4), array{ "a", "b", "c" });
+				assert.equal(array.slice(a, -3), array{ "a", "b", "c" });
+				assert.equal(array.slice(a, -2), array{ "b", "c" });
+				assert.equal(array.slice(a, -1), array{ "c" });
+			end);
+
+			it("can mutate", function ()
+				local a = array({ "a", "b", "c" });
+				assert.equal(a:slice(-1), array{"c"});
+				assert.equal(a, array{"c"});
+			end);
+		end);
 	end);
 
 	-- TODO The various array.foo(array ina, array outa) functions