BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitemformatassistant.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testmodel/jsonitemformatassistant.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "google_test.h"
18 #include <QJsonArray>
19 #include <QJsonObject>
20 
21 using namespace ModelView;
22 
23 //! Checks JsonItem class and its ability to convert SessionItems to json and back.
24 
25 class JsonItemFormatAssistantTest : public ::testing::Test {
26 public:
28 };
29 
31 
32 //! Checks the validity of json object representing SessionItem.
33 
35 {
36  JsonItemFormatAssistant assistant;
37 
38  // empty json object is not valid
39  QJsonObject object;
40  EXPECT_FALSE(assistant.isSessionItem(object));
41 
42  // it also should contain array
43  object[JsonItemFormatAssistant::modelKey] = "abc";
44  object[JsonItemFormatAssistant::itemDataKey] = QJsonArray();
45  object[JsonItemFormatAssistant::itemTagsKey] = 42; // intentionally incorrect
46  EXPECT_FALSE(assistant.isSessionItem(object));
47 
48  // correctly constructed
49  object[JsonItemFormatAssistant::itemTagsKey] = QJsonObject();
50  EXPECT_TRUE(assistant.isSessionItem(object));
51 }
52 
53 //! Checks if json object is correctly identified as representing DataRole.
54 
56 {
57  JsonItemFormatAssistant assistant;
58  JsonVariantConverter variant_converter;
59 
60  // valid json object representing DataRole
61  QJsonObject object;
63  object[JsonItemFormatAssistant::variantKey] = variant_converter.get_json(QVariant(1.23));
64  EXPECT_TRUE(assistant.isSessionItemData(object));
65 
66  // invalid json object which can't represent DataRole
67  QJsonObject object2;
69  EXPECT_FALSE(assistant.isSessionItemData(object2));
70 
71  // another invalid json object
72  QJsonObject object3;
74  object3[JsonItemFormatAssistant::variantKey] = variant_converter.get_json(QVariant(1.23));
75  object3["abc"] = variant_converter.get_json(QVariant::fromValue(std::string("xxx")));
76  EXPECT_FALSE(assistant.isSessionItemData(object3));
77 }
78 
79 //! Checks the validity of json object representing SessionItemTags.
80 
82 {
83  JsonItemFormatAssistant assistant;
84 
85  // empty json object is not valid
86  QJsonObject object;
87  EXPECT_FALSE(assistant.isSessionItemTags(object));
88 
89  // it also should contain array
91  object[JsonItemFormatAssistant::containerKey] = QJsonArray();
92  EXPECT_TRUE(assistant.isSessionItemTags(object));
93 }
94 
95 //! Checks the validity of json object representing SessionItemContainer.
96 
97 TEST_F(JsonItemFormatAssistantTest, isSessionItemContainer)
98 {
99  JsonItemFormatAssistant assistant;
100 
101  // empty json object is not valid
102  QJsonObject object;
103  EXPECT_FALSE(assistant.isSessionItemContainer(object));
104 
105  // it also should contain array
106  object[JsonItemFormatAssistant::tagInfoKey] = QJsonObject();
107  object[JsonItemFormatAssistant::itemsKey] = QJsonArray();
108  EXPECT_TRUE(assistant.isSessionItemContainer(object));
109 }
110 
111 //! Validity of json object representing SessionModel.
112 
113 TEST_F(JsonItemFormatAssistantTest, isValidSessionModel)
114 {
115  JsonItemFormatAssistant assistant;
116 
117  // empty json object is not valid
118  QJsonObject object;
119  EXPECT_FALSE(assistant.isSessionModel(object));
120 
121  // json object representing valid SessionModel
122  QJsonObject object2;
124  object2[JsonItemFormatAssistant::itemsKey] = QJsonArray();
125  EXPECT_TRUE(assistant.isSessionModel(object2));
126 }
Checks JsonItem class and its ability to convert SessionItems to json and back.
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionModel(const QJsonObject &object) const
Returns true if given json object represents SessionModel.
bool isSessionItemContainer(const QJsonObject &json) const
Returns true if given json object represents SessionItemContainer.
bool isSessionItem(const QJsonObject &json) const
Returns true if given json object represents SessionItem.
bool isSessionItemTags(const QJsonObject &json) const
Returns true if given json object represents SessionItemTags.
bool isSessionItemData(const QJsonObject &json) const
Default converter between supported variants and json objects.
QJsonObject get_json(const Variant &variant) override
Defines class CLASS?
Defines class CLASS?
TEST_F(JsonItemFormatAssistantTest, isSessionItem)
Checks the validity of json object representing SessionItem.
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.