Skip to content

Commit

Permalink
cleanup and maximize test coverage of module/currency
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelprograms committed Feb 4, 2025
1 parent 04f004c commit 8911b7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/std/module/currency.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#define VALID_CURRENCIES ({ "copper" })

private mapping __Currency;
mapping __Currency;

string *query_currencies () {
private void initialize_currencies () {
if (!mapp(__Currency)) {
__Currency = ([ ]);
}
}

string *query_currencies () {
initialize_currencies();
return keys(filter(__Currency, (: $2 > 0 :)));
}

int query_currency (string type) {
if (!mapp(__Currency)) {
__Currency = ([ ]);
}
initialize_currencies();
if (!stringp(type)) {
error("Bad argument 1 to currency->query_currency");
}
Expand All @@ -29,9 +31,7 @@ int query_currency (string type) {
int add_currency (string type, int amount) {
int result = 0;

if (!mapp(__Currency)) {
__Currency = ([ ]);
}
initialize_currencies();

if (!stringp(type) || member_array(type, VALID_CURRENCIES) == -1) {
error("Bad argument 1 to currency->add_currency");
Expand Down
14 changes: 14 additions & 0 deletions lib/std/module/currency.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ inherit M_TEST;
* @var {"/std/module/currency"} testOb
*/

void test_null_currencies () {
expect("null achievements are initialized", (: ({
assert_equal(testOb->query_currencies(), ({ })),
store_variable("__Currency", UNDEFINED, testOb),
assert_equal(testOb->query_currencies(), ({ })),
}) :));
}

void test_currencies () {
expect("query_currency returns -1 for invalid currency", (: ({
assert_equal(testOb->query_currency("unknown"), -1),
Expand All @@ -29,4 +37,10 @@ void test_currencies () {
assert_equal(testOb->query_currencies(), ({ "copper" })), // some remains
assert_equal(testOb->query_currency("copper"), 5),
}) :));
expect("currency handles bad inputs", (: ({
assert_catch((: testOb->query_currency(UNDEFINED) :), "*Bad argument 1 to currency->query_currency\n"),

assert_catch((: testOb->add_currency(UNDEFINED, UNDEFINED) :), "*Bad argument 1 to currency->add_currency\n"),
assert_catch((: testOb->add_currency("copper", UNDEFINED) :), "*Bad argument 2 to currency->add_currency\n"),
}) :));
}

0 comments on commit 8911b7e

Please sign in to comment.