Add unit tests that catch invalid 'screen' and 'widget' JSON data.

This commit is contained in:
Pim van Pelt
2017-11-26 16:01:08 +01:00
parent f9c4520b60
commit 83b0d346ae
6 changed files with 64 additions and 39 deletions

View File

@ -0,0 +1,27 @@
{
"x": 0,
"y": 0,
"w": 320,
"h": 240,
"widgets": [
{
"name": "one",
"x": 16,
"y": 16,
"w": 48,
"h": 48,
"label": "One",
"type": 0
},
{
"name": "two",
"x": 256,
"y": 16,
"w": 48,
"h": 48,
"label": "Two",
"type": 0
}
]
}

View File

@ -0,0 +1,8 @@
{
"x": 16,
"y": 16,
"w": 48,
"h": 48,
"label": "One",
"type": 0
}

View File

@ -72,6 +72,11 @@ int test_screen() {
struct widget_t *w = NULL;
uint16_t num_widgets;
LOG(LL_INFO, ("screen_create_from_file(data/TestScreen-invalid.json)"));
s = screen_create_from_file("data/TestScreen-invalid.json");
ASSERT(!s, "created screen from invalid date");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("screen_create_from_file(data/TestScreen.json)"));
s = screen_create_from_file("data/TestScreen.json");
ASSERT(s, "Could not create screen");

View File

@ -1,37 +1,11 @@
#include "test.h"
#include "widget.h"
/*
char *json;
void *h = NULL;
struct json_token key, val;
int idx;
json = json_fread("data/TestWidget.json");
if (!json) {
printf("Could not read json\n");
return -1;
}
// Traverse Object
while ((h = json_next_key(json, strlen(json), h, ".", &key, &val)) != NULL) {
printf("[%.*s] -> [%.*s]\n", key.len, key.ptr, val.len, val.ptr);
}
// Traverse Array
while ((h = json_next_elem(json, strlen(json), h, ".widgets", &idx, &val)) != NULL) {
printf("[%d]: [%.*s]\n", idx, val.len, val.ptr);
}
exit:
free(json);
return 0;
*/
static int test_widget_create_from_file(char *fn) {
static int test_widget_create_from_file(void) {
struct widget_t *w;
LOG(LL_INFO, ("Adding widget from %s", fn));
char *fn = "data/TestWidget.json";
LOG(LL_INFO, ("widget_create_from_file(%s)", fn));
w = widget_create_from_file(fn);
ASSERT(w, "widget_create_from_file()");
ASSERT(w->x == 16, "'x' field is invalid");
@ -39,10 +13,14 @@ static int test_widget_create_from_file(char *fn) {
ASSERT(w->w == 48, "'x' field is invalid");
ASSERT(w->h == 48, "'x' field is invalid");
fn = "data/TestWidget-invalid.json";
LOG(LL_INFO, ("widget_create_from_file(%s)", fn));
w = widget_create_from_file(fn);
ASSERT(!w, "invalid widget created");
return 0;
}
int test_widget() {
test_widget_create_from_file("data/TestWidget.json");
test_widget_create_from_file();
return 0;
}