Skip to content

Commit 8fe6674

Browse files
committed
Tests are added for the new QJsonObject conversion
1 parent 3017f68 commit 8fe6674

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

Tests/taggedjsonarray_test.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ TEST_F(TaggedArrayFixture, ArraySetFromBrackets)
7070
TEST_F(TaggedArrayFixture, ArrayArrowOperator)
7171
{
7272
auto lambda_customSum = [](QString previous, QJsonValue current) -> QString {return previous + current.toString(); };
73-
const QString concatText = std::reduce(testObj.example_arr->begin(), testObj.example_arr->end(), QString(), lambda_customSum);
73+
const QString concatText = std::accumulate(testObj.example_arr->begin(), testObj.example_arr->end(), QString(), lambda_customSum);
7474
ASSERT_EQ(std::string(EXPECTED_REDUCED_RESULT), concatText.toStdString());
7575
}
7676

@@ -106,7 +106,7 @@ TEST_F(TaggedArrayFixture, TaggedObjectContainer)
106106
TEST_F(TaggedArrayFixture, TaggedObjectContainerVectorOperations)
107107
{
108108
const auto lambda_accumulateAges = [](const int previous, const Identity& curIdentity) -> int {return previous + *curIdentity.age; };
109-
const int totalAge = std::reduce(testObj.example_tagged_object_array->cbegin(), testObj.example_tagged_object_array->cend(), 0, lambda_accumulateAges);
109+
const int totalAge = std::accumulate(testObj.example_tagged_object_array->cbegin(), testObj.example_tagged_object_array->cend(), 0, lambda_accumulateAges);
110110

111111
ASSERT_EQ(EXPECTED_TOTAL_AGE, totalAge);
112112
}
@@ -123,4 +123,24 @@ TEST_F(TaggedArrayFixture, TaggedObjectContainerMutation)
123123

124124
testObj.example_tagged_object_array[0] = Identity{ newObj };
125125
ASSERT_EQ(MODIFIED_AGE, *testObj.example_tagged_object_array.at(0).age);
126-
}
126+
}
127+
128+
//QJsonObject conversions can be done on TaggedJSONArray's as well
129+
TEST_F(TaggedArrayFixture, TaggedStringArraytoQJsonObject)
130+
{
131+
const QJsonObject obj = testObj.toJsonObject();
132+
const QJsonArray arr = obj["example_arr"].toArray();
133+
ASSERT_EQ(EXPECTED_ARRAY_FIRST_ELEMENT_RESULT, arr[0].toString());
134+
}
135+
136+
//Array of TaggedObjects inside a taggedObject can be converted to QJsonObject
137+
TEST_F(TaggedArrayFixture, TaggedObjectArraytoQJsonObject)
138+
{
139+
const QJsonObject obj = testObj.toJsonObject();
140+
const QJsonArray arr = obj["example_tagged_object_array"].toArray();
141+
142+
const auto lambda_accumulateAges = [](const int previous, const QJsonValue& curIdentity) -> int {return previous + curIdentity["age"].toInt(); };
143+
const int totalAge = std::accumulate(arr.cbegin(), arr.cend(), 0, lambda_accumulateAges);
144+
ASSERT_EQ(EXPECTED_TOTAL_AGE, totalAge);
145+
}
146+

Tests/taggedjsonobject_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,30 @@ TEST_F(TaggedObjectFixture, SubclassSetFromAssignment)
291291
testObj.example_sub_class.example_sub_str = MODIFIED_VALUE;
292292
ASSERT_EQ(MODIFIED_VALUE, testObj.example_sub_class.example_sub_str);
293293
}
294+
295+
// A QJsonObject that holds all the data the TaggedObject can be obtained with the toJsonObject method
296+
TEST_F(TaggedObjectFixture, ToJsonObjectInt)
297+
{
298+
const QJsonObject obj = testObj.toJsonObject();
299+
ASSERT_EQ(EXPECTED_INT_RESULT, obj["example_int"].toInt());
300+
}
301+
TEST_F(TaggedObjectFixture, ToJsonObjectString)
302+
{
303+
const QJsonObject obj = testObj.toJsonObject();
304+
ASSERT_EQ(EXPECTED_STRING_RESULT, obj["example_str"].toString().toUtf8());
305+
}
306+
TEST_F(TaggedObjectFixture, ToJsonObjectDouble)
307+
{
308+
const QJsonObject obj = testObj.toJsonObject();
309+
ASSERT_DOUBLE_EQ(EXPECTED_DOUBLE_RESULT, obj["example_double"].toDouble());
310+
}
311+
TEST_F(TaggedObjectFixture, ToJsonObjectSubClass)
312+
{
313+
const QJsonObject obj = testObj.toJsonObject();
314+
ASSERT_EQ(EXPECTED_SUBSTRING_RESULT, obj["example_sub_class"]["example_sub_str"].toString().toUtf8());
315+
}
316+
TEST_F(TaggedObjectFixture, ToJsonObjectQJsonValue)
317+
{
318+
const QJsonObject obj = testObj.toJsonObject();
319+
ASSERT_DOUBLE_EQ(EXPECTED_JSON_VALUE_RESULT, obj["example_json_value1"]["test_value"].toInt());
320+
}

0 commit comments

Comments
 (0)