2014-03-13 15:33:44 +01:00
|
|
|
#include <platform.h>
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <stream.h>
|
|
|
|
#include <memstream.h>
|
2014-03-16 22:08:37 +01:00
|
|
|
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/terrain.h>
|
|
|
|
|
2014-03-16 22:34:05 +01:00
|
|
|
#include "json.h"
|
|
|
|
#include "tests.h"
|
2014-03-16 22:08:37 +01:00
|
|
|
|
2014-03-16 22:34:05 +01:00
|
|
|
#include <cJSON.h>
|
2014-03-16 22:08:37 +01:00
|
|
|
#include <stdlib.h>
|
2014-03-16 22:34:05 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2014-03-16 22:08:37 +01:00
|
|
|
|
|
|
|
static char *strip(char *str) {
|
|
|
|
char *s = str, *b = str, *e = str;
|
|
|
|
// b is where text begins, e where it ends, s where we insert it.
|
|
|
|
for (; *b && isspace(*b); ++b) {};
|
|
|
|
for (e = b; *e && !isspace(*e); ++e) {};
|
|
|
|
while (*b) {
|
2015-01-30 20:37:14 +01:00
|
|
|
if (s != b) {
|
2014-06-27 06:48:01 +02:00
|
|
|
memcpy(s, b, e - b);
|
|
|
|
}
|
2014-03-16 22:08:37 +01:00
|
|
|
s += e - b;
|
|
|
|
for (b = e; *b && isspace(*b); ++b) {};
|
|
|
|
for (e = b; *e && !isspace(*e); ++e) {};
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
return str;
|
|
|
|
}
|
2014-03-13 15:33:44 +01:00
|
|
|
|
2014-03-16 19:17:34 +01:00
|
|
|
static void test_export_no_regions(CuTest * tc) {
|
2014-03-13 15:33:44 +01:00
|
|
|
char buf[1024];
|
|
|
|
stream out = { 0 };
|
|
|
|
int err;
|
2014-11-04 07:57:29 +01:00
|
|
|
size_t len;
|
2014-03-16 22:08:37 +01:00
|
|
|
|
|
|
|
mstream_init(&out);
|
|
|
|
err = json_export(&out, EXPORT_REGIONS);
|
|
|
|
CuAssertIntEquals(tc, 0, err);
|
|
|
|
out.api->rewind(out.handle);
|
2014-11-04 07:57:29 +01:00
|
|
|
len = out.api->read(out.handle, buf, sizeof(buf));
|
|
|
|
buf[len] = '\0';
|
2014-03-16 22:08:37 +01:00
|
|
|
CuAssertStrEquals(tc, "{}", strip(buf));
|
|
|
|
mstream_done(&out);
|
2014-12-30 23:49:50 +01:00
|
|
|
test_cleanup();
|
2014-03-16 22:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cJSON *export_a_region(CuTest * tc, const struct terrain_type *terrain, region **_r) {
|
|
|
|
char buf[1024];
|
|
|
|
stream out = { 0 };
|
|
|
|
int err;
|
|
|
|
region *r;
|
2015-10-15 09:06:44 +02:00
|
|
|
cJSON *json, *attr, *result, *regs;
|
2014-03-16 22:08:37 +01:00
|
|
|
|
|
|
|
r = test_create_region(0, 0, terrain);
|
|
|
|
|
2014-03-13 15:33:44 +01:00
|
|
|
mstream_init(&out);
|
2014-03-15 06:30:07 +01:00
|
|
|
err = json_export(&out, EXPORT_REGIONS);
|
2014-03-13 15:33:44 +01:00
|
|
|
CuAssertIntEquals(tc, 0, err);
|
|
|
|
out.api->rewind(out.handle);
|
2014-03-14 07:59:26 +01:00
|
|
|
out.api->read(out.handle, buf, sizeof(buf));
|
2014-03-13 15:33:44 +01:00
|
|
|
mstream_done(&out);
|
2014-03-16 22:08:37 +01:00
|
|
|
|
|
|
|
json = cJSON_Parse(buf);
|
|
|
|
CuAssertPtrNotNull(tc, json);
|
|
|
|
CuAssertIntEquals(tc, cJSON_Object, json->type);
|
2014-03-22 17:46:08 +01:00
|
|
|
CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "factions"));
|
|
|
|
CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "units"));
|
2015-10-15 09:06:44 +02:00
|
|
|
CuAssertPtrNotNull(tc, regs = cJSON_GetObjectItem(json, "regions"));
|
|
|
|
CuAssertIntEquals(tc, cJSON_Object, regs->type);
|
|
|
|
result = regs->child;
|
|
|
|
regs->child = 0;
|
|
|
|
cJSON_Delete(json);
|
|
|
|
CuAssertIntEquals(tc, r->uid, atoi(result->string));
|
|
|
|
CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(result, "x"));
|
2014-03-16 22:08:37 +01:00
|
|
|
CuAssertIntEquals(tc, r->x, attr->valueint);
|
2015-10-15 09:06:44 +02:00
|
|
|
CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(result, "y"));
|
2014-03-16 22:08:37 +01:00
|
|
|
CuAssertIntEquals(tc, r->y, attr->valueint);
|
2015-10-15 09:06:44 +02:00
|
|
|
CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(result, "type"));
|
2014-03-16 22:08:37 +01:00
|
|
|
CuAssertStrEquals(tc, terrain->_name, attr->valuestring);
|
|
|
|
|
|
|
|
if (_r) *_r = r;
|
2015-10-15 09:06:44 +02:00
|
|
|
return result;
|
2014-03-16 22:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_export_land_region(CuTest * tc) {
|
|
|
|
region *r;
|
|
|
|
struct terrain_type *terrain;
|
|
|
|
cJSON *json, *attr;
|
|
|
|
test_cleanup();
|
|
|
|
terrain = test_create_terrain("plain", LAND_REGION);
|
|
|
|
json = export_a_region(tc, terrain, &r);
|
|
|
|
CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(json, "name"));
|
|
|
|
CuAssertStrEquals(tc, r->land->name, attr->valuestring);
|
|
|
|
cJSON_Delete(json);
|
2014-12-30 23:49:50 +01:00
|
|
|
test_cleanup();
|
2014-03-13 15:33:44 +01:00
|
|
|
}
|
2014-03-16 22:08:37 +01:00
|
|
|
|
|
|
|
static void test_export_ocean_region(CuTest * tc) {
|
|
|
|
struct terrain_type *terrain;
|
|
|
|
cJSON *json;
|
|
|
|
test_cleanup();
|
|
|
|
terrain = test_create_terrain("ocean", SEA_REGION);
|
|
|
|
json = export_a_region(tc, terrain, 0);
|
|
|
|
CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "name"));
|
|
|
|
cJSON_Delete(json);
|
2014-12-30 23:49:50 +01:00
|
|
|
test_cleanup();
|
2014-03-16 22:08:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-16 19:17:34 +01:00
|
|
|
static void test_export_no_factions(CuTest * tc) {
|
|
|
|
char buf[1024];
|
|
|
|
stream out = { 0 };
|
|
|
|
int err;
|
2014-11-04 07:50:33 +01:00
|
|
|
size_t len;
|
2014-03-16 19:17:34 +01:00
|
|
|
|
|
|
|
mstream_init(&out);
|
|
|
|
err = json_export(&out, EXPORT_FACTIONS);
|
|
|
|
CuAssertIntEquals(tc, 0, err);
|
|
|
|
out.api->rewind(out.handle);
|
2014-11-04 07:50:33 +01:00
|
|
|
len = out.api->read(out.handle, buf, sizeof(buf));
|
2015-01-30 20:37:14 +01:00
|
|
|
buf[len] = 0;
|
2014-03-16 22:08:37 +01:00
|
|
|
CuAssertStrEquals(tc, "{}", strip(buf));
|
2014-03-16 19:17:34 +01:00
|
|
|
mstream_done(&out);
|
2014-12-30 23:49:50 +01:00
|
|
|
test_cleanup();
|
2014-03-16 19:17:34 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 06:30:07 +01:00
|
|
|
CuSuite *get_json_suite(void) {
|
2014-03-13 15:33:44 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
2014-03-16 19:17:34 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_export_no_regions);
|
2014-03-16 22:08:37 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_export_ocean_region);
|
|
|
|
SUITE_ADD_TEST(suite, test_export_land_region);
|
2014-03-16 19:17:34 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_export_no_factions);
|
2014-03-13 15:33:44 +01:00
|
|
|
return suite;
|
|
|
|
}
|