Skip to content

Commit

Permalink
cleanup and maximize test coverage of module/property
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelprograms committed Feb 3, 2025
1 parent c1f88a5 commit 04f004c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
38 changes: 11 additions & 27 deletions lib/std/module/property.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
private mapping __Properties = ([ ]);
mapping __Properties = ([ ]);

mapping query_properties () {
private void initialize_properties () {
if (!mapp(__Properties)) {
__Properties = ([ ]);
}
}

mapping query_properties () {
initialize_properties();
return __Properties;
}
mixed query_property (string key) {
if (!stringp(key) || key == "") {
error("Bad argument 1 to property->query_property");
}
if (!mapp(__Properties)) {
__Properties = ([ ]);
}

initialize_properties();
return __Properties[key];
}

Expand All @@ -28,10 +28,7 @@ mixed set_property (string key, mixed value) {
if (arrayp(value) || mapp(value)) {
error("Bad argument 2 to property->set_property");
}
if (!mapp(__Properties)) {
__Properties = ([ ]);
}

initialize_properties();
return __Properties[key] = value;
}
mixed add_property (string key, mixed value) {
Expand All @@ -44,10 +41,7 @@ mixed add_property (string key, mixed value) {
if (arrayp(value) || mapp(value)) {
error("Bad argument 2 to property->add_property");
}
if (!mapp(__Properties)) {
__Properties = ([ ]);
}

initialize_properties();
if (__Properties[key]) {
__Properties[key] += value;
} else {
Expand All @@ -59,26 +53,16 @@ mapping set_properties (mapping properties) {
if (!mapp(properties) || !sizeof(properties)) {
error("Bad argument 1 to property->set_properties");
}
if (!mapp(__Properties)) {
__Properties = ([ ]);
}

if (sizeof(__Properties)) {
__Properties += copy(properties);
} else {
__Properties = copy(properties);
}
initialize_properties();
__Properties += copy(properties);
return __Properties;
}

int remove_property (string key) {
if (!stringp(key) || key == "") {
error("Bad argument 1 to property->remove_property");
}
if (!mapp(__Properties)) {
__Properties = ([ ]);
}

initialize_properties();
if (!undefinedp(__Properties[key])) {
map_delete(__Properties, key);
}
Expand Down
26 changes: 26 additions & 0 deletions lib/std/module/property.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ string *test_order () {
return ({ "test_property_single", "test_properties_multiple", });
}

void test_null_properties () {
expect("null achievements are initialized", (: ({
assert_equal(testOb->query_properties(), ([ ])),
store_variable("__Properties", UNDEFINED, testOb),
assert_equal(testOb->query_properties(), ([ ])),
}) :));
}

void test_property_single () {
expect("add, set, remove, and query property are handled", (: ({
assert_equal(testOb->query_property("test_key"), UNDEFINED),
Expand All @@ -31,6 +39,23 @@ void test_property_single () {
assert_equal(testOb->remove_property("test_key2"), 1),
assert_equal(testOb->query_property("test_key2"), UNDEFINED),
}) :));
expect("add, set, remove, and query property handle bad inputs", (: ({
assert_catch((: testOb->query_property(UNDEFINED) :), "*Bad argument 1 to property->query_property\n"),

assert_catch((: testOb->set_property(UNDEFINED, UNDEFINED) :), "*Bad argument 1 to property->set_property\n"),
assert_catch((: testOb->set_property("key", UNDEFINED) :), "*Bad argument 2 to property->set_property\n"),
assert_catch((: testOb->set_property("key", ({ })) :), "*Bad argument 2 to property->set_property\n"),
assert_catch((: testOb->set_property("key", ([ ])) :), "*Bad argument 2 to property->set_property\n"),

assert_catch((: testOb->add_property(UNDEFINED, UNDEFINED) :), "*Bad argument 1 to property->add_property\n"),
assert_catch((: testOb->add_property("key", UNDEFINED) :), "*Bad argument 2 to property->add_property\n"),
assert_catch((: testOb->add_property("key", ({ })) :), "*Bad argument 2 to property->add_property\n"),
assert_catch((: testOb->add_property("key", ([ ])) :), "*Bad argument 2 to property->add_property\n"),

assert_catch((: testOb->set_properties(UNDEFINED) :), "*Bad argument 1 to property->set_properties\n"),

assert_catch((: testOb->remove_property(UNDEFINED) :), "*Bad argument 1 to property->remove_property\n"),
}) :));
}

private mapping m1 = ([ "test_key1": "test_value" ]);
Expand All @@ -55,5 +80,6 @@ void test_properties_multiple () {
assert_equal(testOb->set_properties(m1), identify(m1)),
assert_equal(testOb->set_properties(m2), identify(m1+m2)),
assert_equal(testOb->remove_properties(), ([ ])),
assert_equal(testOb->query_properties(), ([ ])),
}) :));
}

0 comments on commit 04f004c

Please sign in to comment.