make all test modules work with lunitx

This commit is contained in:
Enno Rehling 2020-04-22 03:03:44 +02:00
parent f5d352b680
commit 5fb8757047
14 changed files with 385 additions and 333 deletions

View File

@ -899,307 +899,3 @@ function test_walk_and_carry_the_cart()
assert_equal(1, u.region.x) assert_equal(1, u.region.x)
end end
module("tests.recruit", package.seeall, lunit.testcase)
function setup()
eressea.free_game()
eressea.settings.set("rules.food.flags", "4")
eressea.settings.set("rules.peasants.growth", "0")
end
function test_bug_1795_limit()
local r = region.create(0, 0, "plain")
local f = create_faction('human')
local u1 = one_unit(r,f)
u1:add_item("money", 100000000)
u1:add_order("REKRUTIEREN 9999")
r:set_resource("peasant", 2000) -- no fractional growth!
local peasants = r:get_resource("peasant")
local limit,frac = math.modf(peasants/40) -- one day this should be a parameter
process_orders()
assert_equal(limit+1, u1.number, u1.number .. "!=" .. (limit+1))
assert_equal(peasants-limit, r:get_resource("peasant"))
end
function test_bug_1795_demons()
local r = region.create(0, 0, "plain")
local f = create_faction('demon')
local u1 = one_unit(r,f)
r:set_resource("peasant", 2000)
local peasants = r:get_resource("peasant")
local limit,frac = math.modf(peasants/40)
u1:add_item("money", 100000000)
u1:add_order("REKRUTIEREN 9999")
process_orders()
assert_equal(limit+1, u1.number, u1.number .. "!=" .. (limit+1))
assert_equal(peasants, r:get_resource("peasant"))
end
module("tests.parser", package.seeall, lunit.testcase)
function setup()
eressea.free_game()
eressea.settings.set("rules.food.flags", "4") -- FOOD_IS_FREE
eressea.settings.set("rules.move.owner_leave", "0")
end
function test_parser()
local r = region.create(0, 0, "mountain")
local f = create_faction('human')
local u = unit.create(f, r, 1)
local filename = "orders.txt"
local file = io.open(filename, "w")
assert_not_nil(file)
f.password = 'Hodor'
file:write('ERESSEA ' .. itoa36(f.id) .. ' "Hodor"\n')
file:write('EINHEIT ' .. itoa36(u.id) .. "\n")
file:write("BENENNEN EINHEIT 'Goldene Herde'\n")
file:close()
eressea.read_orders(filename)
process_orders()
os.remove(filename)
assert_equal("Goldene Herde", u.name)
end
local function set_order(u, str)
u:clear_orders()
u:add_order(str)
end
function test_prefix()
local r0 = region.create(0, 0, "plain")
local f1 = create_faction('human')
local u1 = unit.create(f1, r0, 1)
set_order(u1, "PRAEFIX See")
process_orders()
assert_not_nil(u1:show():find("Seemensch"))
u1.race = "elf"
assert_not_nil(u1:show():find("Seeelf"))
set_order(u1, "PRAEFIX Mond")
process_orders()
assert_not_nil(u1:show():find("Mondelf"))
set_order(u1, "PRAEFIX")
process_orders()
assert_not_nil(u1:show():find("Elf"))
set_order(u1, "PRAEFIX Erz")
process_orders()
assert_not_nil(u1:show():find("Erzelf"))
u1.faction.locale = "en"
assert_not_nil(u1:show():find("archelf"))
end
function test_recruit()
local r = region.create(0, 0, "plain")
local f = create_faction('human')
local u = unit.create(f, r, 1)
u:add_item("money", 1000)
set_order(u, "REKRUTIERE 5")
process_orders()
for u in f.units do
assert_equal(6, u.number)
end
end
function test_give_horses()
local r = region.create(0, 0, "plain")
local f = faction.create("human", "noreply@eressea.de", "de")
local u = unit.create(f, r, 1)
r:set_resource("horse", 0)
u:add_item("horse", 21)
u:add_item("dolphin", 10)
u:add_order("GIB 0 7 PFERD")
u:add_order("GIB 0 5 DELPHIN")
process_orders()
assert_equal(7, r:get_resource("horse"))
assert_equal(5, u:get_item("dolphin"))
assert_equal(14, u:get_item("horse"))
end
function test_give_silver()
local r = region.create(0, 0, "plain")
local f = faction.create("human", "noreply@eressea.de", "de")
local u = unit.create(f, r, 1)
r:set_resource("peasant", 0)
r:set_resource("money", 11)
u:clear_orders()
u:add_item("money", 20)
u:add_order("GIB 0 10 SILBER")
process_orders()
assert_equal(21, r:get_resource("money"))
assert_equal(10, u:get_item("money"))
end
function test_give_horses()
local r = region.create(0, 0, "plain")
local f = create_faction('human')
local u = unit.create(f, r, 1)
r:set_resource("horse", 0)
u:add_item("horse", 21)
u:add_item("dolphin", 10)
u:add_order("GIB 0 7 PFERD")
u:add_order("GIB 0 5 DELPHIN")
process_orders()
assert_equal(7, r:get_resource("horse"))
assert_equal(5, u:get_item("dolphin"))
assert_equal(14, u:get_item("horse"))
end
function test_give_silver()
local r = region.create(0, 0, "plain")
local f = create_faction('human')
local u = unit.create(f, r, 1)
r:set_resource("peasant", 0)
r:set_resource("money", 11)
u:clear_orders()
u:add_item("money", 20)
u:add_order("GIB 0 10 SILBER")
process_orders()
assert_equal(21, r:get_resource("money"))
assert_equal(10, u:get_item("money"))
end
function test_build_castle_one_stage()
local r = region.create(0, 0, 'plain')
local f = faction.create('human')
local u = unit.create(f, r, 2)
u:add_item('stone', 4)
u:set_skill('building', 1)
u:add_order('MACHE BURG')
process_orders()
assert_equal(2, u.building.size)
assert_equal(2, u:get_item('stone'))
end
function test_build_castle()
local r = region.create(0, 0, "plain")
local f = create_faction('human')
local u = unit.create(f, r, 1)
u:add_item('stone', 1)
u:set_skill('building', 1)
u:add_order("MACHE BURG")
process_orders()
assert_not_nil(u.building)
assert_equal(1, u.building.size)
assert_equal(u.building.name, "Burg")
end
function test_route()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE O W P")
process_orders()
assert_equal("ROUTE West PAUSE Ost", u:get_order(0))
assert_equal(r2, u.region)
end
function test_route_horse()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE O P W P")
u:add_item('horse', 1)
u:set_skill('riding', 1)
process_orders()
assert_equal("ROUTE West PAUSE Ost PAUSE", u:get_order(0))
assert_equal(r2, u.region)
end
function test_route_pause()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE P O W")
process_orders()
assert_equal("ROUTE P O W", u:get_order(0))
assert_equal(r1, u.region)
end
function test_immunity_stops_guard()
eressea.settings.set("NewbieImmunity", 2)
local f = faction.create('human')
local r = region.create(0, 0, 'plain')
local u = unit.create(f, r)
u:set_skill('polearm', 2)
u:add_item('lance', 1)
u:add_order('BEWACHE')
process_orders()
assert_equal(f.age, 1)
assert_true(not u.guard)
process_orders()
assert_equal(f.age, 2)
assert_true(u.guard)
end
function test_region_keys()
local r = region.create(0, 0, 'plain')
assert_nil(r:get_key('test'))
assert_nil(r:get_key('more'))
r:set_key('test', 42)
r:set_key('more') -- default is 1
assert_equal(42, r:get_key('test'))
assert_equal(1, r:get_key('more'))
end
function test_faction_keys()
local f = faction.create('human')
assert_nil(f:get_key('test'))
assert_nil(f:get_key('more'))
f:set_key('test', 42)
f:set_key('more') -- default is 1
assert_equal(42, f:get_key('test'))
assert_equal(1, f:get_key('more'))
end
function test_cartmaking()
local f = faction.create('human')
local r = region.create(0, 0, 'plain')
local u = unit.create(f, r)
u:set_skill('cartmaking', 1)
u:add_item('log', 10)
u:add_order('MACHE Wagen')
process_orders()
assert_equal(1, u:get_item('cart'))
assert_equal(5, u:get_item('log'))
end
function test_promote_after_recruit()
local f = faction.create('human')
local r1 = region.create(0, 0, 'plain')
local r2 = region.create(1, 0, 'plain')
local u1 = unit.create(f, r1, 1)
u1.name = 'Xolgrim'
local u2 = unit.create(f, r2, 55)
u2:add_order('REKRUTIERE 1')
u1:add_order('BEFOERDERE')
u1:add_item('money', 57)
u2:add_item('money', 150)
local fl = u1.flags
process_orders()
assert_equal(56, u2.number)
assert_equal(fl + 128, u1.flags) -- UFL_HERO
assert_equal(0, u1:get_item('money'))
end

