A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nlohmann-json-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Tom Henderson <tomh@tomh.org>
5 */
6
7#include "nlohmann/json.hpp"
8
9#include "ns3/test.h"
10
11#include <string>
12
13/**
14 * @file
15 * @ingroup core-tests
16 * Smoke test for the vendored nlohmann/json third-party header.
17 */
18
19namespace ns3
20{
21
22namespace tests
23{
24
25/**
26 * @ingroup core-tests
27 * Parse a trivial JSON document and check that the expected fields
28 * round-trip correctly.
29 */
31{
32 public:
34 ~NlohmannJsonParseTestCase() override = default;
35
36 private:
37 void DoRun() override;
38};
39
41 : TestCase("Parse a trivial JSON document with nlohmann::json")
42{
43}
44
45void
47{
48 const std::string input = R"({
49 "name": "ns-3",
50 "version": 3,
51 "enabled": true,
52 "modules": ["core", "network", "internet"]
53 })";
54
55 nlohmann::json doc;
56 bool parsed = false;
57 try
58 {
59 doc = nlohmann::json::parse(input);
60 parsed = true;
61 }
62 catch (const nlohmann::json::parse_error&)
63 {
64 parsed = false;
65 }
66 NS_TEST_ASSERT_MSG_EQ(parsed, true, "nlohmann::json::parse threw on valid JSON");
67
68 NS_TEST_ASSERT_MSG_EQ(doc.is_object(), true, "Top-level value should be an object");
69 NS_TEST_ASSERT_MSG_EQ(doc["name"].get<std::string>(),
70 "ns-3",
71 "String field did not round-trip");
72 NS_TEST_ASSERT_MSG_EQ(doc["version"].get<int>(), 3, "Integer field did not round-trip");
73 NS_TEST_ASSERT_MSG_EQ(doc["enabled"].get<bool>(), true, "Boolean field did not round-trip");
74 NS_TEST_ASSERT_MSG_EQ(doc["modules"].is_array(), true, "Array field should be an array");
75 NS_TEST_ASSERT_MSG_EQ(doc["modules"].size(),
76 static_cast<std::size_t>(3),
77 "Array field has unexpected length");
78 NS_TEST_ASSERT_MSG_EQ(doc["modules"][0].get<std::string>(),
79 "core",
80 "Array element did not round-trip");
81}
83/**
84 * @ingroup core-tests
85 * nlohmann/json test suite.
86 */
89 public:
91};
92
94 : TestSuite("nlohmann-json")
95{
97}
99/**
100 * @ingroup core-tests
101 * NlohmannJsonTestSuite instance variable.
102 */
104
105} // namespace tests
106
107} // namespace ns3
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
A suite of tests to run.
Definition test.h:1264
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Parse a trivial JSON document and check that the expected fields round-trip correctly.
~NlohmannJsonParseTestCase() override=default
void DoRun() override
Implementation to actually run this TestCase.
static NlohmannJsonTestSuite g_nlohmannJsonTestSuite
NlohmannJsonTestSuite instance variable.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:133
Namespace for test files, TestCases and TestSuites.
Every class exported by the ns3 library is enclosed in the ns3 namespace.