Skip to content

Commit

Permalink
Add tests for single parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dominickpastore committed Jun 1, 2020
1 parent ee19c93 commit ba073ee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# You can put your build options here
-include config.mk

test: test_default test_nonstrict test_lowmem test_nonstrict_lowmem test_permissiveprims test_permissivestrs test_primkeys
test: test_default test_nonstrict test_lowmem test_nonstrict_lowmem test_permissiveprims test_permissivestrs test_primkeys test_single test_nonstrict_single
test_default: test/tests.c jsmn.h
$(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
Expand All @@ -23,6 +23,12 @@ test_permissivestrs: test/tests.c jsmn.h
test_primkeys: test/tests.c jsmn.h
$(CC) -DJSMN_PRIMITIVE_KEYS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_single: test/tests.c jsmn.h
$(CC) -DJSMN_SINGLE=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_nonstrict_single: test/tests.c jsmn.h
$(CC) -DJSMN_NON_STRICT=1 -DJSMN_SINGLE=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@

simple_example: example/simple.c jsmn.h
$(CC) $(LDFLAGS) $< -o $@
Expand All @@ -40,7 +46,7 @@ clean:
rm -f *.o example/*.o
rm -f simple_example
rm -f jsondump
rm -f test/test_default test/test_nonstrict test/test_lowmem test/test_nonstrict_lowmem test/test_permissiveprims test/test_permissivestrs test/test_primkeys
rm -f test/test_default test/test_nonstrict test/test_lowmem test/test_nonstrict_lowmem test/test_permissiveprims test/test_permissivestrs test/test_primkeys test/test_single test/test_nonstrict_single

.PHONY: clean test

49 changes: 49 additions & 0 deletions test/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,18 @@ int test_unenclosed(void) {
check(parse(js, JSMN_ERROR_PART, 1));

js = "1234\"0garbage\"";
#ifndef JSMN_SINGLE
check(parse(js, 2, 2, JSMN_PRIMITIVE, "1234", JSMN_STRING, "0garbage", 0));
#else
check(parse(js, 1, 1, JSMN_PRIMITIVE, "1234"));
#endif

js = "\"0garbage\"1234";
#ifndef JSMN_SINGLE
check(parse(js, 2, 2, JSMN_STRING, "0garbage", 0, JSMN_PRIMITIVE, "1234"));
#else
check(parse(js, 1, 1, JSMN_STRING, "0garbage", 0));
#endif

js = " 1234";
check(parse(js, 1, 1, JSMN_PRIMITIVE, "1234"));
Expand Down Expand Up @@ -419,15 +427,19 @@ int test_unenclosed(void) {

int test_unmatched_brackets(void) {
const char *js;
#ifndef JSMN_SINGLE
js = "\"key 1\": 1234}";
check(parse(js, JSMN_ERROR_INVAL, 2));
#endif
js = "{\"key 1\": 1234";
check(parse(js, JSMN_ERROR_PART, 3));

#ifndef JSMN_SINGLE
js = "{\"key 1\": 1234}}";
check(parse(js, JSMN_ERROR_INVAL, 3));
js = "\"key 1\"}: 1234";
check(parse(js, JSMN_ERROR_INVAL, 3));
#endif
js = "{\"key {1\": 1234}";
check(parse(js, 3, 3, JSMN_OBJECT, 0, 16, 1, JSMN_STRING, "key {1", 1,
JSMN_PRIMITIVE, "1234"));
Expand Down Expand Up @@ -458,25 +470,62 @@ int test_object_key(void) {
int test_multiple_objects(void) {
const char *js;
js = "true {\"def\": 123}";
#ifndef JSMN_SINGLE
check(parse(js, 4, 4, JSMN_PRIMITIVE, "true", JSMN_OBJECT, -1, -1, 1,
JSMN_STRING, "def", 1, JSMN_PRIMITIVE, "123"));
#else
check(parse(js, 1, 1, JSMN_PRIMITIVE, "true"));
#endif

js = "{\"def\": 123} true";
#ifndef JSMN_SINGLE
check(parse(js, 4, 4, JSMN_OBJECT, -1, -1, 1, JSMN_STRING, "def", 1,
JSMN_PRIMITIVE, "123", JSMN_PRIMITIVE, "true"));
#else
check(parse(js, 3, 3, JSMN_OBJECT, -1, -1, 1, JSMN_STRING, "def", 1,
JSMN_PRIMITIVE, "123"));
#endif

js = "true{\"def\": 123}";
#ifndef JSMN_SINGLE
check(parse(js, 4, 4, JSMN_PRIMITIVE, "true", JSMN_OBJECT, -1, -1, 1,
JSMN_STRING, "def", 1, JSMN_PRIMITIVE, "123"));
#else
check(parse(js, 1, 1, JSMN_PRIMITIVE, "true"));
#endif

js = "[true, \"abc\"]{\"def\": 123}";
#ifndef JSMN_SINGLE
check(parse(js, 6, 6, JSMN_ARRAY, -1, -1, 2, JSMN_PRIMITIVE, "true",
JSMN_STRING, "abc", 0, JSMN_OBJECT, -1, -1, 1,
JSMN_STRING, "def", 1, JSMN_PRIMITIVE, "123"));
#else
check(parse(js, 3, 3, JSMN_ARRAY, -1, -1, 2, JSMN_PRIMITIVE, "true",
JSMN_STRING, "abc", 0));
#endif

/* Issue #27 */
js = "{ \"name\" : \"Jack\", \"age\" : 27 } { \"name\" : \"Anna\", ";
#ifndef JSMN_SINGLE
check(parse(js, JSMN_ERROR_PART, 8));
#else
check(parse(js, 5, 5, JSMN_OBJECT, 0, 31, 2, JSMN_STRING, "name", 1,
JSMN_STRING, "Jack", 0, JSMN_STRING, "age", 1,
JSMN_PRIMITIVE, "27"));
#endif

#ifdef JSMN_SINGLE
#ifdef JSMN_PERMISSIVE_PRIMITIVES
js = "tru{\"def\": 123}";
check(parse(js, 1, 1, JSMN_PRIMITIVE, "tru"));

js = "truee{\"def\": 123}";
check(parse(js, 1, 1, JSMN_PRIMITIVE, "truee"));

js = "trux{\"def\": 123}";
check(parse(js, 1, 1, JSMN_PRIMITIVE, "trux"));
#endif
#endif
return 0;
}

Expand Down

0 comments on commit ba073ee

Please sign in to comment.