View File

@ -21,6 +21,8 @@ require 'tests.items'
require 'tests.economy' require 'tests.economy'
require 'tests.orders' require 'tests.orders'
require 'tests.common' require 'tests.common'
require 'tests.recruit'
require 'tests.parser'
require 'tests.report' require 'tests.report'
require 'tests.storage' require 'tests.storage'
require 'tests.magicbag' require 'tests.magicbag'

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.buildings'
local lunit = require("lunit")
module("tests.e3.buildings", package.seeall, lunit.testcase ) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup() function setup()
eressea.game.reset() eressea.game.reset()

View File

@ -10,6 +10,8 @@ require 'tests.spells'
require 'tests.economy' require 'tests.economy'
require 'tests.orders' require 'tests.orders'
require 'tests.common' require 'tests.common'
require 'tests.recruit'
require 'tests.parser'
require 'tests.items' require 'tests.items'
require 'tests.magicbag' require 'tests.magicbag'
require 'tests.process' require 'tests.process'

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.items'
local lunit = require("lunit")
module("tests.e3.items", package.seeall, lunit.testcase ) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup() function setup()
eressea.game.reset() eressea.game.reset()

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.morale'
local lunit = require("lunit")
module("tests.e3.morale", package.seeall, lunit.testcase ) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup() function setup()
eressea.game.reset() eressea.game.reset()
@ -171,7 +175,7 @@ function test_morale_change()
run_a_turn() run_a_turn()
assert_equal(4, r.morale) assert_equal(4, r.morale)
-- regel: ohne herrscher fällt die moral jede woche um 1 punkt, bis sie 1 erreicht -- regel: ohne herrscher fällt die moral jede woche um 1 punkt, bis sie 1 erreicht
assert_false(r.is_mourning) assert_false(r.is_mourning)
u1.building = nil u1.building = nil
update_owners() update_owners()
@ -186,7 +190,7 @@ function test_morale_change()
run_a_turn() run_a_turn()
assert_equal(1, r.morale) assert_equal(1, r.morale)
-- ohne herrscher ändert sich auch beschissene Moral nicht: -- ohne herrscher ändert sich auch beschissene Moral nicht:
r.morale = 0 r.morale = 0
run_a_turn() run_a_turn()
assert_equal(0, r.morale) assert_equal(0, r.morale)

