From 3c28bdce7f99a39b2f910b46597a3376401f7c53 Mon Sep 17 00:00:00 2001 From: stringTrimmer Date: Sun, 9 Jul 2023 09:17:09 -0500 Subject: [PATCH 1/3] fix #491 where before_each and after_each execute in unpredictable order. Just changed the tables that store these functions to be indexed by the parent `describe`s index in the current_describe table which uses sequential integers. Then in the run_each function where these functions get executed, iterating thru them usning ipairs instead of pairs. --- lua/plenary/busted.lua | 14 ++--- tests/plenary/simple_busted_spec.lua | 94 +++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/lua/plenary/busted.lua b/lua/plenary/busted.lua index 1b15fcec..10721602 100644 --- a/lua/plenary/busted.lua +++ b/lua/plenary/busted.lua @@ -59,13 +59,13 @@ local pop_description = function() end local add_new_each = function() - current_before_each[current_description[#current_description]] = {} - current_after_each[current_description[#current_description]] = {} + current_before_each[#current_description] = {} + current_after_each[#current_description] = {} end local clear_last_each = function() - current_before_each[current_description[#current_description]] = nil - current_after_each[current_description[#current_description]] = nil + current_before_each[#current_description] = nil + current_after_each[#current_description] = nil end local call_inner = function(desc, func) @@ -140,11 +140,11 @@ mod.inner_describe = function(desc, func) end mod.before_each = function(fn) - table.insert(current_before_each[current_description[#current_description]], fn) + table.insert(current_before_each[#current_description], fn) end mod.after_each = function(fn) - table.insert(current_after_each[current_description[#current_description]], fn) + table.insert(current_after_each[#current_description], fn) end mod.clear = function() @@ -161,7 +161,7 @@ local indent = function(msg, spaces) end local run_each = function(tbl) - for _, v in pairs(tbl) do + for _, v in ipairs(tbl) do for _, w in ipairs(v) do if type(w) == "function" then w() diff --git a/tests/plenary/simple_busted_spec.lua b/tests/plenary/simple_busted_spec.lua index f45e7577..aab42181 100644 --- a/tests/plenary/simple_busted_spec.lua +++ b/tests/plenary/simple_busted_spec.lua @@ -67,6 +67,51 @@ describe("before each", function() end) end) +describe("before_each ordering", function() + local order = "" + before_each(function() + order = order .. "1," + end) + before_each(function() + order = order .. "2," + end) + -- it("runs 1st and 2nd before_each in order", function () + -- eq("1,2", order) + -- end) + describe("nested 1 deep", function() + before_each(function() + order = order .. "3," + end) + before_each(function() + order = order .. "4," + end) + describe("nested 2 deep", function() + before_each(function() + order = order .. "5," + end) + it("runs before_each`s in order", function () + eq("1,2,3,4,5,", order) + end) + end) + end) + describe("adjacent nested 1 deep", function() + before_each(function() + order = order .. "3a," + end) + before_each(function() + order = order .. "4a," + end) + describe("nested 2 deep", function() + before_each(function() + order = order .. "5a," + end) + it("runs before_each`s in order", function () + eq("1,2,3,4,5,1,2,3a,4a,5a,", order) + end) + end) + end) +end) + describe("after each", function() local a = 2 local b = 3 @@ -110,7 +155,54 @@ describe("after each", function() end) end) -describe("fourth top level describe test", function() +describe("after_each ordering", function() + local order = "" + describe("1st describe having after_each", function() + after_each(function() + order = order .. "1," + end) + after_each(function() + order = order .. "2," + end) + describe("nested 1 deep", function() + after_each(function() + order = order .. "3," + end) + after_each(function() + order = order .. "4," + end) + describe("nested 2 deep", function() + after_each(function() + order = order .. "5," + end) + it("a test to trigger the after_each`s", function () + assert(true) + end) + end) + end) + describe("adjacent nested 1 deep", function() + after_each(function() + order = order .. "3a," + end) + after_each(function() + order = order .. "4a," + end) + describe("nested 2 deep", function() + after_each(function() + order = order .. "5a," + end) + it("a test to trigger the adjacent after_each`s", function () + assert(true) + end) + end) + end) + end) + it("ran after_each`s in order", function () + eq("1,2,3,4,5,1,2,3a,4a,5a,", order) + end) +end) + +describe("another top level describe test", function() it("should work", function() eq(1, 1) end) From 7b278e4dff9edf53ed320119ab91a2d3208668fb Mon Sep 17 00:00:00 2001 From: stringTrimmer Date: Mon, 10 Jul 2023 08:17:56 -0500 Subject: [PATCH 2/3] Fixed stylua formatting. --- tests/plenary/simple_busted_spec.lua | 60 ++++++++++++++-------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/plenary/simple_busted_spec.lua b/tests/plenary/simple_busted_spec.lua index aab42181..3d32981e 100644 --- a/tests/plenary/simple_busted_spec.lua +++ b/tests/plenary/simple_busted_spec.lua @@ -68,32 +68,32 @@ describe("before each", function() end) describe("before_each ordering", function() - local order = "" - before_each(function() - order = order .. "1," - end) - before_each(function() - order = order .. "2," - end) - -- it("runs 1st and 2nd before_each in order", function () - -- eq("1,2", order) - -- end) - describe("nested 1 deep", function() - before_each(function() - order = order .. "3," - end) - before_each(function() - order = order .. "4," - end) - describe("nested 2 deep", function() - before_each(function() - order = order .. "5," - end) - it("runs before_each`s in order", function () - eq("1,2,3,4,5,", order) - end) - end) - end) + local order = "" + before_each(function() + order = order .. "1," + end) + before_each(function() + order = order .. "2," + end) + -- it("runs 1st and 2nd before_each in order", function () + -- eq("1,2", order) + -- end) + describe("nested 1 deep", function() + before_each(function() + order = order .. "3," + end) + before_each(function() + order = order .. "4," + end) + describe("nested 2 deep", function() + before_each(function() + order = order .. "5," + end) + it("runs before_each`s in order", function() + eq("1,2,3,4,5,", order) + end) + end) + end) describe("adjacent nested 1 deep", function() before_each(function() order = order .. "3a," @@ -105,7 +105,7 @@ describe("before_each ordering", function() before_each(function() order = order .. "5a," end) - it("runs before_each`s in order", function () + it("runs before_each`s in order", function() eq("1,2,3,4,5,1,2,3a,4a,5a,", order) end) end) @@ -175,7 +175,7 @@ describe("after_each ordering", function() after_each(function() order = order .. "5," end) - it("a test to trigger the after_each`s", function () + it("a test to trigger the after_each`s", function() assert(true) end) end) @@ -191,13 +191,13 @@ describe("after_each ordering", function() after_each(function() order = order .. "5a," end) - it("a test to trigger the adjacent after_each`s", function () + it("a test to trigger the adjacent after_each`s", function() assert(true) end) end) end) end) - it("ran after_each`s in order", function () + it("ran after_each`s in order", function() eq("1,2,3,4,5,1,2,3a,4a,5a,", order) end) end) From bd75e6d9112c21e3f6d12cc89ee99c9143147623 Mon Sep 17 00:00:00 2001 From: stringTrimmer Date: Wed, 12 Jul 2023 09:51:41 -0500 Subject: [PATCH 3/3] removed commentted out code --- tests/plenary/simple_busted_spec.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/plenary/simple_busted_spec.lua b/tests/plenary/simple_busted_spec.lua index 3d32981e..ce81ebfd 100644 --- a/tests/plenary/simple_busted_spec.lua +++ b/tests/plenary/simple_busted_spec.lua @@ -75,9 +75,6 @@ describe("before_each ordering", function() before_each(function() order = order .. "2," end) - -- it("runs 1st and 2nd before_each in order", function () - -- eq("1,2", order) - -- end) describe("nested 1 deep", function() before_each(function() order = order .. "3,"