Skip to content

Commit

Permalink
Move CNC tests, use shared fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
S-S-X committed Nov 13, 2021
1 parent 35ed5db commit 14c217b
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 72 deletions.
1 change: 1 addition & 0 deletions technic/spec/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require("mineunit")

-- Load fixtures required by tests
fixture("technic")
sourcefile("init")

describe("Technic API", function()

Expand Down
29 changes: 18 additions & 11 deletions technic/spec/fixtures/default.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

local function register_node(name)
minetest.register_node(":default:"..name, {
mineunit:set_modpath("default", "spec/fixtures")

local function register_node(name, groups, additional_definition)
local definition = {
description = name.." description",
tiles = { "default_"..name },
groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
})
groups = groups,
}
for k,v in pairs(additional_definition or {}) do definition[k] = v end
minetest.register_node(":default:"..name, definition)
end

local function register_item(name)
Expand All @@ -13,12 +17,15 @@ local function register_item(name)
})
end

register_node("furnace")
register_node("stone")
register_node("cobble")
register_node("sand")
register_node("sandstone")
register_node("wood")
register_node("steelblock")
-- Register some basic nodes for cutting, grinding, digging, registering recipes etc.
register_node("stone", {cracky = 3, stone = 1}, {is_ground_content = true, drop = "default:cobble"})
register_node("cobble", {cracky=3, stone = 2})
register_node("sand", {snappy=2, choppy=2, oddly_breakable_by_hand=2})
register_node("wood", {tree=1, choppy=2, oddly_breakable_by_hand=2})
register_node("dirt", {crumbly = 3, soil = 1})
register_node("sandstone", {crumbly = 1, cracky = 3})
register_node("steelblock", {cracky = 1, level = 2})
register_node("furnace", {cracky=2})
register_node("furnace_active", {cracky=2, not_in_creative_inventory=1}, {drop = "default:furnace"})

register_item("steel_ingot")
3 changes: 0 additions & 3 deletions technic/spec/fixtures/technic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ fixture("mesecons")
fixture("digilines")
fixture("pipeworks")
fixture("technic_worldgen")

-- Load mod
sourcefile("init")
8 changes: 0 additions & 8 deletions technic/spec/fixtures/technic_cnc.lua

This file was deleted.

1 change: 1 addition & 0 deletions technic/spec/hv_network_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require("mineunit")

-- Load complete technic mod
fixture("technic")
sourcefile("init")

