Skip to content

Commit

Permalink
Better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Jun 6, 2023
1 parent 9f254ac commit deaed8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ following routines:

Printing to a buffer: https://github.com/cesanta/str/blob/873b39dd14b074bf0779f5d06f5c5bfe3bcb416b/test/main.c#L174-L177

Print to the UART, JSON and base64 format: https://github.com/cesanta/str/blob/7e05d658a0f89b2bda67dda8cc1a912d0e2b7cba/test/main.ino#L11-L12
Print to the UART. Use JSON and base64 format: https://github.com/cesanta/str/blob/7e05d658a0f89b2bda67dda8cc1a912d0e2b7cba/test/main.ino#L11-L12

Parse JSON: https://github.com/cesanta/str/blob/813e08acf4e389690830bfb5ff525c9e79bdb362/test/main.c#L183-L190

Expand Down
15 changes: 9 additions & 6 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,16 @@ static void test_m(void) {

static void test_json(void) {
char buf[100];
const char *json = "{ \"a\": -42, \"b\": \"hi\", \"c\": true }";
int b = 0;
assert(json_get_long(json, (int) strlen(json), "$.a", 0) == -42);
assert(json_get_str(json, (int) strlen(json), "$.b", buf, sizeof(buf)) == 2);
assert(strcmp(buf, "hi") == 0);
assert(json_get_bool(json, (int) strlen(json), "$.c", &b) == 1);
const char *json = "{ \"a\": -42, \"b\": [ \"hi\\t\\u0020\", true, { } ] }";
int ofs, n, b = 0, len = (int) strlen(json);
assert(json_get_long(json, len, "$.a", 0) == -42);
assert(json_get_str(json, len, "$.b[0]", buf, sizeof(buf)) == 4);
assert(strcmp(buf, "hi\t ") == 0);
assert(json_get_bool(json, len, "$.b[1]", &b) == 1);
assert(b == 1);
assert(json_get(json, len, "$.c", &n) < 0);
assert((ofs = json_get(json, len, "$.b[2]", &n)) > 0 && n == 3 &&
json[ofs] == '{' && json[ofs + 2] == '}');
}

int main(void) {
Expand Down

0 comments on commit deaed8d

Please sign in to comment.