Skip to content

Commit

Permalink
cleanup and expand test coverage of living/biography
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelprograms committed Feb 5, 2025
1 parent b7fef0c commit c08c2e3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
55 changes: 35 additions & 20 deletions lib/std/living/biography.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
// @this_object /std/living.c

private int __Experience = 0, __TotalExperience = 0;
private int __Victory = 0, __VictoryLevel;
private mixed *__Defeat = ({ });
private int __Defeated = 0;
int __Experience = 0, __TotalExperience = 0;
int __Victory = 0, __VictoryLevel = 0;
int __Defeated = 0;
mixed *__Defeat = ({ });

int query_experience () {
private void initialize_experience () {
if (undefinedp(__Experience)) {
__Experience = 0;
}
return __Experience;
}
int query_total_experience () {
if (undefinedp(__TotalExperience)) {
__TotalExperience = 0;
}
}
private void initialize_victory () {
if (undefinedp(__Victory)) {
__Victory = 0;
}
if (undefinedp(__VictoryLevel)) {
__VictoryLevel = 0;
}
}
private void initialize_defeat () {
if (undefinedp(__Defeated)) {
__Defeated = 0;
}
if (!arrayp(__Defeat)) {
__Defeat = ({ });
}
}

int query_experience () {
initialize_experience();
return __Experience;
}
int query_total_experience () {
initialize_experience();
return __TotalExperience;
}
void add_experience (int exp) {
if (!intp(exp)) {
error("Bad argument 1 to body->add_experience");
if (undefinedp(exp) || !intp(exp)) {
error("Bad argument 1 to biography->add_experience");
}
__Experience = __Experience + exp;
// @TODO if (__Experience > ExpMax) __Experience = ExpMax
}
void spend_experience (int exp) {
if (!intp(exp) || __Experience < exp || exp < 1) {
error("Bad argument 1 to body->spend_experience");
error("Bad argument 1 to biography->spend_experience");
}
__Experience = __Experience - exp;
__TotalExperience = __TotalExperience + exp;
}

int query_victory () {
if (undefinedp(__Victory)) {
__Victory = 0;
}
initialize_victory();
return __Victory;
}
int query_victory_average () {
Expand All @@ -45,9 +64,7 @@ int query_victory_average () {
return __VictoryLevel / __Victory;
}
mixed *query_defeat () {
if (!arrayp(__Defeat)) {
__Defeat = ({ });
}
initialize_defeat();
return __Defeat;
}
int query_defeated () {
Expand Down Expand Up @@ -79,9 +96,7 @@ void handle_victory (object source) {
void handle_defeat (object source) {
object env = environment(), corpse;

if (!arrayp(__Defeat)) {
__Defeat = ({ });
}
initialize_defeat();
if (__Defeated) {
return;
}
Expand Down
36 changes: 36 additions & 0 deletions lib/std/living/biography.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ int is_living () { return __MockLiving; }
string query_cap_name () { return "Biography Test"; }

void test_experience () {
expect("null experience is initialized", (: ({
assert_equal(testOb->query_experience(), 0),
assert_equal(testOb->query_total_experience(), 0),
store_variable("__Experience", UNDEFINED, testOb),
store_variable("__TotalExperience", UNDEFINED, testOb),
assert_equal(testOb->query_experience(), 0),
assert_equal(testOb->query_total_experience(), 0),
}) :));

expect("handles adding and spending experience", (: ({
assert_equal(testOb->query_experience(), 0),
assert_equal(testOb->query_total_experience(), 0),
Expand All @@ -33,6 +42,10 @@ void test_experience () {
assert_equal(testOb->spend_experience(2000), 0),
assert_equal(testOb->query_experience(), 6123),
assert_equal(testOb->query_total_experience(), 3000),

assert_catch((: testOb->add_experience(UNDEFINED) :), "*Bad argument 1 to biography->add_experience\n"),
assert_catch((: testOb->spend_experience(UNDEFINED) :), "*Bad argument 1 to biography->spend_experience\n"),
assert_catch((: testOb->spend_experience(testOb->query_experience() * 2) :), "*Bad argument 1 to biography->spend_experience\n"),
}) :));
}

Expand All @@ -41,6 +54,15 @@ void test_handle_victory () {
__MockLiving = 1;
__MockLevel = 1;

expect("null victory is initialized", (: ({
assert_equal(testOb->query_victory(), 0),
assert_equal(testOb->query_victory_average(), 0),
store_variable("__Victory", UNDEFINED, testOb),
store_variable("__VictoryLevel", UNDEFINED, testOb),
assert_equal(testOb->query_victory(), 0),
assert_equal(testOb->query_victory_average(), 0),
}) :));

expect("handle_victory behaves", (: ({
assert_equal(testOb->query_experience(), 0),
assert_equal(testOb->query_victory(), 0),
Expand All @@ -65,6 +87,20 @@ void test_handle_defeat () {
object r = new(STD_ROOM);
object mockCharacter = new("/std/character.mock.c");

expect("null defeat is initialized", (: ({
assert_equal(testOb->query_defeat(), ({ })),
assert_equal(testOb->query_defeated(), 0),
store_variable("__Defeat", UNDEFINED, testOb),
store_variable("__Defeated", UNDEFINED, testOb),
assert_equal(testOb->query_defeat(), ({ })),
assert_equal(testOb->query_defeated(), 0),
}) :));
expect("defeat can be set", (: ({
testOb->set_defeated(1),
assert_equal(testOb->query_defeated(), 1),
}) :));

// @TODO: re-visit this for test coverage
// setup test object
if (testOb) destruct(testOb);
testOb = new(STD_LIVING); // need biography's parent inherit living for handle_move
Expand Down

0 comments on commit c08c2e3

Please sign in to comment.