0ad/binaries/data/mods/public/simulation/components/tests/test_Timer.js
Dunedan 93ce94655d
Use @stylistic/brace-style for eslint
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace
style for JavaScript code. This plugin was however updated the last time
over 9 years ago and will be incompatible with ESLint v10, as that
[removes `context.getSourceCode()`][1], the plugin relies on.

To keep the eslint config working with ESLint v10, this replaces
`eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule
from `@stylistic/eslint-plugin`, a package we already use.

While `@stylistic/brace-style` doesn't offer an option to format braces
in exactly the same way as before, the "allman" style seems to be the
one closest to the existing code.

[1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members
[2]: https://eslint.style/rules/brace-style
2026-01-12 21:33:52 +01:00

99 lines
2.8 KiB
JavaScript

Engine.LoadComponentScript("interfaces/Timer.js");
Engine.LoadComponentScript("Timer.js");
Engine.RegisterInterface("Test");
var cmpTimer = ConstructComponent(SYSTEM_ENTITY, "Timer");
var fired = [];
AddMock(10, IID_Test, {
"Callback": function(data, lateness)
{
fired.push([data, lateness]);
}
});
var cancelId;
AddMock(20, IID_Test, {
"Callback": function(data, lateness)
{
fired.push([data, lateness]);
cmpTimer.CancelTimer(cancelId);
}
});
TS_ASSERT_EQUALS(cmpTimer.GetTime(), 0);
cmpTimer.OnUpdate({ "turnLength": 1/3 });
TS_ASSERT_EQUALS(cmpTimer.GetTime(), 333);
cmpTimer.SetTimeout(10, IID_Test, "Callback", 1000, "a");
cmpTimer.SetTimeout(10, IID_Test, "Callback", 1200, "b");
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, []);
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["a", 0]]);
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["a", 0], ["b", 300]]);
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["a", 0], ["b", 300]]);
fired = [];
var c = cmpTimer.SetTimeout(10, IID_Test, "Callback", 1000, "c");
var d = cmpTimer.SetTimeout(10, IID_Test, "Callback", 1000, "d");
var e = cmpTimer.SetTimeout(10, IID_Test, "Callback", 1000, "e");
cmpTimer.CancelTimer(d);
cmpTimer.OnUpdate({ "turnLength": 1.0 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["c", 0], ["e", 0]]);
fired = [];
var r = cmpTimer.SetInterval(10, IID_Test, "Callback", 500, 1000, "r");
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["r", 0]]);
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["r", 0]]);
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["r", 0], ["r", 0]]);
cmpTimer.OnUpdate({ "turnLength": 3.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["r", 0], ["r", 0], ["r", 2500], ["r", 1500], ["r", 500]]);
cmpTimer.CancelTimer(r);
cmpTimer.OnUpdate({ "turnLength": 3.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["r", 0], ["r", 0], ["r", 2500], ["r", 1500], ["r", 500]]);
fired = [];
cancelId = cmpTimer.SetInterval(20, IID_Test, "Callback", 500, 1000, "s");
cmpTimer.OnUpdate({ "turnLength": 3.0 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["s", 2500]]);
fired = [];
const f = cmpTimer.SetInterval(10, IID_Test, "Callback", 1000, 1000, "f");
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0]]);
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0], ["f", 0]]);
cmpTimer.UpdateRepeatTime(f, 500);
cmpTimer.OnUpdate({ "turnLength": 1.5 });
// Interval updated at next updated, so expecting latency here.
TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0], ["f", 0], ["f", 500], ["f", 0]]);
cmpTimer.OnUpdate({ "turnLength": 0.5 });
TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0], ["f", 0], ["f", 500], ["f", 0], ["f", 0]]);