Skip to content

Commit 16eba9c

Browse files
msiebenMFransen69
andauthored
Development/metrol 1231 (#2038)
* [corre/JSON / Tests/unit/core] : METROL-1231 * [Tests/unit/core] : rename METROL-1231 to JsonObject * [Tests/unit/core] : add 'access' test to 'test_jsonobject.cpp' * [Tests/unit/core] : enable 'test_jsonobject.cpp' * [Tests/unit/core] : add 'indexing' test to 'test_jsonobject.cpp' --------- Co-authored-by: MFransen69 <[email protected]>
1 parent 76aacdc commit 16eba9c

File tree

3 files changed

+209
-2
lines changed

3 files changed

+209
-2
lines changed

Source/core/JSON.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,7 +3815,9 @@ namespace Core {
38153815
{
38163816
JSONElementList::iterator index(_data.begin());
38173817

3818-
while ((index != _data.end()) && (index->first != label)) {
3818+
while ( (index != _data.end())
3819+
&& (strncmp(index->first, label, std::min(strlen(label), strlen(index->first))) != 0)
3820+
) {
38193821
index++;
38203822
}
38213823

@@ -4861,7 +4863,9 @@ namespace Core {
48614863

48624864
bool HasLabel(const TCHAR labelName[]) const
48634865
{
4864-
return (Find(labelName) != _elements.end());
4866+
return ( Find(labelName) != _elements.end()
4867+
&& Container::HasLabel(labelName) != false
4868+
);
48654869
}
48664870

48674871
Iterator Variants() const
@@ -4876,6 +4880,17 @@ namespace Core {
48764880
}
48774881
string GetDebugString(int indent = 0) const;
48784882

4883+
4884+
void Remove(const TCHAR label[])
4885+
{
4886+
Elements::iterator index = Find(label);
4887+
if (index != _elements.end()) {
4888+
_elements.erase(index);
4889+
}
4890+
4891+
Container::Remove(label);
4892+
}
4893+
48794894
private:
48804895
Elements::iterator Find(const TCHAR fieldName[])
48814896
{

Tests/unit/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ add_executable(${TEST_RUNNER_NAME}
3737
test_ipcclient.cpp
3838
test_iso639.cpp
3939
test_iterator.cpp
40+
test_jsonobject.cpp
4041
test_jsonparser.cpp
4142
test_jsonstring.cpp
4243
test_keyvalue.cpp
@@ -101,6 +102,7 @@ add_executable(${TEST_RUNNER_NAME}
101102
test_hex2strserialization.cpp
102103
test_iso639.cpp
103104
test_iterator.cpp
105+
test_jsonobject.cpp
104106
test_jsonparser.cpp
105107
test_jsonstring.cpp
106108
test_keyvalue.cpp
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* If not stated otherwise in this file or this component's LICENSE file the
3+
* following copyright and licenses apply:
4+
*
5+
* Copyright 2020 Metrological
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifdef __APPLE__
21+
#include <time.h>
22+
#endif
23+
24+
#include <gtest/gtest.h>
25+
26+
#ifndef MODULE_NAME
27+
#include "../Module.h"
28+
#endif
29+
30+
#include <core/core.h>
31+
32+
namespace Thunder {
33+
namespace Tests {
34+
namespace Core {
35+
36+
TEST(JSONOBJECT, FirstKeyRemove)
37+
{
38+
const std::string keyValues = R"({"Name": "Your full name", "Age": 0, "Gender": "Undisclosed"})";
39+
40+
JsonObject container;
41+
42+
EXPECT_TRUE(container.FromString(keyValues));
43+
44+
EXPECT_FALSE(container.HasLabel("name"));
45+
46+
EXPECT_TRUE(container.HasLabel("Name"));
47+
48+
/* void */ container.Remove("Name");
49+
50+
EXPECT_FALSE(container.HasLabel("Name"));
51+
}
52+
53+
TEST(JSONOBJECT, SecondKeyRemove)
54+
{
55+
const std::string keyValues = R"({"Name": "Your full name", "Age": 0, "Gender": "Undisclosed"})";
56+
57+
JsonObject container;
58+
59+
EXPECT_TRUE(container.FromString(keyValues));
60+
61+
EXPECT_FALSE(container.HasLabel("age"));
62+
63+
EXPECT_TRUE(container.HasLabel("Age"));
64+
65+
/* void */ container.Remove("Age");
66+
67+
EXPECT_FALSE(container.HasLabel("Age"));
68+
}
69+
70+
TEST(JSONOBJECT, ThirdKeyRemove)
71+
{
72+
const std::string keyValues = R"({"Name": "Your full name", "Age": 0, "Gender": "Undisclosed"})";
73+
74+
JsonObject container;
75+
76+
EXPECT_TRUE(container.FromString(keyValues));
77+
78+
EXPECT_FALSE(container.HasLabel("gender"));
79+
80+
EXPECT_TRUE(container.HasLabel("Gender"));
81+
82+
/* void */ container.Remove("Gender");
83+
84+
EXPECT_FALSE(container.HasLabel("Gender"));
85+
}
86+
87+
TEST(JSONOBJECT, IdenticalKeyValueRemove)
88+
{
89+
const std::string keyValues = R"({"Name": "Your full name", "Age": "Age", "Gender": "Undisclosed"})";
90+
91+
JsonObject container;
92+
93+
EXPECT_TRUE(container.FromString(keyValues));
94+
95+
EXPECT_FALSE(container.HasLabel("age"));
96+
97+
EXPECT_TRUE(container.HasLabel("Age"));
98+
99+
/* void */ container.Remove("Age");
100+
101+
EXPECT_FALSE(container.HasLabel("Age"));
102+
}
103+
104+
TEST(JSONOBJECT, KeyValueAccess)
105+
{
106+
JsonObject container;
107+
108+
::Thunder::Core::JSON::Variant intType{ 123 };
109+
::Thunder::Core::JSON::Variant floatType{ 123.456f };
110+
::Thunder::Core::JSON::Variant doubleType{ 123.456 }; // defaults to double with omitted suffix
111+
::Thunder::Core::JSON::Variant boolType{ true };
112+
::Thunder::Core::JSON::Variant stringType{ "scribble" };
113+
114+
// An empty JsonObject is valid
115+
EXPECT_TRUE(container.IsValid());
116+
117+
/* void */ container.Set("integer", intType);
118+
/* void */ container.Set("float", floatType);
119+
/* void */ container.Set("double", doubleType);
120+
/* void */ container.Set("boolean", boolType);
121+
/* void */ container.Set("string", stringType);
122+
123+
// All contained elements are valid
124+
EXPECT_TRUE(container.IsValid());
125+
126+
::Thunder::Core::JSON::Variant intFound = container.Get("integer");
127+
// Create and add if it does not exist otherwise return the existing element
128+
::Thunder::Core::JSON::Variant& intRefFound = container["integer"];
129+
130+
EXPECT_TRUE(intFound.Content() == ::Thunder::Core::JSON::Variant::type::NUMBER);
131+
EXPECT_EQ(intFound.Number(), 123);
132+
EXPECT_TRUE(intFound == intRefFound);
133+
134+
::Thunder::Core::JSON::Variant floatFound = container.Get("float");
135+
::Thunder::Core::JSON::Variant& floatRefFound = container["float"];
136+
EXPECT_TRUE(floatFound.Content() == ::Thunder::Core::JSON::Variant::type::FLOAT);
137+
EXPECT_FLOAT_EQ(floatFound.Float(), 123.456);
138+
EXPECT_TRUE(floatFound == floatRefFound);
139+
140+
::Thunder::Core::JSON::Variant doubleFound = container.Get("double");
141+
::Thunder::Core::JSON::Variant& doubleRefFound = container["double"];
142+
EXPECT_TRUE(doubleFound.Content() == ::Thunder::Core::JSON::Variant::type::DOUBLE);
143+
EXPECT_FLOAT_EQ(doubleFound.Float(), 123.456);
144+
EXPECT_TRUE(doubleFound == doubleRefFound);
145+
146+
::Thunder::Core::JSON::Variant boolFound = container.Get("boolean");
147+
::Thunder::Core::JSON::Variant& boolRefFound = container["boolean"];
148+
EXPECT_TRUE(boolFound.Content() == ::Thunder::Core::JSON::Variant::type::BOOLEAN);
149+
EXPECT_EQ(boolFound.Boolean(), true);
150+
EXPECT_TRUE(boolFound == boolRefFound);
151+
152+
::Thunder::Core::JSON::Variant stringFound = container.Get("string");
153+
::Thunder::Core::JSON::Variant& stringRefFound = container["string"];
154+
EXPECT_TRUE(stringFound.Content() == ::Thunder::Core::JSON::Variant::type::STRING);
155+
EXPECT_STREQ(stringFound.String().c_str(), "scribble");
156+
EXPECT_TRUE(stringFound == stringRefFound);
157+
158+
container.Clear();
159+
160+
// The container should have nothing to index
161+
::Thunder::Core::JSON::VariantContainer::Iterator it = container.Variants();
162+
EXPECT_FALSE(it.IsValid());
163+
164+
// It does not exist so a default constructed variant is returned
165+
// Create and add a default variant with the label 'integer'
166+
::Thunder::Core::JSON::Variant& intRefCreated = container["integer"];
167+
EXPECT_TRUE(container.HasLabel("integer"));
168+
EXPECT_TRUE(intRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY);
169+
170+
::Thunder::Core::JSON::Variant& floatRefCreated = container["float"];
171+
EXPECT_TRUE(container.HasLabel("float"));
172+
EXPECT_TRUE(floatRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY);
173+
174+
::Thunder::Core::JSON::Variant& doubleRefCreated = container["double"];
175+
EXPECT_TRUE(container.HasLabel("double"));
176+
EXPECT_TRUE(doubleRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY);
177+
178+
::Thunder::Core::JSON::Variant& boolRefCreated = container["boolean"];
179+
EXPECT_TRUE(container.HasLabel("boolean"));
180+
EXPECT_TRUE(boolRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY);
181+
182+
::Thunder::Core::JSON::Variant& stringRefCreated = container["string"];
183+
EXPECT_TRUE(container.HasLabel("string"));
184+
EXPECT_TRUE(stringRefCreated.Content() == ::Thunder::Core::JSON::Variant::type::EMPTY);
185+
}
186+
187+
188+
} // Core
189+
} // Tests
190+
} // Thunder

0 commit comments

Comments
 (0)