diff --git a/build.gradle.kts b/build.gradle.kts index 7a5c1f845..5691dc3c0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -300,7 +300,7 @@ val docWebsite by tasks.registering(Copy::class) { // Check tasks tasks.test { - systemProperty("cct.test-files", buildDir.resolve("tmp/test-files").absolutePath) + systemProperty("cct.test-files", buildDir.resolve("tmp/testFiles").absolutePath) } val lintLua by tasks.registering(IlluaminateExec::class) { diff --git a/src/main/resources/data/computercraft/lua/rom/apis/peripheral.lua b/src/main/resources/data/computercraft/lua/rom/apis/peripheral.lua index f5e8fcc8b..b10530232 100644 --- a/src/main/resources/data/computercraft/lua/rom/apis/peripheral.lua +++ b/src/main/resources/data/computercraft/lua/rom/apis/peripheral.lua @@ -217,7 +217,7 @@ function getMethods(name) end for n = 1, #sides do local side = sides[n] - if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then + if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then return native.call(side, "getMethodsRemote", name) end end @@ -257,7 +257,7 @@ function call(name, method, ...) for n = 1, #sides do local side = sides[n] - if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then + if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then return native.call(side, "callRemote", name, method, ...) end end diff --git a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java index 6ac766bc7..095ede05f 100644 --- a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java +++ b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java @@ -16,13 +16,9 @@ import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.filesystem.FileMount; import dan200.computercraft.core.filesystem.FileSystemException; import dan200.computercraft.core.terminal.Terminal; -import dan200.computercraft.shared.peripheral.modem.ModemState; -import dan200.computercraft.shared.peripheral.modem.wireless.WirelessModemPeripheral; import dan200.computercraft.support.TestFiles; import dan200.computercraft.test.core.computer.BasicEnvironment; import dan200.computercraft.test.core.computer.FakeMainThreadScheduler; -import net.minecraft.util.math.vector.Vector3d; -import net.minecraft.world.World; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.*; @@ -118,6 +114,7 @@ public class ComputerTestDelegate context = new ComputerContext( environment, 1, new FakeMainThreadScheduler() ); computer = new Computer( context, environment, term, 0 ); computer.getEnvironment().setPeripheral( ComputerSide.TOP, new FakeModem() ); + computer.getEnvironment().setPeripheral( ComputerSide.BOTTOM, new FakePeripheralHub() ); computer.addApi( new CctTestAPI() ); computer.turnOn(); @@ -282,26 +279,13 @@ public class ComputerTestDelegate return name.replace( "\0", " -> " ); } - private static class FakeModem extends WirelessModemPeripheral + public static class FakeModem implements IPeripheral { - FakeModem() - { - super( new ModemState(), true ); - } - @Nonnull @Override - @SuppressWarnings( "ConstantConditions" ) - public World getWorld() + public String getType() { - return null; - } - - @Nonnull - @Override - public Vector3d getPosition() - { - return Vector3d.ZERO; + return "modem"; } @Override @@ -309,6 +293,58 @@ public class ComputerTestDelegate { return this == other; } + + @LuaFunction + public final boolean isOpen( int channel ) + { + return false; + } + } + + public static class FakePeripheralHub implements IPeripheral + { + @Nonnull + @Override + public String getType() + { + return "peripheral_hub"; + } + + @Override + public boolean equals( @Nullable IPeripheral other ) + { + return this == other; + } + + @LuaFunction + public final Collection getNamesRemote() + { + return Collections.singleton( "remote_1" ); + } + + @LuaFunction + public final boolean isPresentRemote( String name ) + { + return name.equals( "remote_1" ); + } + + @LuaFunction + public final Object[] getTypeRemote( String name ) + { + return name.equals( "remote_1" ) ? new Object[] { "remote", "other_type" } : null; + } + + @LuaFunction + public final Object[] hasTypeRemote( String name, String type ) + { + return name.equals( "remote_1" ) ? new Object[] { type.equals( "remote" ) || type.equals( "other_type" ) } : null; + } + + @LuaFunction + public final Object[] getMethodsRemote( String name ) + { + return name.equals( "remote_1" ) ? new Object[] { Collections.singletonList( "func" ) } : null; + } } public class CctTestAPI implements ILuaAPI diff --git a/src/test/resources/test-rom/spec/apis/peripheral_spec.lua b/src/test/resources/test-rom/spec/apis/peripheral_spec.lua index c64c042ce..36d507a5e 100644 --- a/src/test/resources/test-rom/spec/apis/peripheral_spec.lua +++ b/src/test/resources/test-rom/spec/apis/peripheral_spec.lua @@ -1,5 +1,6 @@ describe("The peripheral library", function() local it_modem = peripheral.getType("top") == "modem" and it or pending + local it_remote = peripheral.getType("bottom") == "peripheral_hub" and it or pending local multitype_peripheral = setmetatable({}, { __name = "peripheral", @@ -13,6 +14,16 @@ describe("The peripheral library", function() peripheral.isPresent("") expect.error(peripheral.isPresent, nil):eq("bad argument #1 (expected string, got nil)") end) + + it_modem("asserts the presence of local peripherals", function() + expect(peripheral.isPresent("top")):eq(true) + expect(peripheral.isPresent("left")):eq(false) + end) + + it_remote("asserts the presence of remote peripherals", function() + expect(peripheral.isPresent("remote_1")):eq(true) + expect(peripheral.isPresent("remote_2")):eq(false) + end) end) describe("peripheral.getName", function() @@ -24,6 +35,10 @@ describe("The peripheral library", function() it_modem("can get the name of a wrapped peripheral", function() expect(peripheral.getName(peripheral.wrap("top"))):eq("top") end) + + it("can get the name of a fake peripheral", function() + expect(peripheral.getName(multitype_peripheral)):eq("top") + end) end) describe("peripheral.getType", function() @@ -34,13 +49,18 @@ describe("The peripheral library", function() end) it("returns nil when no peripheral is present", function() - expect(peripheral.getType("bottom")):eq(nil) + expect(peripheral.getType("left")):eq(nil) + expect(peripheral.getType("remote_2")):eq(nil) end) - it_modem("can get the type of a peripheral by side", function() + it_modem("can get the type of a local peripheral", function() expect(peripheral.getType("top")):eq("modem") end) + it_remote("can get the type of a remote peripheral", function() + expect(peripheral.getType("remote_1")):eq("remote") + end) + it_modem("can get the type of a wrapped peripheral", function() expect(peripheral.getType(peripheral.wrap("top"))):eq("modem") end) @@ -59,7 +79,8 @@ describe("The peripheral library", function() end) it("returns nil when no peripherals are present", function() - expect(peripheral.hasType("bottom", "modem")):eq(nil) + expect(peripheral.hasType("left", "modem")):eq(nil) + expect(peripheral.hasType("remote_2", "remote")):eq(nil) end) it_modem("can check type of a peripheral by side", function() @@ -76,6 +97,10 @@ describe("The peripheral library", function() expect(peripheral.hasType(multitype_peripheral, "inventory")):eq(true) expect(peripheral.hasType(multitype_peripheral, "something else")):eq(false) end) + + it_remote("can check type of a remote peripheral", function() + expect(peripheral.hasType("remote_1", "remote")):eq(true) + end) end) describe("peripheral.getMethods", function() @@ -103,6 +128,18 @@ describe("The peripheral library", function() peripheral.wrap("") expect.error(peripheral.wrap, nil):eq("bad argument #1 (expected string, got nil)") end) + + it_modem("wraps a local peripheral", function() + local p = peripheral.wrap("top") + expect(type(p)):eq("table") + expect(type(next(p))):eq("string") + end) + + it_remote("wraps a remote peripheral", function() + local p = peripheral.wrap("remote_1") + expect(type(p)):eq("table") + expect(next(p)):eq("func") + end) end) describe("peripheral.find", function() @@ -113,5 +150,17 @@ describe("The peripheral library", function() expect.error(peripheral.find, nil):eq("bad argument #1 (expected string, got nil)") expect.error(peripheral.find, "", false):eq("bad argument #2 (expected function, got boolean)") end) + + it_modem("finds a local peripheral", function() + local p = peripheral.find("modem") + expect(type(p)):eq("table") + expect(peripheral.getName(p)):eq("top") + end) + + it_modem("finds a local peripheral", function() + local p = peripheral.find("remote") + expect(type(p)):eq("table") + expect(peripheral.getName(p)):eq("remote_1") + end) end) end)