Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Merge f1112b9 into 3de4137
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoteTheMighty committed Sep 24, 2019
2 parents 3de4137 + f1112b9 commit e888960
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 172 deletions.
196 changes: 98 additions & 98 deletions src/assertDeepEqual.spec.lua
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
return function()
local assertDeepEqual = require(script.Parent.assertDeepEqual)

it("should fail with a message when args are not equal", function()
local success, message = pcall(assertDeepEqual, 1, 2)

expect(success).to.equal(false)
expect(message:find("first ~= second")).to.be.ok()

success, message = pcall(assertDeepEqual, {
foo = 1,
}, {
foo = 2,
})

expect(success).to.equal(false)
expect(message:find("first%[foo%] ~= second%[foo%]")).to.be.ok()
end)

it("should compare non-table values using standard '==' equality", function()
assertDeepEqual(1, 1)
assertDeepEqual("hello", "hello")
assertDeepEqual(nil, nil)

local someFunction = function() end
local theSameFunction = someFunction

assertDeepEqual(someFunction, theSameFunction)

local A = {
foo = someFunction
}
local B = {
foo = theSameFunction
}

assertDeepEqual(A, B)
end)

it("should fail when types differ", function()
local success, message = pcall(assertDeepEqual, 1, "1")

expect(success).to.equal(false)
expect(message:find("first is of type number, but second is of type string")).to.be.ok()
end)

it("should compare (and report about) nested tables", function()
local A = {
foo = "bar",
nested = {
foo = 1,
bar = 2,
}
}
local B = {
foo = "bar",
nested = {
foo = 1,
bar = 2,
}
}

assertDeepEqual(A, B)

local C = {
foo = "bar",
nested = {
foo = 1,
bar = 3,
}
}

local success, message = pcall(assertDeepEqual, A, C)

expect(success).to.equal(false)
expect(message:find("first%[nested%]%[bar%] ~= second%[nested%]%[bar%]")).to.be.ok()
end)

it("should be commutative", function()
local equalArgsA = {
foo = "bar",
hello = "world",
}
local equalArgsB = {
foo = "bar",
hello = "world",
}

assertDeepEqual(equalArgsA, equalArgsB)
assertDeepEqual(equalArgsB, equalArgsA)

local nonEqualArgs = {
foo = "bar",
}

expect(function() assertDeepEqual(equalArgsA, nonEqualArgs) end).to.throw()
expect(function() assertDeepEqual(nonEqualArgs, equalArgsA) end).to.throw()
end)
return function()
local assertDeepEqual = require(script.Parent.assertDeepEqual)

it("should fail with a message when args are not equal", function()
local success, message = pcall(assertDeepEqual, 1, 2)

expect(success).to.equal(false)
expect(message:find("first ~= second")).to.be.ok()

success, message = pcall(assertDeepEqual, {
foo = 1,
}, {
foo = 2,
})

expect(success).to.equal(false)
expect(message:find("first%[foo%] ~= second%[foo%]")).to.be.ok()
end)

it("should compare non-table values using standard '==' equality", function()
assertDeepEqual(1, 1)
assertDeepEqual("hello", "hello")
assertDeepEqual(nil, nil)

local someFunction = function() end
local theSameFunction = someFunction

assertDeepEqual(someFunction, theSameFunction)

local A = {
foo = someFunction
}
local B = {
foo = theSameFunction
}

assertDeepEqual(A, B)
end)

it("should fail when types differ", function()
local success, message = pcall(assertDeepEqual, 1, "1")

expect(success).to.equal(false)
expect(message:find("first is of type number, but second is of type string")).to.be.ok()
end)

it("should compare (and report about) nested tables", function()
local A = {
foo = "bar",
nested = {
foo = 1,
bar = 2,
}
}
local B = {
foo = "bar",
nested = {
foo = 1,
bar = 2,
}
}

assertDeepEqual(A, B)

local C = {
foo = "bar",
nested = {
foo = 1,
bar = 3,
}
}

local success, message = pcall(assertDeepEqual, A, C)

expect(success).to.equal(false)
expect(message:find("first%[nested%]%[bar%] ~= second%[nested%]%[bar%]")).to.be.ok()
end)

it("should be commutative", function()
local equalArgsA = {
foo = "bar",
hello = "world",
}
local equalArgsB = {
foo = "bar",
hello = "world",
}

assertDeepEqual(equalArgsA, equalArgsB)
assertDeepEqual(equalArgsB, equalArgsA)

local nonEqualArgs = {
foo = "bar",
}

expect(function() assertDeepEqual(equalArgsA, nonEqualArgs) end).to.throw()
expect(function() assertDeepEqual(nonEqualArgs, equalArgsA) end).to.throw()
end)
end
148 changes: 74 additions & 74 deletions src/createSignal.lua
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
--[[
This is a simple signal implementation that has a dead-simple API.
local signal = createSignal()
local disconnect = signal:subscribe(function(foo)
print("Cool foo:", foo)
end)
signal:fire("something")
disconnect()
]]

local function addToMap(map, addKey, addValue)
local new = {}

for key, value in pairs(map) do
new[key] = value
end

new[addKey] = addValue

return new
end

local function removeFromMap(map, removeKey)
local new = {}

for key, value in pairs(map) do
if key ~= removeKey then
new[key] = value
end
end

return new
end

local function createSignal()
local connections = {}

local function subscribe(self, callback)
assert(typeof(callback) == "function", "Can only subscribe to signals with a function.")

local connection = {
callback = callback,
}

connections = addToMap(connections, callback, connection)

local function disconnect()
assert(not connection.disconnected, "Listeners can only be disconnected once.")

connection.disconnected = true
connections = removeFromMap(connections, callback)
end

return disconnect
end

local function fire(self, ...)
for callback, connection in pairs(connections) do
if not connection.disconnected then
callback(...)
end
end
end

return {
subscribe = subscribe,
fire = fire,
}
end

--[[
This is a simple signal implementation that has a dead-simple API.
local signal = createSignal()
local disconnect = signal:subscribe(function(foo)
print("Cool foo:", foo)
end)
signal:fire("something")
disconnect()
]]

local function addToMap(map, addKey, addValue)
local new = {}

for key, value in pairs(map) do
new[key] = value
end

new[addKey] = addValue

return new
end

local function removeFromMap(map, removeKey)
local new = {}

for key, value in pairs(map) do
if key ~= removeKey then
new[key] = value
end
end

return new
end

local function createSignal()
local connections = {}

local function subscribe(self, callback)
assert(typeof(callback) == "function", "Can only subscribe to signals with a function.")

local connection = {
callback = callback,
}

connections = addToMap(connections, callback, connection)

local function disconnect()
assert(not connection.disconnected, "Listeners can only be disconnected once.")

connection.disconnected = true
connections = removeFromMap(connections, callback)
end

return disconnect
end

local function fire(self, ...)
for callback, connection in pairs(connections) do
if not connection.disconnected then
callback(...)
end
end
end

return {
subscribe = subscribe,
fire = fire,
}
end

return createSignal

0 comments on commit e888960

Please sign in to comment.