View File

@ -1,6 +1,10 @@
require 'lunit' local tcname = 'tests.e3.parser'
local lunit = require("lunit")
module('tests.e3.parser', package.seeall, lunit.testcase) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup() function setup()
eressea.game.reset() eressea.game.reset()

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.production'
local lunit = require("lunit")
module("tests.e3.production", package.seeall, lunit.testcase ) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup() function setup()
eressea.game.reset() eressea.game.reset()

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.rules'
local lunit = require("lunit")
module("tests.e3.e3features", package.seeall, lunit.testcase) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
local settings local settings

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.e2spells'
local lunit = require("lunit")
module("tests.e3.spells-e2", package.seeall, lunit.testcase) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
local r, f, u local r, f, u

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.spells'
local lunit = require("lunit")
module("tests.e3.spells", package.seeall, lunit.testcase) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup() function setup()
eressea.game.reset() eressea.game.reset()

View File

@ -1,6 +1,10 @@
require "lunit" local tcname = 'tests.e3.stealth'
local lunit = require("lunit")
module("tests.e3.stealth", package.seeall, lunit.testcase) if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
local f local f
local u local u

270
scripts/tests/parser.lua Normal file
View File

@ -0,0 +1,270 @@
local tcname = 'tests.shared.parser'
local lunit = require("lunit")
if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup()
eressea.free_game()
eressea.settings.set("rules.food.flags", "4") -- FOOD_IS_FREE
eressea.settings.set("rules.move.owner_leave", "0")
end
function test_parser()
local r = region.create(0, 0, "mountain")
local f = faction.create('human')
local u = unit.create(f, r, 1)
local filename = "orders.txt"
local file = io.open(filename, "w")
assert_not_nil(file)
f.password = 'Hodor'
file:write('ERESSEA ' .. itoa36(f.id) .. ' "Hodor"\n')
file:write('EINHEIT ' .. itoa36(u.id) .. "\n")
file:write("BENENNEN EINHEIT 'Goldene Herde'\n")
file:close()
eressea.read_orders(filename)
process_orders()
os.remove(filename)
assert_equal("Goldene Herde", u.name)
end
local function set_order(u, str)
u:clear_orders()
u:add_order(str)
end
function test_prefix()
local r0 = region.create(0, 0, "plain")
local f1 = faction.create('human')
local u1 = unit.create(f1, r0, 1)
set_order(u1, "PRAEFIX See")
process_orders()
assert_not_nil(u1:show():find("Seemensch"))
u1.race = "elf"
assert_not_nil(u1:show():find("Seeelf"))
set_order(u1, "PRAEFIX Mond")
process_orders()
assert_not_nil(u1:show():find("Mondelf"))
set_order(u1, "PRAEFIX")
process_orders()
assert_not_nil(u1:show():find("Elf"))
set_order(u1, "PRAEFIX Erz")
process_orders()
assert_not_nil(u1:show():find("Erzelf"))
u1.faction.locale = "en"
assert_not_nil(u1:show():find("archelf"))
end
function test_recruit()
local r = region.create(0, 0, "plain")
local f = faction.create('human')
local u = unit.create(f, r, 1)
u:add_item("money", 1000)
set_order(u, "REKRUTIERE 5")
process_orders()
for u in f.units do
assert_equal(6, u.number)
end
end
function test_give_horses()
local r = region.create(0, 0, "plain")
local f = faction.create("human", "noreply@eressea.de", "de")
local u = unit.create(f, r, 1)
r:set_resource("horse", 0)
u:add_item("horse", 21)
u:add_item("dolphin", 10)
u:add_order("GIB 0 7 PFERD")
u:add_order("GIB 0 5 DELPHIN")
process_orders()
assert_equal(7, r:get_resource("horse"))
assert_equal(5, u:get_item("dolphin"))
assert_equal(14, u:get_item("horse"))
end
function test_give_silver()
local r = region.create(0, 0, "plain")
local f = faction.create("human", "noreply@eressea.de", "de")
local u = unit.create(f, r, 1)
r:set_resource("peasant", 0)
r:set_resource("money", 11)
u:clear_orders()
u:add_item("money", 20)
u:add_order("GIB 0 10 SILBER")
process_orders()
assert_equal(21, r:get_resource("money"))
assert_equal(10, u:get_item("money"))
end
function test_give_horses()
local r = region.create(0, 0, "plain")
local f = faction.create('human')
local u = unit.create(f, r, 1)
r:set_resource("horse", 0)
u:add_item("horse", 21)
u:add_item("dolphin", 10)
u:add_order("GIB 0 7 PFERD")
u:add_order("GIB 0 5 DELPHIN")
process_orders()
assert_equal(7, r:get_resource("horse"))
assert_equal(5, u:get_item("dolphin"))
assert_equal(14, u:get_item("horse"))
end
function test_give_silver()
local r = region.create(0, 0, "plain")
local f = faction.create('human')
local u = unit.create(f, r, 1)
r:set_resource("peasant", 0)
r:set_resource("money", 11)
u:clear_orders()
u:add_item("money", 20)
u:add_order("GIB 0 10 SILBER")
process_orders()
assert_equal(21, r:get_resource("money"))
assert_equal(10, u:get_item("money"))
end
function test_build_castle_one_stage()
local r = region.create(0, 0, 'plain')
local f = faction.create('human')
local u = unit.create(f, r, 2)
u:add_item('stone', 4)
u:set_skill('building', 1)
u:add_order('MACHE BURG')
process_orders()
assert_equal(2, u.building.size)
assert_equal(2, u:get_item('stone'))
end
function test_build_castle()
local r = region.create(0, 0, "plain")
local f = faction.create('human')
local u = unit.create(f, r, 1)
u:add_item('stone', 1)
u:set_skill('building', 1)
u:add_order("MACHE BURG")
process_orders()
assert_not_nil(u.building)
assert_equal(1, u.building.size)
assert_equal(u.building.name, "Burg")
end
function test_route()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE O W P")
process_orders()
assert_equal("ROUTE West PAUSE Ost", u:get_order(0))
assert_equal(r2, u.region)
end
function test_route_horse()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE O P W P")
u:add_item('horse', 1)
u:set_skill('riding', 1)
process_orders()
assert_equal("ROUTE West PAUSE Ost PAUSE", u:get_order(0))
assert_equal(r2, u.region)
end
function test_route_pause()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE P O W")
process_orders()
assert_equal("ROUTE P O W", u:get_order(0))
assert_equal(r1, u.region)
end
function test_immunity_stops_guard()
eressea.settings.set("NewbieImmunity", 2)
local f = faction.create('human')
local r = region.create(0, 0, 'plain')
local u = unit.create(f, r)
u:set_skill('polearm', 2)
u:add_item('lance', 1)
u:add_order('BEWACHE')
process_orders()
assert_equal(f.age, 1)
assert_true(not u.guard)
process_orders()
assert_equal(f.age, 2)
assert_true(u.guard)
end
function test_region_keys()
local r = region.create(0, 0, 'plain')
assert_nil(r:get_key('test'))
assert_nil(r:get_key('more'))
r:set_key('test', 42)
r:set_key('more') -- default is 1
assert_equal(42, r:get_key('test'))
assert_equal(1, r:get_key('more'))
end
function test_faction_keys()
local f = faction.create('human')
assert_nil(f:get_key('test'))
assert_nil(f:get_key('more'))
f:set_key('test', 42)
f:set_key('more') -- default is 1
assert_equal(42, f:get_key('test'))
assert_equal(1, f:get_key('more'))
end
function test_cartmaking()
local f = faction.create('human')
local r = region.create(0, 0, 'plain')
local u = unit.create(f, r)
u:set_skill('cartmaking', 1)
u:add_item('log', 10)
u:add_order('MACHE Wagen')
process_orders()
assert_equal(1, u:get_item('cart'))
assert_equal(5, u:get_item('log'))
end
function test_promote_after_recruit()
local f = faction.create('human')
local r1 = region.create(0, 0, 'plain')
local r2 = region.create(1, 0, 'plain')
local u1 = unit.create(f, r1, 1)
u1.name = 'Xolgrim'
local u2 = unit.create(f, r2, 55)
u2:add_order('REKRUTIERE 1')
u1:add_order('BEFOERDERE')
u1:add_item('money', 57)
u2:add_item('money', 150)
local fl = u1.flags
process_orders()
assert_equal(56, u2.number)
assert_equal(fl + 128, u1.flags) -- UFL_HERO
assert_equal(0, u1:get_item('money'))
end

