|
| 1 | +/* |
| 2 | + * .============. |
| 3 | + * // M A K E / \ |
| 4 | + * // C++ DEV / \ |
| 5 | + * // E A S Y / \/ \ |
| 6 | + * ++ ----------. \/\ . |
| 7 | + * \\ \ \ /\ / |
| 8 | + * \\ \ \ / |
| 9 | + * \\ \ \ / |
| 10 | + * -============' |
| 11 | + * |
| 12 | + * Copyright (c) 2025 Hevake and contributors, all rights reserved. |
| 13 | + * |
| 14 | + * This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox) |
| 15 | + * Use of this source code is governed by MIT license that can be found |
| 16 | + * in the LICENSE file in the root of the source tree. All contributing |
| 17 | + * project authors may be found in the CONTRIBUTORS.md file in the root |
| 18 | + * of the source tree. |
| 19 | + */ |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <tbox/base/json.hpp> |
| 22 | +#include "json_deep_loader.h" |
| 23 | +#include "fs.h" |
| 24 | +#include "json.h" |
| 25 | + |
| 26 | +namespace tbox { |
| 27 | +namespace util { |
| 28 | +namespace json { |
| 29 | + |
| 30 | +TEST(JsonDeepLoader, Load) { |
| 31 | + const char *filepath = "/tmp/tbox-json-deep-loader-test.json"; |
| 32 | + const char *json_text = \ |
| 33 | +R"( |
| 34 | +{ |
| 35 | + "int": 123, |
| 36 | + "string": "hello", |
| 37 | + "array": [1, 2, 3] |
| 38 | +} |
| 39 | +)"; |
| 40 | + Json js; |
| 41 | + |
| 42 | + fs::WriteStringToTextFile(filepath, json_text); |
| 43 | + EXPECT_NO_THROW({ js = LoadDeeply(filepath); } ); |
| 44 | + |
| 45 | + int i_value; |
| 46 | + GetField(js, "int", i_value); |
| 47 | + EXPECT_EQ(i_value, 123); |
| 48 | + |
| 49 | + fs::RemoveFile(filepath); |
| 50 | + EXPECT_ANY_THROW({ js = Load(filepath); } ); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(JsonDeepLoader, LoadNoThrow) { |
| 54 | + const char *filepath = "/tmp/tbox-json-deep-loader-test.json"; |
| 55 | + const char *json_text = \ |
| 56 | +R"( |
| 57 | +{ |
| 58 | + "int": 123, |
| 59 | + "string": "hello", |
| 60 | + "array": [1, 2, 3] |
| 61 | +} |
| 62 | +)"; |
| 63 | + Json js; |
| 64 | + |
| 65 | + fs::WriteStringToTextFile(filepath, json_text); |
| 66 | + EXPECT_TRUE(LoadDeeply(filepath, js)); |
| 67 | + |
| 68 | + int i_value; |
| 69 | + GetField(js, "int", i_value); |
| 70 | + EXPECT_EQ(i_value, 123); |
| 71 | + |
| 72 | + fs::RemoveFile(filepath); |
| 73 | + EXPECT_FALSE(Load(filepath, js)); |
| 74 | +} |
| 75 | + |
| 76 | +} |
| 77 | +} |
| 78 | +} |
0 commit comments