From eba61c1cda3c6b6bf9ea258b693c97a3dfb83499 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 16 Mar 2014 11:17:34 -0700 Subject: [PATCH] handle empty world and add tests for it --- src/json.c | 4 ++-- src/json_test.c | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/json.c b/src/json.c index 40ad20c8c..224341db2 100644 --- a/src/json.c +++ b/src/json.c @@ -57,7 +57,7 @@ int json_import(struct stream * out) { int json_export(stream * out, unsigned int flags) { cJSON *json, *root = cJSON_CreateObject(); assert(out && out->api); - if (flags & EXPORT_REGIONS) { + if (regions && (flags & EXPORT_REGIONS)) { region * r; cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject()); for (r = regions; r; r = r->next) { @@ -70,7 +70,7 @@ int json_export(stream * out, unsigned int flags) { cJSON_AddStringToObject(data, "type", r->terrain->_name); } } - if (flags & EXPORT_FACTIONS) { + if (factions && (flags & EXPORT_FACTIONS)) { faction *f; cJSON_AddItemToObject(root, "factions", json = cJSON_CreateObject()); for (f = factions; f; f = f->next) { diff --git a/src/json_test.c b/src/json_test.c index 6512151c5..5672e9c97 100644 --- a/src/json_test.c +++ b/src/json_test.c @@ -5,7 +5,7 @@ #include "json.h" #include "bind_eressea.h" -static void test_export(CuTest * tc) { +static void test_export_no_regions(CuTest * tc) { char buf[1024]; stream out = { 0 }; int err; @@ -19,8 +19,23 @@ static void test_export(CuTest * tc) { mstream_done(&out); } +static void test_export_no_factions(CuTest * tc) { + char buf[1024]; + stream out = { 0 }; + int err; + + mstream_init(&out); + err = json_export(&out, EXPORT_FACTIONS); + CuAssertIntEquals(tc, 0, err); + out.api->rewind(out.handle); + out.api->read(out.handle, buf, sizeof(buf)); + CuAssertStrEquals(tc, "{\n}\n", buf); + mstream_done(&out); +} + CuSuite *get_json_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_export); + SUITE_ADD_TEST(suite, test_export_no_regions); + SUITE_ADD_TEST(suite, test_export_no_factions); return suite; }