Skip to content

Commit

Permalink
Timer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Dec 10, 2024
1 parent efa146c commit 84a11b8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 69 deletions.
26 changes: 26 additions & 0 deletions ci/Test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type TestExpect = {
ToBeLessThan: (self: TestExpect, value: number) -> (),
ToBeLessThanOrEqual: (self: TestExpect, value: number) -> (),
ToContain: (self: TestExpect, value: any) -> (),
ToBeNear: (self: TestExpect, value: number, epsilon: number) -> (),
ToBeA: (self: TestExpect, typeOf: string) -> (),
}

local TestExpect = {}
Expand Down Expand Up @@ -206,6 +208,30 @@ function TestExpect:ToContain(value: any)
end
end

function TestExpect:ToBeA(t: string)
local condition = typeof(self.Value) == t

if self.Flip then
condition = not condition
end

if not condition then
error(`"{typeof(self.Value)}" {if self.Flip then "!=" else "=="} "{t}"`, 0)
end
end

function TestExpect:ToBeNear(value: number, epsilon: number)
local condition = math.abs(self.Value - value) < epsilon

if self.Flip then
condition = not condition
end

if not condition then
error(`{self.Value} {if self.Flip then "is not near" else "is near"} {value}`, 0)
end
end

function TestExpect:Not()
self.Flip = not self.Flip
return self
Expand Down
69 changes: 0 additions & 69 deletions modules/timer/init.spec.luau

This file was deleted.

73 changes: 73 additions & 0 deletions modules/timer/init.test.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
local ServerScriptService = game:GetService("ServerScriptService")

local Test = require(ServerScriptService.TestRunner.Test)

return function(ctx: Test.TestContext)
local Timer = require(script.Parent)

ctx:Describe("Timer", function()
local timer

ctx:BeforeEach(function()
timer = Timer.new(0.1)
timer.TimeFunction = os.clock
end)

ctx:AfterEach(function()
if timer then
timer:Destroy()
timer = nil
end
end)

ctx:Test("should create a new timer", function()
ctx:Expect(Timer.Is(timer)):ToBe(true)
end)

ctx:Test("should tick appropriately", function()
local start = os.clock()
timer:Start()
timer.Tick:Wait()
local duration = (os.clock() - start)
ctx:Expect(duration):ToBeNear(duration, 0.02)
end)

ctx:Test("should start immediately", function()
local start = os.clock()
local stop = nil
timer.Tick:Connect(function()
if not stop then
stop = os.clock()
end
end)
timer:StartNow()
timer.Tick:Wait()
ctx:Expect(stop):ToBeA("number")
local duration = (stop - start)
ctx:Expect(duration):ToBeNear(0, 0.02)
end)

ctx:Test("should stop", function()
local ticks = 0
timer.Tick:Connect(function()
ticks += 1
end)
timer:StartNow()
timer:Stop()
task.wait(1)
ctx:Expect(ticks):ToBe(1)
end)

ctx:Test("should detect if running", function()
ctx:Expect(timer:IsRunning()):ToBe(false)
timer:Start()
ctx:Expect(timer:IsRunning()):ToBe(true)
timer:Stop()
ctx:Expect(timer:IsRunning()):ToBe(false)
timer:StartNow()
ctx:Expect(timer:IsRunning()):ToBe(true)
timer:Stop()
ctx:Expect(timer:IsRunning()):ToBe(false)
end)
end)
end
1 change: 1 addition & 0 deletions modules/trove/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Trove = {
Add: <T>(self: Trove, object: T & Trackable, cleanupMethod: string?) -> T,
Remove: <T>(self: Trove, object: T & Trackable) -> boolean,
Clean: (self: Trove) -> (),
WrapClean: (self: Trove) -> () -> (),
AttachToInstance: (self: Trove, instance: Instance) -> RBXScriptConnection,
Destroy: (self: Trove) -> (),
}
Expand Down

0 comments on commit 84a11b8

Please sign in to comment.