Skip to content

Commit 70614e0

Browse files
committed
Add tests
1 parent 5f2ea57 commit 70614e0

File tree

6 files changed

+250
-1
lines changed

6 files changed

+250
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
packages/*/Packages
33
/build
44
/Packages
5+
/test/Packages

default.project.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "rbx-util",
3+
"tree": {
4+
"$className": "DataModel",
5+
"ReplicatedStorage": {
6+
"$className": "ReplicatedStorage",
7+
"Modules": {
8+
"$path": "modules"
9+
}
10+
},
11+
"TestService": {
12+
"$className": "TestService",
13+
"$properties": {
14+
"ExecuteWithStudioRun": true
15+
},
16+
"$path": "test"
17+
}
18+
}
19+
}

modules/signal/init.spec.lua

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
local function AwaitCondition(predicate, timeout)
2+
local start = os.clock()
3+
timeout = (timeout or 10)
4+
while true do
5+
if predicate() then return true end
6+
if (os.clock() - start) > timeout then return false end
7+
task.wait()
8+
end
9+
end
10+
11+
return function()
12+
13+
local Signal = require(script.Parent)
14+
15+
local signal
16+
17+
local function NumConns(sig)
18+
sig = sig or signal
19+
return #sig:GetConnections()
20+
end
21+
22+
beforeEach(function()
23+
signal = Signal.new()
24+
end)
25+
26+
afterEach(function()
27+
signal:Destroy()
28+
end)
29+
30+
describe("Constructor", function()
31+
32+
it("should create a new signal and fire it", function()
33+
expect(Signal.Is(signal)).to.equal(true)
34+
task.defer(function()
35+
signal:Fire(10, 20)
36+
end)
37+
local n1, n2 = signal:Wait()
38+
expect(n1).to.equal(10)
39+
expect(n2).to.equal(20)
40+
end)
41+
42+
it("should create a proxy signal and connect to it", function()
43+
local signalWrap = Signal.Wrap(game:GetService("RunService").Heartbeat)
44+
expect(Signal.Is(signalWrap)).to.equal(true)
45+
local fired = false
46+
signalWrap:Connect(function()
47+
fired = true
48+
end)
49+
expect(AwaitCondition(function() return fired end, 2)).to.equal(true)
50+
signalWrap:Destroy()
51+
end)
52+
53+
end)
54+
55+
describe("FireDeferred", function()
56+
57+
it("should be able to fire primitive argument", function()
58+
local send = 10
59+
local value
60+
signal:Connect(function(v)
61+
value = v
62+
end)
63+
signal:FireDeferred(send)
64+
expect(AwaitCondition(function() return (send == value) end, 1)).to.equal(true)
65+
end)
66+
67+
it("should be able to fire a reference based argument", function()
68+
local send = {10, 20}
69+
local value
70+
signal:Connect(function(v)
71+
value = v
72+
end)
73+
signal:FireDeferred(send)
74+
expect(AwaitCondition(function() return (send == value) end, 1)).to.equal(true)
75+
end)
76+
77+
end)
78+
79+
describe("Fire", function()
80+
81+
it("should be able to fire primitive argument", function()
82+
local send = 10
83+
local value
84+
signal:Connect(function(v)
85+
value = v
86+
end)
87+
signal:Fire(send)
88+
expect(value).to.equal(send)
89+
end)
90+
91+
it("should be able to fire a reference based argument", function()
92+
local send = {10, 20}
93+
local value
94+
signal:Connect(function(v)
95+
value = v
96+
end)
97+
signal:Fire(send)
98+
expect(value).to.equal(send)
99+
end)
100+
101+
end)
102+
103+
describe("Wait", function()
104+
105+
it("should be able to wait for a signal to fire", function()
106+
task.defer(function()
107+
signal:Fire(10, 20, 30)
108+
end)
109+
local n1, n2, n3 = signal:Wait()
110+
expect(n1).to.equal(10)
111+
expect(n2).to.equal(20)
112+
expect(n3).to.equal(30)
113+
end)
114+
115+
end)
116+
117+
describe("DisconnectAll", function()
118+
119+
it("should disconnect all connections", function()
120+
signal:Connect(function() end)
121+
signal:Connect(function() end)
122+
expect(NumConns()).to.equal(2)
123+
signal:DisconnectAll()
124+
expect(NumConns()).to.equal(0)
125+
end)
126+
127+
end)
128+
129+
describe("Disconnect", function()
130+
131+
it("should disconnect connection", function()
132+
local con = signal:Connect(function() end)
133+
expect(NumConns()).to.equal(1)
134+
con:Disconnect()
135+
expect(NumConns()).to.equal(0)
136+
end)
137+
138+
it("should still work if connections disconnected while firing", function()
139+
local a = 0
140+
local c
141+
signal:Connect(function() a += 1 end)
142+
c = signal:Connect(function() c:Disconnect() a += 1 end)
143+
signal:Connect(function() a += 1 end)
144+
signal:Fire()
145+
expect(a).to.equal(3)
146+
end)
147+
148+
it("should still work if connections disconnected while firing deferred", function()
149+
local a = 0
150+
local c
151+
signal:Connect(function() a += 1 end)
152+
c = signal:Connect(function() c:Disconnect() a += 1 end)
153+
signal:Connect(function() a += 1 end)
154+
signal:FireDeferred()
155+
expect(AwaitCondition(function() return a == 3 end)).to.equal(true)
156+
end)
157+
158+
end)
159+
160+
end

selene.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
std = "roblox"
1+
std = "roblox+testez"
22

33
[config]
44
unused_variable = {allow_unused_self = true}
5+
multiple_statements = "allow"

test/RunTests.server.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print("Running unit tests...")
2+
require(script.Parent.Packages.TestEZ).TestBootstrap:run({game:GetService("ReplicatedStorage").Modules})

testez.toml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[[afterAll.args]]
2+
type = "function"
3+
4+
[[afterEach.args]]
5+
type = "function"
6+
7+
[[beforeAll.args]]
8+
type = "function"
9+
10+
[[beforeEach.args]]
11+
type = "function"
12+
13+
[[describe.args]]
14+
type = "string"
15+
16+
[[describe.args]]
17+
type = "function"
18+
19+
[[describeFOCUS.args]]
20+
type = "string"
21+
22+
[[describeFOCUS.args]]
23+
type = "function"
24+
25+
[[describeSKIP.args]]
26+
type = "string"
27+
28+
[[describeSKIP.args]]
29+
type = "function"
30+
31+
[[expect.args]]
32+
type = "any"
33+
34+
[[FIXME.args]]
35+
type = "string"
36+
required = false
37+
38+
[FOCUS]
39+
args = []
40+
41+
[[it.args]]
42+
type = "string"
43+
44+
[[it.args]]
45+
type = "function"
46+
47+
[[itFIXME.args]]
48+
type = "string"
49+
50+
[[itFIXME.args]]
51+
type = "function"
52+
53+
[[itFOCUS.args]]
54+
type = "string"
55+
56+
[[itFOCUS.args]]
57+
type = "function"
58+
59+
[[itSKIP.args]]
60+
type = "string"
61+
62+
[[itSKIP.args]]
63+
type = "function"
64+
65+
[SKIP]
66+
args = []

0 commit comments

Comments
 (0)