46
scripts/tests/recruit.lua Normal file
View File

@ -0,0 +1,46 @@
local tcname = 'tests.shared.recruit'
local lunit = require("lunit")
if _VERSION >= 'Lua 5.2' then
_ENV = module(tcname, 'seeall')
else
module(tcname, 'seeall')
end
function setup()
eressea.free_game()
eressea.settings.set("rules.food.flags", "4")
eressea.settings.set("rules.peasants.growth", "0")
end
function test_bug_1795_limit()
local r = region.create(0, 0, "plain")
local f = faction.create('human')
local u1 = unit.create(f, r, 1)
u1:add_item("money", 100000000)
u1:add_order("REKRUTIEREN 9999")
r:set_resource("peasant", 2000) -- no fractional growth!
local peasants = r:get_resource("peasant")
local limit,frac = math.modf(peasants/40) -- one day this should be a parameter
process_orders()
assert_equal(limit+1, u1.number, u1.number .. "!=" .. (limit+1))
local np = r:get_resource("peasant")
assert_equal(peasants-limit, np)
end
function test_bug_1795_demons()
local r = region.create(0, 0, "plain")
local f = faction.create('demon')
local u1 = unit.create(f, r, 1)
r:set_resource("peasant", 2000)
local peasants = r:get_resource("peasant")
local limit,frac = math.modf(peasants/40)
u1:add_item("money", 100000000)
u1:add_order("REKRUTIEREN 9999")
process_orders()
assert_equal(limit+1, u1.number, u1.number .. "!=" .. (limit+1))
assert_equal(peasants, r:get_resource("peasant"))
end