Skip to content

Commit

Permalink
Update tests for new feature macros
Browse files Browse the repository at this point in the history
  • Loading branch information
dominickpastore committed May 28, 2020
1 parent c4f8122 commit bb7fa41
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
25 changes: 17 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# You can put your build options here
-include config.mk

test: test_default test_strict test_links test_strict_links
test: test_default test_nonstrict test_lowmem test_nonstrict_lowmem test_permissiveprims test_permissivestrs test_primkeys
test_default: test/tests.c jsmn.h
$(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict: test/tests.c jsmn.h
$(CC) -DJSMN_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
test_nonstrict: test/tests.c jsmn.h
$(CC) -DJSMN_NON_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_links: test/tests.c jsmn.h
$(CC) -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
test_lowmem: test/tests.c jsmn.h
$(CC) -DJSMN_LOW_MEMORY=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict_links: test/tests.c jsmn.h
$(CC) -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
test_nonstrict_lowmem: test/tests.c jsmn.h
$(CC) -DJSMN_NON_STRICT=1 -DJSMN_LOW_MEMORY=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_permissiveprims: test/tests.c jsmn.h
$(CC) -DJSMN_PERMISSIVE_PRIMITIVES=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_permissivestrs: test/tests.c jsmn.h
$(CC) -DJSMN_PERMISSIVE_STRINGS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_primkeys: test/tests.c jsmn.h
$(CC) -DJSMN_PRIMITIVE_KEYS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@

simple_example: example/simple.c jsmn.h
Expand All @@ -31,7 +40,7 @@ clean:
rm -f *.o example/*.o
rm -f simple_example
rm -f jsondump
rm -f test/test_default test/test_strict test/test_links test/test_strict_links
rm -f test/test_default test/test_nonstrict test/test_lowmem test/test_nonstrict_lowmem test/test_permissiveprims test/test_permissivestrs test/test_primkeys

.PHONY: clean test

6 changes: 3 additions & 3 deletions jsmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ extern "C" {
#endif

#ifdef JSMN_NON_STRICT
#ifndef
#ifndef JSMN_PERMISSIVE_PRIMITIVES
#define JSMN_PERMISSIVE_PRIMITIVES
#endif
#ifndef
#ifndef JSMN_PERMISSIVE_STRINGS
#define JSMN_PERMISSIVE_STRINGS
#endif
#ifndef
#ifndef JSMN_PRIMITIVE_KEYS
#define JSMN_PRIMITIVE_KEYS
#endif
#endif
Expand Down
27 changes: 14 additions & 13 deletions test/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ int test_object(void) {
JSMN_STRING, "a", 1, JSMN_PRIMITIVE, "0", JSMN_STRING, "b", 1,
JSMN_STRING, "c", 0));

#ifdef JSMN_STRICT
#ifndef JSMN_NON_STRICT
check(parse("{\"a\"\n0}", JSMN_ERROR_INVAL, 3));
check(parse("{\"a\", 0}", JSMN_ERROR_INVAL, 3));
check(parse("{\"a\": {2}}", JSMN_ERROR_INVAL, 4));
check(parse("{\"a\": {2: 3}}", JSMN_ERROR_INVAL, 5));
check(parse("{\"a\": {\"a\": 2 3}}", JSMN_ERROR_INVAL, 6));
check(parse("{\"a\"}", JSMN_ERROR_INVAL, 2));
check(parse("{\"a\": 1, \"b\"}", JSMN_ERROR_INVAL, 4));
Expand Down Expand Up @@ -88,7 +87,7 @@ int test_primitive(void) {
check(parse("{\"floatVar\" : 12e+6}", 3, 3, JSMN_OBJECT, -1, -1, 1,
JSMN_STRING, "floatVar", 1, JSMN_PRIMITIVE, "12e+6"));

#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
check(parse("{\"boolVar\" : tru }", JSMN_ERROR_INVAL, 3));
check(parse("{\"boolVar\" : falsee }", JSMN_ERROR_INVAL, 3));
check(parse("{\"nullVar\" : nulm }", JSMN_ERROR_INVAL, 3));
Expand Down Expand Up @@ -145,7 +144,7 @@ int test_string(void) {
check(parse("{\"a\":\"str\xc2\xa9\"}", 3, 3, JSMN_OBJECT, -1, -1, 1,
JSMN_STRING, "a", 1, JSMN_STRING, "str\xc2\xa9", 0));

#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_STRINGS
check(parse("{\"a\":\"str\nstr\"}", JSMN_ERROR_INVAL, 3));
check(parse("{\"a\":\"str\\uFFGFstr\"}", JSMN_ERROR_INVAL, 3));
check(parse("{\"a\":\"str\\u@FfF\"}", JSMN_ERROR_INVAL, 3));
Expand Down Expand Up @@ -236,20 +235,22 @@ int test_array_nomem(void) {
}

int test_unquoted_keys(void) {
#ifndef JSMN_STRICT
int r;
jsmn_parser p;
jsmntok_t tok[10];
const char *js;

jsmn_init(&p);
js = "{key1: \"value\", key2 : 123}";
js = "{123: \"value\", 456 : null}";

r = jsmn_parse(&p, js, strlen(js), tok, 10);
#ifdef JSMN_PRIMITIVE_KEYS
check(r >= 0);
check(tokeq(js, tok, 5, JSMN_OBJECT, -1, -1, 2, JSMN_PRIMITIVE, "key1",
JSMN_STRING, "value", 0, JSMN_PRIMITIVE, "key2",
JSMN_PRIMITIVE, "123"));
check(tokeq(js, tok, 5, JSMN_OBJECT, -1, -1, 2, JSMN_PRIMITIVE, "123",
JSMN_STRING, "value", 0, JSMN_PRIMITIVE, "456",
JSMN_PRIMITIVE, "null"));
#else
check(r == JSMN_ERROR_INVAL);
#endif
return 0;
}
Expand Down Expand Up @@ -349,7 +350,7 @@ int test_unenclosed(void) {
check(parse(js, 1, 1, JSMN_PRIMITIVE, "false"));

js = "0garbage";
#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
check(parse(js, JSMN_ERROR_INVAL, 1));
#else
check(parse(js, 1, 1, JSMN_PRIMITIVE, "0garbage"));
Expand All @@ -376,7 +377,7 @@ int test_unenclosed(void) {
js = " 1234 ";
check(parse(js, 1, 1, JSMN_PRIMITIVE, "1234"));

#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
js = "fal";
check(parse(js, JSMN_ERROR_PART, 1));

Expand Down Expand Up @@ -441,7 +442,7 @@ int test_object_key(void) {
js = "{\"key\": 1}";
check(parse(js, 3, 3, JSMN_OBJECT, 0, 10, 1, JSMN_STRING, "key", 1,
JSMN_PRIMITIVE, "1"));
#ifdef JSMN_STRICT
#ifndef JSMN_PRIMITIVE_KEYS
js = "{true: 1}";
check(parse(js, JSMN_ERROR_INVAL, 3));
js = "{1: 1}";
Expand Down Expand Up @@ -493,7 +494,7 @@ int main(void) {
test(test_input_length, "test strings that are not null-terminated");
test(test_issue_22, "test issue #22");
test(test_count, "test tokens count estimation");
test(test_unenclosed, "test for non-strict mode");
test(test_unenclosed, "test for non-strict mode");
test(test_unmatched_brackets, "test for unmatched brackets");
test(test_object_key, "test for key type");
test(test_multiple_objects, "test parsing multiple items at once");
Expand Down

0 comments on commit bb7fa41

Please sign in to comment.