1
0
cc-fake-peripheral/init.test.lua
2023-05-11 21:50:02 +03:00

196 lines
5.6 KiB
Lua

local lust = require("lust").nocolor()
local PeripheralAPIMock = require("init")
local describe, it, expect = lust.describe, lust.it, lust.expect
local function newPeripheralStub(name, types, methodNames)
local peripheral = {}
local metaTypes = {}
for i, ty in ipairs(types) do
metaTypes[i] = ty
metaTypes[ty] = true
end
setmetatable(peripheral, {
__name = "peripheral",
name = name,
type = types[1],
types = metaTypes
})
for _, methodName in pairs(methodNames) do
peripheral[methodName] = function() end
end
return peripheral
end
local function newInventoryStub(type, id)
return newPeripheralStub(("%s_%d"):format(type, id), {type, "inventory"}, {
"size",
"list",
"getItemDetail",
"getItemLimit",
"pushItems",
"pullItems"
})
end
local function pprint(...)
local pretty = require("cc.pretty")
pretty.print(pretty.pretty(...))
end
describe("peripherals mock", function()
it("initialize with no peripherals by default", function()
local peripheral = PeripheralAPIMock.new()
expect(peripheral:listPeripherals()).to.equal{}
end)
it("initialize with initial peripherals", function()
local chest1 = newInventoryStub("minecraft:chest", 1)
local chest2 = newInventoryStub("minecraft:chest", 2)
local peripheral = PeripheralAPIMock.new{ chest1, chest2 }
expect(peripheral:listPeripherals()).to.equal{
"minecraft:chest_1",
"minecraft:chest_2",
}
end)
it("throw error if given table is not a valid peripheral", function()
local chest = { }
expect(function()
PeripheralAPIMock.new{ chest }
end).to.fail()
end)
it("check if peripheral is added", function()
local chest = newInventoryStub("minecraft:chest", 1)
local peripheral = PeripheralAPIMock.new{ chest }
expect(peripheral:hasPeripheral(chest)).to.be.truthy()
end)
it("check if peripheral is removed", function()
local chest = newInventoryStub("minecraft:chest", 1)
local peripheral = PeripheralAPIMock.new{ chest }
expect(peripheral:hasPeripheral(chest)).to.be.truthy()
peripheral:removePeripheral(chest)
expect(peripheral:hasPeripheral(chest)).to_not.be.truthy()
end)
it("listPeripherals returns a new list every time", function()
local peripheral = PeripheralAPIMock.new()
expect(peripheral:listPeripherals()).to_not.be(peripheral:listPeripherals())
end)
describe("external api", function()
local chest1, chest2, api
lust.before(function()
chest1 = newInventoryStub("minecraft:chest", 1)
chest2 = newInventoryStub("minecraft:chest", 2)
local peripheral = PeripheralAPIMock.new{ chest1, chest2 }
api = peripheral:newExternalAPI()
end)
it("has the same methods as the real api", function()
for methodName in pairs(peripheral) do
assert(type(api[methodName]) == "function", ("Missing method %s"):format(methodName))
end
expect(#api).to.be(#peripheral)
end)
it("using .call and .wrap, invoke the same function", function()
local spy = lust.spy(chest1, "size")
api.call("minecraft:chest_1", "size")
expect(#spy).to.be(1)
api.wrap("minecraft:chest_1").size()
expect(#spy).to.be(2)
end)
it(".call dosen't supply self to methods", function()
local spy = lust.spy(chest1, "size")
api.call("minecraft:chest_1", "size", 1, 2, 3)
expect(spy).to.equal{{1, 2, 3}}
end)
it(".call returns nil if peripheral was not found", function()
expect(api.call("foobar", "size")).to.be(nil)
end)
it(".call throws error if peripheral dosen't have requested method", function()
expect(function()
api.call("minecraft:chest_1", "foobar")
end).to.fail()
end)
it(".wrap returns a new table each time, but have the same contents", function()
local name = "minecraft:chest_1"
expect(api.wrap(name)).to.equal(api.wrap(name))
expect(api.wrap(name)).to_not.be(api.wrap(name))
end)
it(".wrap returns nil if peripheral was not found", function()
expect(api.wrap("foobar")).to.be(nil)
end)
it("check if peripheral exists if .isPresent", function()
expect(api.isPresent("minecraft:chest_1")).to.be(true)
expect(api.isPresent("foobar")).to.be(false)
end)
it(".getType throws error if given table is not a peripheral", function()
expect(function ()
api.getType({})
end).to.fail()
expect(function ()
api.getType(chest1)
end).to_not.fail()
end)
it(".getType returns multiple types if that peripheral has it", function()
local types = {api.getType("minecraft:chest_1")}
expect(types).to.equal{"minecraft:chest", "inventory"}
end)
it(".getNames returns a new table each time, but have the same contents", function()
expect(api.getNames()).to.equal(api.getNames())
expect(api.getNames()).to_not.be(api.getNames())
end)
it(".getName throws error if table is not a valid peripheral", function()
expect(function()
api.getName({})
end).to.fail()
expect(function()
api.getName(chest1)
end).to_not.fail()
end)
it(".getMethods returns nil if peripheral is not found", function()
expect(api.getMethods("foobar")).to.be(nil)
end)
it(".hasType returns nil if peripheral is not found", function()
expect(api.hasType("foobar", "inventory")).to.be(nil)
end)
it(".hasType returns true if peripheral has target type", function()
expect(api.hasType(chest1, "inventory")).to.be(true)
end)
it(".find returns a list of wrapped peripherals", function()
local peripherals = api.find("inventory")
expect(peripherals).to.equal{ chest1, chest2 }
end)
it(".find with filter returns a list of wrapped peripherals", function()
local peripherals = api.find("inventory", function(_, wrapped)
return wrapped.size ~= nil
end)
expect(peripherals).to.equal{ chest1, chest2 }
end)
end)
end)