File indexing completed on 2024-05-12 05:52:02

0001 /*
0002  * This file is part of the Kate project.
0003  *
0004  * SPDX-FileCopyrightText: 2021 Héctor Mesa Jiménez <wmj.py@gmx.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #include "json_utils_test.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QTest>
0013 #include <json_utils.h>
0014 
0015 QTEST_MAIN(JsonUtilsTest)
0016 
0017 /*
0018  * check that two json objects (A, B) are deeply merged correctly:
0019  *
0020  * - If a key is in A and not B, take A's value
0021  * - If a key is in B ant not A, take B's value
0022  * - If a key is present in A and B:
0023  *  - If both are objects, merge values
0024  *  - If any of them is not an object, take B's value
0025  */
0026 void JsonUtilsTest::testMerge()
0027 {
0028     const auto base = QJsonDocument::fromJson(QByteArray(R"JSON(
0029         {
0030             "nested_object": {
0031                 "number_to_number": 1,
0032                 "number": 2,
0033                 "list_to_list": [5,7],
0034                 "string_to_object": "literal1",
0035                 "settings": {
0036                     "path": "/standard_path",
0037                     "param1": "checked",
0038                     "param2": "unchecked"
0039                 }
0040             },
0041             "list_only_in_A": [1,2,3],
0042             "text": "literal2",
0043             "object_to_same": {"a": 1, "b": 2},
0044             "list_to_empty": [3,2,1],
0045             "string_to_null": "notnull",
0046             "object_only_in_A": {"b": 3}
0047         }
0048     )JSON"));
0049 
0050     QVERIFY(!base.isEmpty());
0051 
0052     const auto addenda = QJsonDocument::fromJson(QByteArray(R"JSON(
0053         {
0054             "nested_object": {
0055                 "number_to_number": 100,
0056                 "list_to_list": [1,2,3],
0057                 "string_to_object": {"a": 1},
0058                 "settings": {
0059                     "path": "/my_local_path",
0060                     "notes": "important notes"
0061                 }
0062             },
0063             "int_only_in_B": 3,
0064             "object_to_same": {},
0065             "list_to_empty": [],
0066             "string_to_null": null
0067         }
0068     )JSON"));
0069 
0070     QVERIFY(!addenda.isEmpty());
0071 
0072     const auto expected = QJsonDocument::fromJson(QByteArray(R"JSON(
0073         {
0074             "nested_object": {
0075                 "number_to_number": 100,
0076                 "number": 2,
0077                 "list_to_list": [1,2,3],
0078                 "string_to_object": {"a": 1},
0079                 "settings": {
0080                     "path": "/my_local_path",
0081                     "notes": "important notes",
0082                     "param1": "checked",
0083                     "param2": "unchecked"
0084                 }
0085             },
0086             "list_only_in_A": [1,2,3],
0087             "text": "literal2",
0088             "object_to_same": {"a": 1, "b": 2},
0089             "list_to_empty": [],
0090             "string_to_null": null,
0091             "object_only_in_A": {"b": 3},
0092             "int_only_in_B": 3
0093         }
0094     )JSON"));
0095 
0096     QVERIFY(!expected.isEmpty());
0097 
0098     const auto result = json::merge(base.object(), addenda.object());
0099 
0100     QCOMPARE(result, expected.object());
0101 }
0102 
0103 #include "moc_json_utils_test.cpp"
0104 
0105 // kate: space-indent on; indent-width 4; replace-tabs on;