2020-04-19 21:35:48 +02:00
|
|
|
local tcname = 'tests.shared.storage'
|
2020-05-03 16:52:40 +02:00
|
|
|
local lunit = require('lunit')
|
2020-04-19 21:35:48 +02:00
|
|
|
if _VERSION >= 'Lua 5.2' then
|
|
|
|
_ENV = module(tcname, 'seeall')
|
|
|
|
else
|
2020-05-03 16:52:40 +02:00
|
|
|
module(tcname, lunit.testcase, package.seeall)
|
2020-04-19 21:35:48 +02:00
|
|
|
end
|
2015-07-01 21:24:44 +02:00
|
|
|
|
|
|
|
function setup()
|
|
|
|
eressea.free_game()
|
|
|
|
end
|
|
|
|
|
|
|
|
function test_store_unit()
|
|
|
|
local r = region.create(0, 0, "plain")
|
2017-03-17 22:31:59 +01:00
|
|
|
local f = faction.create("human", "noreply15@eressea.de", "de")
|
2015-07-01 21:24:44 +02:00
|
|
|
local u = unit.create(f, r, 1)
|
|
|
|
local fid = f.id
|
|
|
|
u:add_item("money", u.number * 100)
|
2015-09-06 20:07:15 +02:00
|
|
|
local filename = "test.dat"
|
2015-07-01 21:24:44 +02:00
|
|
|
store = storage.create(filename, "wb")
|
|
|
|
assert_not_equal(store, nil)
|
|
|
|
store:write_unit(u)
|
|
|
|
store:close()
|
|
|
|
eressea.free_game()
|
|
|
|
-- recreate world:
|
|
|
|
r = region.create(0, 0, "plain")
|
2017-03-17 22:31:59 +01:00
|
|
|
f = faction.create("human", "noreply16@eressea.de", "de")
|
2015-07-01 21:24:44 +02:00
|
|
|
f.id = fid
|
|
|
|
store = storage.create(filename, "rb")
|
|
|
|
assert_not_nil(store)
|
|
|
|
u = store:read_unit()
|
2015-11-04 09:48:51 +01:00
|
|
|
assert_not_nil(u)
|
|
|
|
assert_equal(f, u.faction)
|
2015-11-04 10:38:12 +01:00
|
|
|
assert_equal(nil, u.region)
|
2015-07-01 21:24:44 +02:00
|
|
|
store:close()
|
2015-09-11 09:34:38 +02:00
|
|
|
os.remove(filename)
|
2015-07-01 21:24:44 +02:00
|
|
|
assert_not_nil(u)
|
|
|
|
assert_equal(u:get_item("money"), u.number * 100)
|
2015-10-11 14:57:35 +02:00
|
|
|
os.remove(filename)
|
2015-07-01 21:24:44 +02:00
|
|
|
end
|