Ocean
Loading...
Searching...
No Matches
TestJSONParser.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_TEST_TESTIO_TEST_JSON_PARSER_H
9#define META_OCEAN_TEST_TESTIO_TEST_JSON_PARSER_H
10
12
14
15#include "ocean/io/JSONParser.h"
16
19
20namespace Ocean
21{
22
23namespace Test
24{
25
26namespace TestIO
27{
28
29/**
30 * This class implements a JSON parser test.
31 * @ingroup testio
32 */
33class OCEAN_TEST_IO_EXPORT TestJSONParser
34{
35 protected:
36
37 /**
38 * This class holds both the JSON string and the expected parsed value.
39 */
41 {
42 public:
43
44 /**
45 * Generates a random JSON string with random content.
46 * @param randomGenerator The random generator to use
47 * @param maxLength Maximum length of the string
48 * @return The generated JSON-safe string
49 */
50 static std::string randomString(RandomGenerator& randomGenerator, const size_t maxLength = 20);
51
52 /**
53 * Generates a random JSON value (primitive, array, or object) with expected value.
54 * @param randomGenerator The random generator to use
55 * @param remainingDepth Remaining nesting depth allowed
56 * @return The generated JSON test data
57 */
58 static JSONTestData randomValue(RandomGenerator& randomGenerator, const unsigned int remainingDepth);
59
60 /**
61 * Generates a random JSON array with expected value.
62 * @param randomGenerator The random generator to use
63 * @param remainingDepth Remaining nesting depth allowed
64 * @return The generated JSON test data
65 */
66 static JSONTestData randomArray(RandomGenerator& randomGenerator, const unsigned int remainingDepth);
67
68 /**
69 * Generates a random JSON object with expected value.
70 * @param randomGenerator The random generator to use
71 * @param remainingDepth Remaining nesting depth allowed
72 * @return The generated JSON test data
73 */
74 static JSONTestData randomObject(RandomGenerator& randomGenerator, const unsigned int remainingDepth);
75
76 public:
77
78 /// The JSON string representation.
79 std::string jsonString_;
80
81 /// The expected parsed value.
83 };
84
85 public:
86
87 /**
88 * Tests all JSON parser functions.
89 * @param testDuration The number of seconds for each test
90 * @param selector The test selector
91 * @return True, if succeeded
92 */
93 static bool test(const double testDuration, const TestSelector& selector);
94
95 /**
96 * Tests parsing of primitive JSON values (null, boolean, number, string).
97 * @param testDuration The number of seconds for each test
98 * @return True, if succeeded
99 */
100 static bool testPrimitives(const double testDuration);
101
102 /**
103 * Tests parsing of JSON arrays.
104 * @param testDuration The number of seconds for each test
105 * @return True, if succeeded
106 */
107 static bool testArrays(const double testDuration);
108
109 /**
110 * Tests parsing of JSON objects.
111 * @return True, if succeeded
112 */
113 static bool testObjects();
114
115 /**
116 * Tests parsing of nested JSON structures.
117 * @return True, if succeeded
118 */
119 static bool testNestedStructures();
120
121 /**
122 * Tests error handling with invalid JSON.
123 * @return True, if succeeded
124 */
125 static bool testErrorHandling();
126
127 /**
128 * Tests parsing from different input sources (file, buffer, stream).
129 * @return True, if succeeded
130 */
131 static bool testInputSources();
132
133 /**
134 * Tests parsing of randomly generated JSON structures.
135 * @param testDuration The number of seconds for each test
136 * @return True, if succeeded
137 */
138 static bool testRandomJSON(const double testDuration);
139
140 /**
141 * Tests strict and lenient parsing modes (trailing commas).
142 * @return True, if succeeded
143 */
145
146 protected:
147
148 /**
149 * Compares two JSON values for equality.
150 * @param validation The validation object
151 * @param parsed The parsed JSON value
152 * @param expected The expected JSON value
153 * @return True if they match
154 */
155 static bool compareJSONValues(Validation& validation, const IO::JSONParser::JSONValue& parsed, const IO::JSONParser::JSONValue& expected);
156};
157
158} // namespace TestIO
159
160} // namespace Test
161
162} // namespace Ocean
163
164#endif // META_OCEAN_TEST_TESTIO_TEST_JSON_PARSER_H
This class implements a JSON value that can hold different JSON types.
Definition JSONParser.h:35
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
This class holds both the JSON string and the expected parsed value.
Definition TestJSONParser.h:41
IO::JSONParser::JSONValue expectedValue_
The expected parsed value.
Definition TestJSONParser.h:82
static JSONTestData randomValue(RandomGenerator &randomGenerator, const unsigned int remainingDepth)
Generates a random JSON value (primitive, array, or object) with expected value.
static std::string randomString(RandomGenerator &randomGenerator, const size_t maxLength=20)
Generates a random JSON string with random content.
static JSONTestData randomArray(RandomGenerator &randomGenerator, const unsigned int remainingDepth)
Generates a random JSON array with expected value.
static JSONTestData randomObject(RandomGenerator &randomGenerator, const unsigned int remainingDepth)
Generates a random JSON object with expected value.
std::string jsonString_
The JSON string representation.
Definition TestJSONParser.h:79
This class implements a JSON parser test.
Definition TestJSONParser.h:34
static bool testArrays(const double testDuration)
Tests parsing of JSON arrays.
static bool testObjects()
Tests parsing of JSON objects.
static bool testPrimitives(const double testDuration)
Tests parsing of primitive JSON values (null, boolean, number, string).
static bool testRandomJSON(const double testDuration)
Tests parsing of randomly generated JSON structures.
static bool testErrorHandling()
Tests error handling with invalid JSON.
static bool testNestedStructures()
Tests parsing of nested JSON structures.
static bool testStrictAndLenientParsing()
Tests strict and lenient parsing modes (trailing commas).
static bool compareJSONValues(Validation &validation, const IO::JSONParser::JSONValue &parsed, const IO::JSONParser::JSONValue &expected)
Compares two JSON values for equality.
static bool testInputSources()
Tests parsing from different input sources (file, buffer, stream).
static bool test(const double testDuration, const TestSelector &selector)
Tests all JSON parser functions.
This class implements a test selector that parses test function strings and determines which tests sh...
Definition TestSelector.h:51
This class implements a helper class to validate tests.
Definition Validation.h:105
The namespace covering the entire Ocean framework.
Definition Accessor.h:15