aboutsummaryrefslogtreecommitdiff
path: root/advtrains_interlocking/spec
diff options
context:
space:
mode:
authorY. Wang <yw05@forksworld.de>2022-06-11 18:07:00 +0200
committerY. Wang <yw05@forksworld.de>2023-03-23 20:06:02 +0100
commit98c37108762c6d7c9f1d691b84f49bfa65b81b28 (patch)
tree32402fd5365d14470016562f16a5bc5f560eec87 /advtrains_interlocking/spec
parentd1a0d8f2654d6ee64c1a43de7958b1eadfaff6b0 (diff)
downloadadvtrains-98c37108762c6d7c9f1d691b84f49bfa65b81b28.tar.gz
advtrains-98c37108762c6d7c9f1d691b84f49bfa65b81b28.tar.bz2
advtrains-98c37108762c6d7c9f1d691b84f49bfa65b81b28.zip
Implement primitive distant signaling
Diffstat (limited to 'advtrains_interlocking/spec')
-rw-r--r--advtrains_interlocking/spec/basic_signalling_spec.lua87
-rw-r--r--advtrains_interlocking/spec/signal_api_spec.lua49
2 files changed, 87 insertions, 49 deletions
diff --git a/advtrains_interlocking/spec/basic_signalling_spec.lua b/advtrains_interlocking/spec/basic_signalling_spec.lua
new file mode 100644
index 0000000..0b79972
--- /dev/null
+++ b/advtrains_interlocking/spec/basic_signalling_spec.lua
@@ -0,0 +1,87 @@
+--[[
+This file tests a large part of the signaling system, as a lot of tests for the
+signaling system tend to overlap for various parts of the system.
+]]
+
+require("mineunit")
+mineunit("core")
+
+_G.advtrains = {
+ interlocking = {
+ aspects = fixture("../../signal_aspects"),
+ },
+ ndb = {
+ get_node = minetest.get_node,
+ swap_node = minetest.swap_node,
+ }
+}
+
+fixture("advtrains_helpers")
+fixture("../../database")
+sourcefile("distant")
+sourcefile("signal_api")
+sourcefile("signal_aspect_accessors")
+fixture("../../demosignals")
+
+local D = advtrains.distant
+local I = advtrains.interlocking
+
+local stub_aspect_t1 = {
+ free = {main = -1},
+ slow = {main = 6},
+ danger = {main = 0, shunt = false},
+}
+local stub_pos_t1 = {}
+for i = 1, 3 do
+ stub_pos_t1[i] = {x = 1, y = 0, z = i}
+end
+
+world.layout {
+ {stub_pos_t1[1], "advtrains_interlocking:ds_danger"},
+ {stub_pos_t1[2], "advtrains_interlocking:ds_slow"},
+ {stub_pos_t1[3], "advtrains_interlocking:ds_free"},
+}
+
+describe("API for supposed signal aspects", function()
+ it("should load and save data properly", function()
+ local tbl = {_foo = true}
+ I.load_supposed_aspects(tbl)
+ assert.same(tbl, I.save_supposed_aspects())
+ end)
+ it("should set and get type 1 signals properly", function ()
+ local pos = stub_pos_t1[2]
+ local asp = stub_aspect_t1.slow
+ local newasp = { main = math.random(1,5) }
+ assert.same(asp, I.signal_get_aspect(pos))
+ I.signal_set_aspect(pos, newasp)
+ assert.same(newasp, I.signal_get_aspect(pos))
+ assert.same(asp, I.signal_get_real_aspect(pos))
+ I.signal_set_aspect(pos, asp)
+ end)
+end)
+
+describe("Distant signaling", function()
+ it("should assign distant signals and set the distant aspect correspondingly", function()
+ for i = 1, 2 do
+ D.assign(stub_pos_t1[i], stub_pos_t1[i+1])
+ end
+ assert.same(stub_aspect_t1.danger, I.signal_get_aspect(stub_pos_t1[1]))
+ assert.same({main = 6, dst = 0}, I.signal_get_aspect(stub_pos_t1[2]))
+ assert.same({main = -1, dst = 6}, I.signal_get_aspect(stub_pos_t1[3]))
+ end)
+ it("should report assignments properly", function()
+ assert.same({stub_pos_t1[1], "manual"}, {D.get_main(stub_pos_t1[2])})
+ assert.same({[advtrains.encode_pos(stub_pos_t1[3])] = "manual"}, D.get_dst(stub_pos_t1[2]))
+ end)
+ it("should update distant aspects automatically", function()
+ I.signal_set_aspect(stub_pos_t1[2], {main = 2, dst = -1})
+ assert.same({main = 2, dst = 0}, I.signal_get_aspect(stub_pos_t1[2]))
+ assert.same({main = -1, dst = 2}, I.signal_get_aspect(stub_pos_t1[3]))
+ end)
+ it("should unassign signals when one is removed", function()
+ world.set_node(stub_pos_t1[2], "air")
+ assert.same({}, D.get_dst(stub_pos_t1[1]))
+ assert.same({}, {D.get_main(stub_pos_t1[3])})
+ assert.same(stub_aspect_t1.free, I.signal_get_aspect(stub_pos_t1[3]))
+ end)
+end)
diff --git a/advtrains_interlocking/spec/signal_api_spec.lua b/advtrains_interlocking/spec/signal_api_spec.lua
deleted file mode 100644
index cd7a1d1..0000000
--- a/advtrains_interlocking/spec/signal_api_spec.lua
+++ /dev/null
@@ -1,49 +0,0 @@
-require("mineunit")
-
-mineunit("core")
-
-_G.advtrains = {
- interlocking = {
- aspects = fixture("../../signal_aspects"),
- },
- ndb = {
- get_node = minetest.get_node,
- }
-}
-
-fixture("advtrains_helpers")
-fixture("../../database")
-sourcefile("signal_api")
-
-local stub_aspect_t1 = { main = math.random() }
-local stub_pos_t1 = {x = 1, y = 0, z = 1}
-
-minetest.register_node(":stubsignal_t1", {
- advtrains = {
- supported_aspects = {},
- get_aspect = function () return stub_aspect_t1 end,
- set_aspect = function () end,
- },
- groups = { advtrains_signal = 2 },
-})
-
-world.layout {
- {stub_pos_t1, "stubsignal_t1"},
-}
-
-describe("API for supposed signal aspects", function()
- it("should load and save data properly", function()
- local tbl = {_foo = true}
- advtrains.interlocking.load_supposed_aspects(tbl)
- assert.same(tbl, advtrains.interlocking.save_supposed_aspects())
- end)
- it("should set and get type 1 signals properly", function ()
- local pos = stub_pos_t1
- local asp = stub_aspect_t1
- local newasp = { dst = math.random() }
- assert.same(asp, advtrains.interlocking.signal_get_aspect(pos))
- advtrains.interlocking.signal_set_aspect(pos, newasp)
- assert.same(newasp, advtrains.interlocking.signal_get_aspect(pos))
- assert.same(asp, advtrains.interlocking.signal_get_real_aspect(pos))
- end)
-end)