describe("HV machine network", function()

Expand Down
1 change: 1 addition & 0 deletions technic/spec/lv_network_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require("mineunit")

-- Load complete technic mod
fixture("technic")
sourcefile("init")

describe("LV machine network", function()

Expand Down
1 change: 1 addition & 0 deletions technic/spec/nodes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require("mineunit")

-- Load complete technic mod
fixture("technic")
sourcefile("init")

describe("Technic node placement", function()

Expand Down
47 changes: 43 additions & 4 deletions technic_cnc/spec/digilines_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,38 @@ describe("CNC digiline API", function()
assert.equals(1, meta:get_int("size"))
end)

it("disables machine", function()
action(pos, table.copy(node), "ch1", "disable")
it("sets size", function()
action(pos, table.copy(node), "ch1", { size = 2 })
local meta = minetest.get_meta(pos)
assert.equals("cylinder", meta:get("program"))
assert.equals(2, meta:get_int("size"))
end)

it("table disables machine", function()
local meta = minetest.get_meta(pos)
assert.is_true(technic_cnc.is_enabled(meta))
action(pos, table.copy(node), "ch1", { enabled = false })
assert.is_false(technic_cnc.is_enabled(meta))
end)

it("enables machine", function()
action(pos, table.copy(node), "ch1", "enable")
it("table enables machine", function()
local meta = minetest.get_meta(pos)
assert.is_false(technic_cnc.is_enabled(meta))
action(pos, table.copy(node), "ch1", { enabled = true })
assert.is_true(technic_cnc.is_enabled(meta))
end)

it("string disables machine", function()
local meta = minetest.get_meta(pos)
assert.is_true(technic_cnc.is_enabled(meta))
action(pos, table.copy(node), "ch1", "disable")
assert.is_false(technic_cnc.is_enabled(meta))
end)

it("string enables machine", function()
local meta = minetest.get_meta(pos)
assert.is_false(technic_cnc.is_enabled(meta))
action(pos, table.copy(node), "ch1", "enable")
assert.is_true(technic_cnc.is_enabled(meta))
end)

Expand All @@ -78,4 +101,20 @@ describe("CNC digiline API", function()
assert.not_nil(response.msg.sphere)
end)

it("returns status", function()
local count = #digilines._msg_log
action(pos, table.copy(node), "ch1", "status")
assert.equals(count + 1, #digilines._msg_log)
local response = digilines._msg_log[#digilines._msg_log]
assert.is_table(response)
assert.is_table(response.msg)
assert.equals("ch1", response.channel)
assert.is_boolean(response.msg.enabled)
assert.is_number(response.msg.time)
assert.is_number(response.msg.size)
assert.is_string(response.msg.program)
assert.is_string(response.msg.user)
assert.is_table(response.msg.material)
end)

end)
10 changes: 0 additions & 10 deletions technic_cnc/spec/fixtures/default.lua

This file was deleted.

30 changes: 0 additions & 30 deletions technic_cnc/spec/fixtures/digilines.lua

This file was deleted.

7 changes: 6 additions & 1 deletion technic_cnc/spec/mineunit.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
time_step = 100
exclude = {
"../technic/",
"^../"
}
-- Using helper at technic/spec/fixtures/technic.lua for loading technic mod
fixture_paths = {
"../technic/spec/fixtures",
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ require("mineunit")
-- Load complete technic mod
fixture("technic")

describe("Technic CNC", function()
mineunit:set_modpath("technic", "../technic")
mineunit:set_current_modname("technic")
sourcefile("../technic/init")
mineunit:restore_current_modname()

sourcefile("init")

fixture("technic_cnc")
describe("Technic CNC", function()

local player = Player("SX")

Expand Down Expand Up @@ -39,14 +44,21 @@ describe("Technic CNC", function()
return inv:get_stack(listname or "dst", index or 1)
end

local function clear_itemstack(pos, listname)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
for index=1, inv:get_size(listname or "dst") do
inv:set_stack(listname or "dst", index, ItemStack())
end
end

-- Execute on mods loaded callbacks to finish loading.
mineunit:mods_loaded()
-- Tell mods that 1 minute passed already to execute all weird minetest.after hacks.
mineunit:execute_globalstep(60)

local cnc_pos = {x=0,y=1,z=0}
local program = next(technic_cnc.products)
local product_count = technic_cnc.products[program]
local program, product_count = next(technic_cnc.products)

describe("technic:cnc", function()

Expand All @@ -63,6 +75,7 @@ describe("Technic CNC", function()
end)

it("produces items", function()
clear_itemstack(cnc_pos)
local id = technic.pos2network(cnc_pos)
local net = technic.networks[id]
assert.not_nil(net)
Expand All @@ -81,6 +94,7 @@ describe("Technic CNC", function()
end)

it("uses energy", function()
clear_itemstack(cnc_pos)
place_itemstack(cnc_pos, "default:wood 99")
local id = technic.pos2network(cnc_pos)
local net = technic.networks[id]
Expand Down Expand Up @@ -111,6 +125,7 @@ describe("Technic CNC", function()
end)

it("produces items", function()
clear_itemstack(cnc_pos)
local id = technic.pos2network(cnc_pos)
local net = technic.networks[id]
assert.not_nil(net)
Expand All @@ -129,14 +144,46 @@ describe("Technic CNC", function()
end)

it("uses energy", function()
clear_itemstack(cnc_pos)
place_itemstack(cnc_pos, "default:wood 99")
local id = technic.pos2network(cnc_pos)
local net = technic.networks[id]
assert.not_nil(net)

-- Run network and check results
run_network()
assert.equal(net.demand, 900)
assert.equal(900, net.demand)
end)

it("disabling and enabling", function()
clear_itemstack(cnc_pos)
local id = technic.pos2network(cnc_pos)
local net = technic.networks[id]
assert.not_nil(net)

-- Fill input inventory and select program
local on_receive_fields = minetest.registered_nodes["technic:cnc_mk2"].on_receive_fields
assert.equals("function", type(on_receive_fields))
place_itemstack(cnc_pos, "default:wood 99")
on_receive_fields(cnc_pos, nil, {[program] = true}, player)

-- Clear and disable machine
local meta = minetest.get_meta(cnc_pos)
technic_cnc.disable(meta)

-- Run network and check results
run_network(10)
local products = get_itemstack(cnc_pos)
assert.is_true(products:is_empty())
assert.equal(0, net.demand)

-- Is enabled again when user selects program
on_receive_fields(cnc_pos, nil, {[program] = true}, player)
run_network(10)
products = get_itemstack(cnc_pos)
assert.is_ItemStack(products)
assert.gt(products:get_count(), product_count)
assert.equal(900, net.demand)
end)

end)
Expand Down

0 comments on commit 14c217b

Please sign in to comment.