Enhance tests
authorPetri Lehtinen <petri@digip.org>
Wed, 16 Dec 2009 20:05:55 +0000 (22:05 +0200)
committerPetri Lehtinen <petri@digip.org>
Mon, 21 Dec 2009 10:50:49 +0000 (12:50 +0200)
* Now that JSON_SORT_KEYS is implemented, take it into use with the
  valid and valid-strip suites. This is to ensure that the tests
  remain valid even if the string hash function is changed in the
  future.

* Remove test_dump API test. Instead, implement the same tests more
  elegantly in the encoding-flags suite.

32 files changed:
test/.gitignore
test/suites/api/Makefile.am
test/suites/api/test_dump.c [deleted file]
test/suites/encoding-flags/array/input [new file with mode: 0644]
test/suites/encoding-flags/array/output [new file with mode: 0644]
test/suites/encoding-flags/compact-array/env [new file with mode: 0644]
test/suites/encoding-flags/compact-array/input [new file with mode: 0644]
test/suites/encoding-flags/compact-array/output [new file with mode: 0644]
test/suites/encoding-flags/compact-object/env [new file with mode: 0644]
test/suites/encoding-flags/compact-object/input [new file with mode: 0644]
test/suites/encoding-flags/compact-object/output [new file with mode: 0644]
test/suites/encoding-flags/ensure-ascii/env [new file with mode: 0644]
test/suites/encoding-flags/ensure-ascii/input [new file with mode: 0644]
test/suites/encoding-flags/ensure-ascii/output [new file with mode: 0644]
test/suites/encoding-flags/indent-array/env [new file with mode: 0644]
test/suites/encoding-flags/indent-array/input [new file with mode: 0644]
test/suites/encoding-flags/indent-array/output [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-array/env [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-array/input [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-array/output [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-object/env [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-object/input [new file with mode: 0644]
test/suites/encoding-flags/indent-compact-object/output [new file with mode: 0644]
test/suites/encoding-flags/indent-object/env [new file with mode: 0644]
test/suites/encoding-flags/indent-object/input [new file with mode: 0644]
test/suites/encoding-flags/indent-object/output [new file with mode: 0644]
test/suites/encoding-flags/object/input [new file with mode: 0644]
test/suites/encoding-flags/object/output [new file with mode: 0644]
test/suites/valid-strip/complex-array/output
test/suites/valid-strip/run
test/suites/valid/complex-array/output
test/suites/valid/run

index 06d11c0..2cc5d6d 100644 (file)
@@ -1,7 +1,6 @@
 logs
 bin/json_process
 suites/api/test_array
 logs
 bin/json_process
 suites/api/test_array
-suites/api/test_dump
 suites/api/test_load
 suites/api/test_number
 suites/api/test_object
 suites/api/test_load
 suites/api/test_number
 suites/api/test_object
index ac83ed1..a2829e1 100644 (file)
@@ -1,13 +1,11 @@
 check_PROGRAMS = \
        test_array \
 check_PROGRAMS = \
        test_array \
-       test_dump \
        test_load \
        test_simple \
        test_number \
        test_object
 
 test_array_SOURCES = test_array.c util.h
        test_load \
        test_simple \
        test_number \
        test_object
 
 test_array_SOURCES = test_array.c util.h
-test_dump_SOURCES = test_dump.c util.h
 test_load_SOURCES = test_load.c util.h
 test_simple_SOURCES = test_simple.c util.h
 test_number_SOURCES = test_number.c util.h
 test_load_SOURCES = test_load.c util.h
 test_simple_SOURCES = test_simple.c util.h
 test_number_SOURCES = test_number.c util.h
diff --git a/test/suites/api/test_dump.c b/test/suites/api/test_dump.c
deleted file mode 100644 (file)
index 548de06..0000000
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#include <jansson.h>
-#include <string.h>
-#include "util.h"
-
-static json_t *create_object()
-{
-    json_t *object;
-
-    object = json_object();
-    if(!object)
-        fail("unable to create an object");
-
-    if(json_object_set_new(object, "a", json_integer(1)) ||
-       json_object_set_new(object, "b", json_integer(2)))
-        fail("unable to set object values");
-
-    return object;
-}
-
-static json_t *create_array()
-{
-    json_t *array;
-
-    array = json_array();
-    if(!array)
-        fail("unable to create an array");
-
-    if(json_array_append_new(array, json_integer(1)) ||
-       json_array_append_new(array, json_integer(2)))
-        fail("unable to append array values");
-
-    return array;
-}
-
-
-#define NORMAL_OBJECT "{\"a\": 1, \"b\": 2}"
-#define NORMAL_ARRAY  "[1, 2]"
-
-static void test_normal()
-{
-    json_t *object;
-    json_t *array;
-    char *result;
-
-    object = create_object();
-    array = create_array();
-
-    result = json_dumps(object, 0);
-    if(strcmp(result, NORMAL_OBJECT) != 0)
-        fail("unexpected encoded object");
-    free(result);
-
-    result = json_dumps(array, 0);
-    if(strcmp(result, NORMAL_ARRAY) != 0)
-        fail("unexpected encoded array");
-    free(result);
-
-    json_decref(object);
-    json_decref(array);
-}
-
-
-#define INDENTED_OBJECT                         \
-    "{\n"                                       \
-    "    \"a\": 1,\n"                           \
-    "    \"b\": 2\n"                            \
-    "}"
-#define INDENTED_ARRAY                          \
-    "[\n"                                       \
-    "    1,\n"                                  \
-    "    2\n"                                   \
-    "]"
-
-static void test_indent()
-{
-    json_t *object;
-    json_t *array;
-    char *result;
-
-    object = create_object();
-    array = create_array();
-
-    result = json_dumps(object, JSON_INDENT(4));
-    if(strcmp(result, INDENTED_OBJECT) != 0)
-        fail("unexpected encoded object");
-    free(result);
-
-    result = json_dumps(array, JSON_INDENT(4));
-    if(strcmp(result, INDENTED_ARRAY) != 0)
-        fail("unexpected encoded array");
-    free(result);
-
-    json_decref(object);
-    json_decref(array);
-}
-
-
-#define COMPACT_OBJECT "{\"a\":1,\"b\":2}"
-#define COMPACT_ARRAY  "[1,2]"
-
-static void test_compact()
-{
-    json_t *object;
-    json_t *array;
-    char *result;
-
-    object = create_object();
-    array = create_array();
-
-    result = json_dumps(object, JSON_COMPACT);
-    if(strcmp(result, COMPACT_OBJECT) != 0)
-        fail("unexpected encoded object");
-    free(result);
-
-    result = json_dumps(array, JSON_COMPACT);
-    if(strcmp(result, COMPACT_ARRAY) != 0)
-        fail("unexpected encoded array");
-    free(result);
-
-    json_decref(object);
-    json_decref(array);
-}
-
-
-#define INDENTED_COMPACT_OBJECT                 \
-    "{\n"                                       \
-    "    \"a\":1,\n"                            \
-    "    \"b\":2\n"                             \
-    "}"
-#define INDENTED_COMPACT_ARRAY                  \
-    "[\n"                                       \
-    "    1,\n"                                  \
-    "    2\n"                                   \
-    "]"
-
-static void test_compact_indent()
-{
-    json_t *object;
-    json_t *array;
-    char *result;
-
-    object = create_object();
-    array = create_array();
-
-    result = json_dumps(object, JSON_INDENT(4) | JSON_COMPACT);
-    if(strcmp(result, INDENTED_COMPACT_OBJECT) != 0)
-        fail("unexpected encoded object");
-    free(result);
-
-    result = json_dumps(array, JSON_INDENT(4) | JSON_COMPACT);
-    if(strcmp(result, INDENTED_COMPACT_ARRAY) != 0)
-        fail("unexpected encoded array");
-    free(result);
-
-    json_decref(object);
-    json_decref(array);
-}
-
-
-static const char *test_ensure_ascii_data[][2] = {
-    /*
-    { "input", "output" }
-    */
-
-    /* ascii */
-    { "foo", "foo" },
-
-    /* BMP */
-    { "\xc3\xa4 \xc3\xb6 \xc3\xa5", "\\u00e4 \\u00f6 \\u00e5" },
-    { "foo \xc3\xa4\xc3\xa5", "foo \\u00e4\\u00e5" },
-    { "\xc3\xa4\xc3\xa5 foo", "\\u00e4\\u00e5 foo" },
-    { "\xc3\xa4 foo \xc3\xa5", "\\u00e4 foo \\u00e5" },
-
-    /* non-BMP */
-    { "clef g: \xf0\x9d\x84\x9e", "clef g: \\ud834\\udd1e" },
-};
-
-static void test_ensure_ascii()
-{
-    int i;
-    int num_tests = sizeof(test_ensure_ascii_data) / sizeof(const char *) / 2;
-
-    for(i = 0; i < num_tests; i++) {
-        json_t *array, *string;
-        const char *input, *output;
-        char *result, *stripped;
-
-        input = test_ensure_ascii_data[i][0];
-        output = test_ensure_ascii_data[i][1];
-
-        array = json_array();
-        string = json_string(input);
-        if(!array || !string)
-            fail("unable to create json values");
-
-        json_array_append(array, string);
-        result = json_dumps(array, JSON_ENSURE_ASCII);
-
-        /* strip leading [" and trailing "] */
-        stripped = &result[2];
-        stripped[strlen(stripped) - 2] = '\0';
-
-        if(strcmp(stripped, output) != 0) {
-            free(result);
-            fail("the result of json_dumps is invalid");
-        }
-        free(result);
-    }
-}
-
-int main(void)
-{
-    test_normal();
-    test_indent();
-    test_compact();
-    test_compact_indent();
-    test_ensure_ascii();
-
-    return 0;
-}
diff --git a/test/suites/encoding-flags/array/input b/test/suites/encoding-flags/array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/array/output b/test/suites/encoding-flags/array/output
new file mode 100644 (file)
index 0000000..fd8ef09
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/compact-array/env b/test/suites/encoding-flags/compact-array/env
new file mode 100644 (file)
index 0000000..9eab19d
--- /dev/null
@@ -0,0 +1 @@
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/compact-array/input b/test/suites/encoding-flags/compact-array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/compact-array/output b/test/suites/encoding-flags/compact-array/output
new file mode 100644 (file)
index 0000000..3169929
--- /dev/null
@@ -0,0 +1 @@
+[1,2]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/compact-object/env b/test/suites/encoding-flags/compact-object/env
new file mode 100644 (file)
index 0000000..9eab19d
--- /dev/null
@@ -0,0 +1 @@
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/compact-object/input b/test/suites/encoding-flags/compact-object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/compact-object/output b/test/suites/encoding-flags/compact-object/output
new file mode 100644 (file)
index 0000000..73a5d70
--- /dev/null
@@ -0,0 +1 @@
+{"a":1,"b":2}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/ensure-ascii/env b/test/suites/encoding-flags/ensure-ascii/env
new file mode 100644 (file)
index 0000000..6a0e0aa
--- /dev/null
@@ -0,0 +1 @@
+export JSON_ENSURE_ASCII=1
diff --git a/test/suites/encoding-flags/ensure-ascii/input b/test/suites/encoding-flags/ensure-ascii/input
new file mode 100644 (file)
index 0000000..69469ce
--- /dev/null
@@ -0,0 +1,8 @@
+[
+    "foo",
+    "å ä ö",
+    "foo åä",
+    "åä foo",
+    "å foo ä",
+    "clef g: 𝄞"
+]
diff --git a/test/suites/encoding-flags/ensure-ascii/output b/test/suites/encoding-flags/ensure-ascii/output
new file mode 100644 (file)
index 0000000..36f8eb5
--- /dev/null
@@ -0,0 +1 @@
+["foo", "\u00e5 \u00e4 \u00f6", "foo \u00e5\u00e4", "\u00e5\u00e4 foo", "\u00e5 foo \u00e4", "clef g: \ud834\udd1e"]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-array/env b/test/suites/encoding-flags/indent-array/env
new file mode 100644 (file)
index 0000000..273232a
--- /dev/null
@@ -0,0 +1 @@
+export JSON_INDENT=4
diff --git a/test/suites/encoding-flags/indent-array/input b/test/suites/encoding-flags/indent-array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/indent-array/output b/test/suites/encoding-flags/indent-array/output
new file mode 100644 (file)
index 0000000..c57d705
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    1,
+    2
+]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-compact-array/env b/test/suites/encoding-flags/indent-compact-array/env
new file mode 100644 (file)
index 0000000..89c9f78
--- /dev/null
@@ -0,0 +1,2 @@
+export JSON_INDENT=4
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/indent-compact-array/input b/test/suites/encoding-flags/indent-compact-array/input
new file mode 100644 (file)
index 0000000..44e2ace
--- /dev/null
@@ -0,0 +1 @@
+[1, 2]
diff --git a/test/suites/encoding-flags/indent-compact-array/output b/test/suites/encoding-flags/indent-compact-array/output
new file mode 100644 (file)
index 0000000..c57d705
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    1,
+    2
+]
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-compact-object/env b/test/suites/encoding-flags/indent-compact-object/env
new file mode 100644 (file)
index 0000000..89c9f78
--- /dev/null
@@ -0,0 +1,2 @@
+export JSON_INDENT=4
+export JSON_COMPACT=1
diff --git a/test/suites/encoding-flags/indent-compact-object/input b/test/suites/encoding-flags/indent-compact-object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/indent-compact-object/output b/test/suites/encoding-flags/indent-compact-object/output
new file mode 100644 (file)
index 0000000..9cc4294
--- /dev/null
@@ -0,0 +1,4 @@
+{
+    "a":1,
+    "b":2
+}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/indent-object/env b/test/suites/encoding-flags/indent-object/env
new file mode 100644 (file)
index 0000000..273232a
--- /dev/null
@@ -0,0 +1 @@
+export JSON_INDENT=4
diff --git a/test/suites/encoding-flags/indent-object/input b/test/suites/encoding-flags/indent-object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/indent-object/output b/test/suites/encoding-flags/indent-object/output
new file mode 100644 (file)
index 0000000..0fbddba
--- /dev/null
@@ -0,0 +1,4 @@
+{
+    "a": 1,
+    "b": 2
+}
\ No newline at end of file
diff --git a/test/suites/encoding-flags/object/input b/test/suites/encoding-flags/object/input
new file mode 100644 (file)
index 0000000..062e54f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
diff --git a/test/suites/encoding-flags/object/output b/test/suites/encoding-flags/object/output
new file mode 100644 (file)
index 0000000..ecd219f
--- /dev/null
@@ -0,0 +1 @@
+{"a": 1, "b": 2}
\ No newline at end of file
index 44ac463..7aefe56 100644 (file)
@@ -1 +1 @@
-[1, 2, 3, 4, "a", "b", "c", {"foo": "bar", "core": "dump"}, true, false, true, true, null, false]
\ No newline at end of file
+[1, 2, 3, 4, "a", "b", "c", {"core": "dump", "foo": "bar"}, true, false, true, true, null, false]
\ No newline at end of file
index 6d2bc2f..aec5649 100755 (executable)
@@ -5,6 +5,8 @@
 # Jansson is free software; you can redistribute it and/or modify
 # it under the terms of the MIT license. See LICENSE for details.
 
 # Jansson is free software; you can redistribute it and/or modify
 # it under the terms of the MIT license. See LICENSE for details.
 
+export JSON_SORT_KEYS=1
+
 is_test() {
     test -d $test_path
 }
 is_test() {
     test -d $test_path
 }
index 44ac463..7aefe56 100644 (file)
@@ -1 +1 @@
-[1, 2, 3, 4, "a", "b", "c", {"foo": "bar", "core": "dump"}, true, false, true, true, null, false]
\ No newline at end of file
+[1, 2, 3, 4, "a", "b", "c", {"core": "dump", "foo": "bar"}, true, false, true, true, null, false]
\ No newline at end of file
index 6d2bc2f..aec5649 100755 (executable)
@@ -5,6 +5,8 @@
 # Jansson is free software; you can redistribute it and/or modify
 # it under the terms of the MIT license. See LICENSE for details.
 
 # Jansson is free software; you can redistribute it and/or modify
 # it under the terms of the MIT license. See LICENSE for details.
 
+export JSON_SORT_KEYS=1
+
 is_test() {
     test -d $test_path
 }
 is_test() {
     test -d $test_path
 }