From 71c1118039d5997498e58a66d6daa7e266ecfbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Tue, 1 Nov 2011 22:36:43 +0100 Subject: [PATCH] added test and fix for undue hard reference storage --- beholder.lua | 3 ++- spec/unit.lua | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/beholder.lua b/beholder.lua index dc655b3..7693f98 100644 --- a/beholder.lua +++ b/beholder.lua @@ -18,7 +18,8 @@ local Node = { } function Node:new() - return setmetatable( { callbacks = {}, children = {} }, { __index = Node } ) + local node = { callbacks = {}, children = setmetatable({}, {__mode="k"}) } + return setmetatable( node, { __index = Node } ) end function Node:findById(id) diff --git a/spec/unit.lua b/spec/unit.lua index 31a4e26..9178df9 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -45,6 +45,17 @@ describe("Unit", function() it("throws an error if called without at least one parameter", function() assert_error(function() beholder:observe() end) end) + + it("does not store hard references to variables", function() + local counter = 0 + local x = {} + beholder:observe(x, function() counter = counter + 1 end) + beholder:triggerAll() + x = nil + collectgarbage("collect") + beholder:triggerAll() + assert_equal(1, counter) + end) end) describe(":stopObserving", function()