Simple tests for persistent attributes and a fix.

This commit is contained in:
Enno Rehling 2011-02-19 06:13:24 -08:00
parent 204924c35a
commit d033e7156c
4 changed files with 40 additions and 7 deletions

30
scripts/tests/bson.lua Normal file
View File

@ -0,0 +1,30 @@
require "lunit"
module("tests.bson", package.seeall, lunit.testcase)
function setup()
free_game()
end
function test_bson_create()
local a = attrib.create("global", 12)
assert_not_equal(nil, a)
for a in attrib.get("global") do
assert_equal(a.data, 12)
end
end
function test_bson_readwrite()
local r = region.create(0, 0, "mountain")
attrib.create(r, 42)
write_game("test_read_write.dat")
free_game()
r = get_region(0, 0)
assert_equal(nil, r)
read_game("test_read_write.dat")
r = get_region(0, 0)
assert_not_equal(nil, r)
for a in attrib.get(r) do
assert_equal(a.data, 42)
end
end

View File

@ -25,7 +25,7 @@ local function two_factions()
return f1, f2
end
module( "common", package.seeall, lunit.testcase )
module("tests.common", package.seeall, lunit.testcase)
function setup()
free_game()
@ -760,7 +760,7 @@ function test_walk_and_carry_the_cart()
assert_equal(1, u.region.x)
end
module( "report", package.seeall, lunit.testcase )
module("tests.report", package.seeall, lunit.testcase)
function setup()
free_game()
@ -820,7 +820,7 @@ function test_coordinates_noname_plane()
assert_true(find_in_report(f, r.name .. " %(0,0%), Berg"))
end
module( "parser", package.seeall, lunit.testcase )
module("tests.parser", package.seeall, lunit.testcase)
function setup()
free_game()
@ -890,6 +890,9 @@ function test_bson()
local r = region.create(0, 0, "mountain")
local f = faction.create("noreply@eressea.de", "human", "de")
local u = unit.create(f, r, 1)
assert_not_equal(nil, u)
assert_not_equal(nil, r)
assert_not_equal(nil, f)
attrib.create(r, 1)
assert_equal(attrib.get(r)().data, 1)
attrib.create(u, 3)

View File

@ -1,6 +1,6 @@
require "lunit"
module( "spells", package.seeall, lunit.testcase )
module("tests.spells", package.seeall, lunit.testcase)
function setup()
free_game()

View File

@ -300,13 +300,13 @@ get_attribs(lua_State * L, int idx) {
if (tolua_isusertype(L, idx, TOLUA_CAST "unit", 0, &tolua_err)) {
unit * u = (unit *)tolua_tousertype(L, idx, 0);
ap = &u->attribs;
if (u) ap = &u->attribs;
} else if (tolua_isusertype(L, idx, TOLUA_CAST "region", 0, &tolua_err)) {
region * r = (region *)tolua_tousertype(L, idx, 0);
ap = &r->attribs;
if (r) ap = &r->attribs;
} else if (tolua_isusertype(L, idx, TOLUA_CAST "faction", 0, &tolua_err)) {
faction * f = (faction *)tolua_tousertype(L, idx, 0);
ap = &f->attribs;
if (f) ap = &f->attribs;
} else if (lua_isstring(L, idx)) {
const char * str = tolua_tostring(L, idx, NULL);
if (str && strcmp(str, "global")==0) {