File: test_json_merge_patch.c

package info (click to toggle)
yyjson 0.10.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,848 kB
  • sloc: ansic: 26,228; xml: 226; javascript: 99; sh: 13; makefile: 8; cpp: 8; objc: 7
file content (82 lines) | stat: -rw-r--r-- 3,777 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// This file is used to test the `JSON Merge Patch` functions.

#include "yyjson.h"
#include "yy_test_utils.h"

#if !YYJSON_DISABLE_UTILS

static void test_one(const char *orig_json,
                     const char *patch_json,
                     const char *expt_json) {
#if !YYJSON_DISABLE_READER
    yyjson_doc *i_orig_doc = yyjson_read(orig_json, strlen(orig_json), 0);
    yyjson_doc *i_patch_doc = yyjson_read(patch_json, strlen(patch_json), 0);
    yyjson_doc *i_expe_doc = yyjson_read(expt_json, strlen(expt_json), 0);
    yyjson_mut_doc *m_orig_doc = yyjson_doc_mut_copy(i_orig_doc, NULL);
    yyjson_mut_doc *m_patch_doc = yyjson_doc_mut_copy(i_patch_doc, NULL);
    yyjson_mut_doc *m_expe_doc = yyjson_doc_mut_copy(i_expe_doc, NULL);
    
    yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
    yyjson_mut_val *ret1 = yyjson_merge_patch(doc, i_orig_doc->root, i_patch_doc->root);
    yyjson_mut_val *ret2 = yyjson_mut_merge_patch(doc, m_orig_doc->root, m_patch_doc->root);
    
#if !YYJSON_DISABLE_WRITER
    char *str1 = yyjson_mut_val_write(ret1, 0, NULL);
    char *str2 = yyjson_mut_val_write(ret2, 0, NULL);
    yy_assert(strcmp(expt_json, str1) == 0);
    yy_assert(strcmp(expt_json, str2) == 0);
    free(str1);
    free(str2);
#endif
    
    yy_assert(yyjson_mut_equals(m_expe_doc->root, ret1));
    yy_assert(yyjson_mut_equals(m_expe_doc->root, ret2));
    
    yy_assert(yyjson_merge_patch(NULL, NULL, NULL) == NULL);
    yy_assert(yyjson_merge_patch(NULL, i_orig_doc->root, NULL) == NULL);
    yy_assert(yyjson_merge_patch(NULL, NULL, i_patch_doc->root) == NULL);
    yy_assert(yyjson_merge_patch(NULL, i_orig_doc->root, i_patch_doc->root) == NULL);
    yy_assert(yyjson_merge_patch(doc, i_orig_doc->root, NULL) == NULL);
    yy_assert(yyjson_merge_patch(doc, NULL, i_patch_doc->root) != NULL);
    
    yy_assert(yyjson_mut_merge_patch(NULL, NULL, NULL) == NULL);
    yy_assert(yyjson_mut_merge_patch(NULL, m_orig_doc->root, NULL) == NULL);
    yy_assert(yyjson_mut_merge_patch(NULL, NULL, m_patch_doc->root) == NULL);
    yy_assert(yyjson_mut_merge_patch(NULL, m_orig_doc->root, m_patch_doc->root) == NULL);
    yy_assert(yyjson_mut_merge_patch(doc, m_orig_doc->root, NULL) == NULL);
    yy_assert(yyjson_mut_merge_patch(doc, NULL, m_patch_doc->root) != NULL);
    
    yyjson_mut_doc_free(doc);
    yyjson_mut_doc_free(m_expe_doc);
    yyjson_mut_doc_free(m_patch_doc);
    yyjson_mut_doc_free(m_orig_doc);
    yyjson_doc_free(i_expe_doc);
    yyjson_doc_free(i_patch_doc);
    yyjson_doc_free(i_orig_doc);
    
#endif
}

yy_test_case(test_json_merge_patch) {
    // test cases from spec: https://tools.ietf.org/html/rfc7386
    test_one("{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}");
    test_one("{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}");
    test_one("{\"a\":\"b\"}", "{\"a\":null }", "{}");
    test_one("{\"a\":\"b\"}", "{\"a\":null }", "{}");
    test_one("{\"a\":\"b\", \"b\":\"c\"}", "{\"a\":null }", "{\"b\":\"c\"}");
    test_one("{\"a\":[\"b\"] }", "{\"a\":\"c\"}", "{\"a\":\"c\"}");
    test_one("{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}");
    test_one("{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}");
    test_one("{\"a\":{\"b\":\"c\"}}", "{\"a\":[1]}", "{\"a\":[1]}");
    test_one("[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]");
    test_one("{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]");
    test_one("{\"a\":\"foo\"}", "null", "null");
    test_one("{\"a\":\"foo\"}", "\"bar\"", "\"bar\"");
    test_one("{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}");
    test_one("[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}");
    test_one("{}", "{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}");
}

#else
yy_test_case(test_json_merge_patch) {}
#endif