This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
172 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |