BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitemconverter.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for jsonitemconverter.test.cpp:

Go to the source code of this file.

Classes

class  JsonItemConverterTest
 Checks JsonItem class and its ability to convert SessionItems to json and back. More...
 
class  JsonItemConverterTest::TestItem
 
class  JsonItemConverterTest::TestModel
 

Functions

 TEST_F (JsonItemConverterTest, parentAndChildToFileAndBack)
 Parent and child to json file and back. More...
 
 TEST_F (JsonItemConverterTest, parentAndChildToJsonAndBack)
 Parent and child to json object. More...
 
 TEST_F (JsonItemConverterTest, propertyItemToFileAndBack)
 PropertyItem to json file and back. More...
 
 TEST_F (JsonItemConverterTest, propertyItemToJson)
 PropertyItem to json object. More...
 
 TEST_F (JsonItemConverterTest, propertyItemToJsonAndBack)
 PropertyItem to json object and back. More...
 
 TEST_F (JsonItemConverterTest, testItemToFileAndBack)
 TestItem to json file and back. More...
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file jsonitemconverter.test.cpp.

Function Documentation

◆ TEST_F() [1/6]

TEST_F ( JsonItemConverterTest  ,
parentAndChildToFileAndBack   
)

Parent and child to json file and back.

Definition at line 170 of file jsonitemconverter.test.cpp.

171 {
172  auto converter = createConverter();
173  const std::string model_type(Constants::BaseType);
174 
175  auto parent = std::make_unique<SessionItem>(model_type);
176  parent->setDisplayName("parent_name");
177  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
178  auto child = new SessionItem(model_type);
179  child->setDisplayName("child_name");
180  parent->insertItem(child, TagRow::append());
181 
182  // converting to json
183  auto object = converter->to_json(parent.get());
184  JsonItemFormatAssistant assistant;
185  EXPECT_TRUE(assistant.isSessionItem(object));
186 
187  // saving object to file
188  auto fileName = TestUtils::TestFileName(testDir(), "parentAndChildToFileAndBack.json");
189  TestUtils::SaveJson(object, fileName);
190 
191  // converting document back to item
192  auto document = TestUtils::LoadJson(fileName);
193  auto reco_parent = converter->from_json(document.object());
194 
195  // checking parent reconstruction
196  EXPECT_EQ(reco_parent->childrenCount(), 1);
197  EXPECT_EQ(reco_parent->modelType(), model_type);
198  EXPECT_EQ(reco_parent->displayName(), "parent_name");
199  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
200  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
201  EXPECT_EQ(reco_parent->model(), nullptr);
202 
203  // checking child reconstruction
204  auto reco_child = reco_parent->getItem("defaultTag");
205  EXPECT_EQ(reco_child->parent(), reco_parent.get());
206  EXPECT_EQ(reco_child->childrenCount(), 0);
207  EXPECT_EQ(reco_child->modelType(), model_type);
208  EXPECT_EQ(reco_child->displayName(), "child_name");
209  EXPECT_EQ(reco_child->identifier(), child->identifier());
210  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
211 }
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionItem(const QJsonObject &json) const
Returns true if given json object represents SessionItem.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
const model_type BaseType
Definition: mvvm_types.h:45
std::string model_type
Definition: types.h:23
std::string TestFileName(const std::string &test_sub_dir, const std::string &file_name)
Returns full path to the file in test directory.
Definition: test_utils.cpp:52
void SaveJson(const QJsonObject &object, const std::string &fileName)
Definition: test_utils.cpp:58
QJsonDocument LoadJson(const std::string &fileName)
Definition: test_utils.cpp:81
std::unique_ptr< IUnitConverter > createConverter(const ISimulation &simulation)

References ModelView::TagRow::append(), ModelView::Constants::BaseType, UnitConverterUtils::createConverter(), ModelView::JsonItemFormatAssistant::isSessionItem(), TestUtils::LoadJson(), TestUtils::SaveJson(), TestUtils::TestFileName(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [2/6]

TEST_F ( JsonItemConverterTest  ,
parentAndChildToJsonAndBack   
)

Parent and child to json object.

Definition at line 130 of file jsonitemconverter.test.cpp.

131 {
132  auto converter = createConverter();
133  const std::string model_type(Constants::BaseType);
134 
135  auto parent = std::make_unique<SessionItem>(model_type);
136  parent->setDisplayName("parent_name");
137  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
138  auto child = new SessionItem(model_type);
139  child->setDisplayName("child_name");
140  parent->insertItem(child, TagRow::append());
141 
142  // converting to json
143  auto object = converter->to_json(parent.get());
144  JsonItemFormatAssistant assistant;
145  EXPECT_TRUE(assistant.isSessionItem(object));
146 
147  // converting json back to item
148  auto reco_parent = converter->from_json(object);
149 
150  // checking parent reconstruction
151  EXPECT_EQ(reco_parent->childrenCount(), 1);
152  EXPECT_EQ(reco_parent->modelType(), model_type);
153  EXPECT_EQ(reco_parent->displayName(), "parent_name");
154  EXPECT_EQ(reco_parent->identifier(), parent->identifier());
155  EXPECT_EQ(reco_parent->itemTags()->defaultTag(), "defaultTag");
156  EXPECT_EQ(reco_parent->model(), nullptr);
157 
158  // checking child reconstruction
159  auto reco_child = reco_parent->getItem("defaultTag");
160  EXPECT_EQ(reco_child->parent(), reco_parent.get());
161  EXPECT_EQ(reco_child->childrenCount(), 0);
162  EXPECT_EQ(reco_child->modelType(), model_type);
163  EXPECT_EQ(reco_child->displayName(), "child_name");
164  EXPECT_EQ(reco_child->identifier(), child->identifier());
165  EXPECT_EQ(reco_child->itemTags()->defaultTag(), "");
166 }

References ModelView::TagRow::append(), ModelView::Constants::BaseType, UnitConverterUtils::createConverter(), ModelView::JsonItemFormatAssistant::isSessionItem(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [3/6]

TEST_F ( JsonItemConverterTest  ,
propertyItemToFileAndBack   
)

PropertyItem to json file and back.

Definition at line 108 of file jsonitemconverter.test.cpp.

109 {
110  auto converter = createConverter();
111 
112  PropertyItem item;
113  auto object = converter->to_json(&item);
114 
115  // saving object to file
116  auto fileName = TestUtils::TestFileName(testDir(), "propertyItemToFileAndBack.json");
117  TestUtils::SaveJson(object, fileName);
118 
119  auto document = TestUtils::LoadJson(fileName);
120  auto reco = converter->from_json(document.object());
121 
122  EXPECT_EQ(reco->parent(), nullptr);
123  EXPECT_EQ(reco->modelType(), item.modelType());
124  EXPECT_EQ(reco->displayName(), item.displayName());
125  EXPECT_EQ(reco->identifier(), item.identifier());
126 }
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
virtual std::string displayName() const
Returns display name.
Definition: sessionitem.cpp:94
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80

References UnitConverterUtils::createConverter(), ModelView::SessionItem::displayName(), ModelView::SessionItem::identifier(), TestUtils::LoadJson(), ModelView::SessionItem::modelType(), TestUtils::SaveJson(), and TestUtils::TestFileName().

Here is the call graph for this function:

◆ TEST_F() [4/6]

TEST_F ( JsonItemConverterTest  ,
propertyItemToJson   
)

PropertyItem to json object.

Definition at line 76 of file jsonitemconverter.test.cpp.

77 {
78  auto converter = createConverter();
79 
80  PropertyItem item;
81  auto object = converter->to_json(&item);
82 
83  // this object represents SessionItem
84  JsonItemFormatAssistant assistant;
85  EXPECT_TRUE(assistant.isSessionItem(object));
86 }

References UnitConverterUtils::createConverter(), and ModelView::JsonItemFormatAssistant::isSessionItem().

Here is the call graph for this function:

◆ TEST_F() [5/6]

TEST_F ( JsonItemConverterTest  ,
propertyItemToJsonAndBack   
)

PropertyItem to json object and back.

Definition at line 90 of file jsonitemconverter.test.cpp.

91 {
92  auto converter = createConverter();
93 
94  PropertyItem item;
95  item.setToolTip("abc");
96  auto object = converter->to_json(&item);
97 
98  auto reco = converter->from_json(object);
99 
100  EXPECT_EQ(reco->modelType(), item.modelType());
101  EXPECT_EQ(reco->displayName(), item.displayName());
102  EXPECT_EQ(reco->identifier(), item.identifier());
103  EXPECT_EQ(reco->toolTip(), std::string("abc"));
104 }
SessionItem * setToolTip(const std::string &tooltip)
Sets item tooltip (fluent interface).

References UnitConverterUtils::createConverter(), ModelView::SessionItem::displayName(), ModelView::SessionItem::identifier(), ModelView::SessionItem::modelType(), and ModelView::SessionItem::setToolTip().

Here is the call graph for this function:

◆ TEST_F() [6/6]

TEST_F ( JsonItemConverterTest  ,
testItemToFileAndBack   
)

TestItem to json file and back.

Definition at line 215 of file jsonitemconverter.test.cpp.

216 {
217  auto converter = createConverter();
218 
219  TestItem item;
220  auto object = converter->to_json(&item);
221 
222  // saving object to file
223  auto fileName = TestUtils::TestFileName(testDir(), "testItemToFileAndBack.json");
224  TestUtils::SaveJson(object, fileName);
225 
226  auto document = TestUtils::LoadJson(fileName);
227  auto reco = converter->from_json(document.object());
228 
229  EXPECT_EQ(reco->parent(), nullptr);
230  EXPECT_EQ(reco->modelType(), item.modelType());
231  EXPECT_EQ(reco->displayName(), item.displayName());
232  EXPECT_EQ(reco->identifier(), item.identifier());
233 
234  EXPECT_EQ(reco->toolTip(), "compound");
235  // tooltip was preserved after the serialization
236  EXPECT_EQ(reco->getItem("Thickness")->toolTip(), "thickness");
237 }

References UnitConverterUtils::createConverter(), TestUtils::LoadJson(), TestUtils::SaveJson(), and TestUtils::TestFileName().

Here is the call graph for this function: