handle empty world

and add tests for it
This commit is contained in:
Enno Rehling 2014-03-16 11:17:34 -07:00
parent e350b8a146
commit eba61c1cda
2 changed files with 19 additions and 4 deletions

View File

@ -57,7 +57,7 @@ int json_import(struct stream * out) {
int json_export(stream * out, unsigned int flags) { int json_export(stream * out, unsigned int flags) {
cJSON *json, *root = cJSON_CreateObject(); cJSON *json, *root = cJSON_CreateObject();
assert(out && out->api); assert(out && out->api);
if (flags & EXPORT_REGIONS) { if (regions && (flags & EXPORT_REGIONS)) {
region * r; region * r;
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject()); cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
for (r = regions; r; r = r->next) { 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); cJSON_AddStringToObject(data, "type", r->terrain->_name);
} }
} }
if (flags & EXPORT_FACTIONS) { if (factions && (flags & EXPORT_FACTIONS)) {
faction *f; faction *f;
cJSON_AddItemToObject(root, "factions", json = cJSON_CreateObject()); cJSON_AddItemToObject(root, "factions", json = cJSON_CreateObject());
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {

View File

@ -5,7 +5,7 @@
#include "json.h" #include "json.h"
#include "bind_eressea.h" #include "bind_eressea.h"
static void test_export(CuTest * tc) { static void test_export_no_regions(CuTest * tc) {
char buf[1024]; char buf[1024];
stream out = { 0 }; stream out = { 0 };
int err; int err;
@ -19,8 +19,23 @@ static void test_export(CuTest * tc) {
mstream_done(&out); 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 *get_json_suite(void) {
CuSuite *suite = CuSuiteNew(); 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; return suite;
} }