Skip to content

Commit

Permalink
Basic tests: technic_get_charge, technic_set_charge
Browse files Browse the repository at this point in the history
  • Loading branch information
S-S-X committed Jan 25, 2025
1 parent 0352cd7 commit 002d22a
Showing 1 changed file with 101 additions and 8 deletions.
109 changes: 101 additions & 8 deletions technic/spec/tools_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe("Technic power tool", function()

describe("API", function()

local TOOLNAME = "mymod:powertool1"

setup(function()
set_charge_stack(ItemStack(nil))
set_discharge_stack(ItemStack(nil))
Expand All @@ -60,7 +62,7 @@ describe("Technic power tool", function()
local use_RE_charge_result

it("technic.register_power_tool works", function()
core.register_item("mymod:powertool", {
core.register_item(TOOLNAME, {
type = "tool",
description = "My Mod Power Tool",
inventory_image = "mymod_powertool.png",
Expand All @@ -70,34 +72,34 @@ describe("Technic power tool", function()
return itemstack
end,
})
technic.power_tools["mymod:powertool"] = 1234
local itemdef = minetest.registered_items["mymod:powertool"]
technic.power_tools[TOOLNAME] = 1234
local itemdef = minetest.registered_items[TOOLNAME]
assert.is_hashed(itemdef)
end)

it("technic.use_RE_charge works (zero charge)", function()
local stack = ItemStack("mymod:powertool")
local stack = ItemStack(TOOLNAME)
local use_RE_charge_result = technic.use_RE_charge(stack, 123)
assert.equals("boolean", type(use_RE_charge_result))
assert.is_false(use_RE_charge_result)
end)

it("technic.get_RE_charge works (zero charge)", function()
local stack = ItemStack("mymod:powertool")
local stack = ItemStack(TOOLNAME)
local charge, max_charge = technic.get_RE_charge(stack)
assert.equals(0, charge)
assert.equals(1234, max_charge)
end)

it("technic.set_RE_charge works (zero charge -> 123)", function()
local stack = ItemStack("mymod:powertool")
local stack = ItemStack(TOOLNAME)
technic.set_RE_charge(stack, 123)
assert.equals(123, technic.get_RE_charge(stack))
end)

it("technic.use_RE_charge works (minimum charge)", function()
-- Add partially charged tool to player inventory
local stack = ItemStack("mymod:powertool")
local stack = ItemStack(TOOLNAME)
technic.set_RE_charge(stack, 123)
set_player_stack(stack)

Expand All @@ -112,7 +114,7 @@ describe("Technic power tool", function()

it("technic.use_RE_charge works (minimum charge + 1)", function()
-- Add partially charged tool to player inventory
local stack = ItemStack("mymod:powertool")
local stack = ItemStack(TOOLNAME)
technic.set_RE_charge(stack, 124)
set_player_stack(stack)
-- Use tool and verify results
Expand Down Expand Up @@ -165,4 +167,95 @@ describe("Technic power tool", function()

end)

describe("battery box", function()

local TOOLNAME = "mymod:powertool2"
local count_technic_get_charge
local count_technic_set_charge

setup(function()
mineunit:set_current_modname("mymod")
core.register_item(TOOLNAME, {
type = "tool",
description = "My Mod Power Tool",
inventory_image = "mymod_powertool.png",
wear_represents = "technic_RE_charge",
technic_get_charge = function(itemstack)
assert.is_ItemStack(itemstack)
count_technic_get_charge = count_technic_get_charge + 1
return technic.get_charge(itemstack)
end,
technic_set_charge = function(itemstack, charge)
assert.is_ItemStack(itemstack)
assert.is_number(charge)
count_technic_set_charge = count_technic_set_charge + 1
return technic.set_charge(itemstack, charge)
end,
on_use = function(itemstack, player, pointed_thing)
use_RE_charge_result = technic.use_RE_charge(itemstack, 123)
return itemstack
end,
})
technic.power_tools[TOOLNAME] = 100000
mineunit:restore_current_modname()
mineunit:execute_on_joinplayer(player)
end)

teardown(function()
mineunit:execute_on_leaveplayer(player)
end)

before_each(function()
set_charge_stack(ItemStack(nil))
set_discharge_stack(ItemStack(nil))
count_technic_get_charge = 0
count_technic_set_charge = 0
end)

local use_RE_charge_result

it("registered test tool", function()
local itemdef = minetest.registered_items[TOOLNAME]
assert.is_hashed(itemdef)
end)

it("discharges tool", function()
-- Add partially charged tool to player inventory
local stack = ItemStack(TOOLNAME)
technic.set_RE_charge(stack, 100000)
set_discharge_stack(stack)
-- First discharge step, 60k charge (discharge step is 40k/each)
mineunit:execute_globalstep(1)
assert.equals(60000, technic.get_RE_charge(get_discharge_stack()))
-- Second discharge steps, 20k charge
mineunit:execute_globalstep(1)
assert.equals(20000, technic.get_RE_charge(get_discharge_stack()))
-- Last discharge step, no charge
mineunit:execute_globalstep(1)
assert.equals(0, technic.get_RE_charge(get_discharge_stack()))
-- technic.get_RE_charge does not currently use technic_get_charge
assert.equals(count_technic_get_charge, 3)
assert.equals(count_technic_set_charge, 3)
end)

it("charges tool", function()
-- Add partially charged tool to player inventory
local stack = ItemStack(TOOLNAME)
set_charge_stack(stack)
-- First charge step, 10k charge (charge step is 10k/each)
mineunit:execute_globalstep(1)
assert.equals(10000, technic.get_RE_charge(get_charge_stack()))
-- Eight charge steps, 90k charge
for i = 1, 8 do mineunit:execute_globalstep(1) end
assert.equals(90000, technic.get_RE_charge(get_charge_stack()))
-- Last charge step, full charge
mineunit:execute_globalstep(1)
assert.equals(100000, technic.get_RE_charge(get_charge_stack()))
-- technic.get_RE_charge does not currently use technic_get_charge
assert.equals(count_technic_get_charge, 10)
assert.equals(count_technic_set_charge, 10)
end)

end)

end)

0 comments on commit 002d22a

Please sign in to comment.