Testing
Transitions move with real frames, and frames are never the same length twice. A test that waits with task.wait sees slightly different values on every run. The manual clock fixes that: transitions only move when the test says so, by an exact amount of time.
const Vide = require("@packages/Vide")
const VideState = require("@VideState")
const schemas = VideState.schemas
const rules = VideState.rules
-- A handler with one field, `opacity`, that is 0 in `Idle` and 1 in `Active`.
const handler = rules.dynamic(schemas.new({
opacity = schemas.float(),
}), { name = "Fade" }).new({
Idle = { opacity = Vide.source(0) },
Active = { opacity = Vide.source(1) },
}, "Idle")
local lastOpacity = 0
VideState.setManualClock(true)
handler.onTransition(function(_, _, current)
lastOpacity = current.opacity
end, {
tweenDefault = {
duration = 1,
},
})
handler.setState("Active") -- the first frame: opacity is 0
VideState.advanceClock(0.25) -- opacity is 0.25
VideState.advanceClock(0.75) -- opacity is 1, and onFinish runs
VideState.setManualClock(false)- While the manual clock is on, real frames do not move any transition. Each
advanceClockcall steps every running transition of every handler once, as one frame of that length. - The time is scaled like a real frame, so
pause, aspeedsource, andscaleSpeedstill apply, and so do delays. setState,finish, and an instant change work as usual.setStatestill sends its first frame at once.- Turning the clock off hands the running transitions back to real frames, from where they are.
- The clock is shared by every handler. Turn it off at the end of each test, so the next test runs with real frames.
- To run everything to its end without counting the time, call
advanceClockUntilIdle. It steps frames until no transition, delay, or sequence hold is left.
Functions
setManualClock Added in 0.5.0
VideState.setManualClock(
enabled: boolean -- Whether transitions wait for advanceClock
) -> ()Turns the manual clock on or off. It is off by default.
Parameters
| Name | Type | Description |
|---|---|---|
enabled | boolean | true stops real frames from moving transitions. false hands them back to real frames. |
Errors
| Cause | Message | |
|---|---|---|
| Error | enabled is not a boolean | setManualClock expects a boolean, got <type> |
isManualClock Added in 0.5.0
VideState.isManualClock() -> booleanReturns whether the manual clock is on.
Returns
| Type | Description |
|---|---|
boolean | true while transitions wait for advanceClock. Defaults to false. |
advanceClock Added in 0.5.0 Changed in 0.8.0
VideState.advanceClock(
seconds: number -- How much time passes
) -> ()Do not call it from inside a transition callback. It raises an error there, because stepping again in the middle of a frame would corrupt the frame that is running.
Steps every running transition by seconds, as one frame, before it returns. Listeners, animate and progress sources, and onFinish run during the call.
Parameters
| Name | Type | Description |
|---|---|---|
seconds | number | The length of the frame. Must be finite and non-negative. |
Errors
| Cause | Message | |
|---|---|---|
| Error | The manual clock is off | advanceClock needs the manual clock. Call setManualClock(true) first |
| Error | seconds is not a finite non-negative number | advanceClock expects a finite non-negative number of seconds |
| Error | It is called from inside a transition callback, while the frames are being stepped | advanceClock cannot be called from inside a transition callback |
advanceClockUntilIdle Added in 0.9.0
VideState.advanceClockUntilIdle(
frameSeconds: number?, -- The length of each frame. Defaults to 1/60
maxSeconds: number? -- How long to step before giving up. Defaults to 60
) -> numberSteps every running transition in frames of frameSeconds until every handler is idle: no transition is running, no connection is waiting out a delay, and no playSequence is holding a step. It returns the time it advanced, which is 0 when everything was already idle.
const Vide = require("@packages/Vide")
const VideState = require("@VideState")
const schemas = VideState.schemas
const rules = VideState.rules
const handler = rules.dynamic(schemas.new({
opacity = schemas.float(),
}), { name = "Menu" }).new({
Closed = { opacity = Vide.source(0) },
Open = { opacity = Vide.source(1) },
}, "Closed")
const animated = handler.animate({
tweenDefault = {
duration = 0.3,
delay = 0.1,
},
})
VideState.setManualClock(true)
handler.setState("Open")
VideState.advanceClockUntilIdle() -- returns about 0.4
assert(animated.opacity() == 1)
VideState.setManualClock(false)A step of a sequence can start up to one frame late, because something that starts during a frame is first stepped on the next one, so the time it returns is a whole number of frames. A paused handler and a sequence with loop = true never become idle, so the call raises an error once maxSeconds have passed.
Parameters
| Name | Type | Description |
|---|---|---|
frameSeconds | number? | The length of each frame. Must be finite and positive. Defaults to 1/60. |
maxSeconds | number? | The longest it steps before it raises an error. Must be finite and positive. Defaults to 60. |
Returns
| Type | Description |
|---|---|
number | The time it advanced, in seconds. |
Errors
| Cause | Message | |
|---|---|---|
| Error | The manual clock is off | advanceClockUntilIdle needs the manual clock. Call setManualClock(true) first |
| Error | frameSeconds or maxSeconds is not a finite positive number | advanceClockUntilIdle expects frameSeconds to be a finite positive number, and the same for maxSeconds |
| Error | Something is still running after maxSeconds, such as a paused handler or a looping sequence | advanceClockUntilIdle advanced <maxSeconds> seconds and a transition is still running. A paused handler or a looping sequence never becomes idle |
| Error | It is called from inside a transition callback | advanceClockUntilIdle cannot be called from inside a transition callback |