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

Implements class CLASS? More...

Include dependency graph for jsonitemcontainerconverter.test.cpp:

Go to the source code of this file.

Classes

class  JsonItemContainerConverterTest
 Checks JsonItemContainerConverter class and its ability to convert SessionItemContainer to json and back. More...
 

Functions

 TEST_F (JsonItemContainerConverterTest, propertyContainerToFileAndBack)
 SessionItemContainer (with single property item) to json file and back. More...
 
 TEST_F (JsonItemContainerConverterTest, propertyContainerToJson)
 SessionItemContainer (with single property item) to json object. More...
 
 TEST_F (JsonItemContainerConverterTest, propertyContainerToJsonAndBack)
 SessionItemContainer (with single property item) to json object and back. More...
 
 TEST_F (JsonItemContainerConverterTest, universalContainerToJsonAndBack)
 SessionItemContainer (with universal tag and several items) to json object 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 jsonitemcontainerconverter.test.cpp.

Function Documentation

◆ TEST_F() [1/4]

TEST_F ( JsonItemContainerConverterTest  ,
propertyContainerToFileAndBack   
)

SessionItemContainer (with single property item) to json file and back.

Definition at line 135 of file jsonitemcontainerconverter.test.cpp.

136 {
137  // creating container
138  TagInfo tag = TagInfo::propertyTag("thickness", Constants::PropertyType);
139  SessionItemContainer container(tag);
140 
141  // inserting single property item
142  auto item = new PropertyItem;
143  item->setData(42);
144  EXPECT_TRUE(container.insertItem(item, 0));
145 
146  // converting top JSON and checking that it is valid JSON object
147  auto converter = createConverter();
148  auto json = converter->to_json(container);
149 
150  // saving object to file
151  auto fileName = TestUtils::TestFileName(testDir(), "propertyContainerToFileAndBack.json");
152  TestUtils::SaveJson(json, fileName);
153 
154  // loading from file
155  auto document = TestUtils::LoadJson(fileName);
156 
157  // creating second container with same layout, and updating it from JSON
158  SessionItemContainer container2(tag);
159  auto item2 = new PropertyItem;
160  item2->setData(43);
161  EXPECT_TRUE(container2.insertItem(item2, 0));
162  converter->from_json(document.object(), container2);
163 
164  // Checking that item in container2 has been reused, and get same properties as item.
165  EXPECT_EQ(container2.itemAt(0), item2);
166  EXPECT_EQ(item->displayName(), item2->displayName());
167  EXPECT_EQ(item->identifier(), item2->identifier());
168  EXPECT_EQ(42, item2->data<int>());
169 }
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
Holds collection of SessionItem objects related to the same tag.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
const model_type PropertyType
Definition: mvvm_types.h:59
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 UnitConverterUtils::createConverter(), ModelView::SessionItemContainer::insertItem(), ModelView::SessionItemContainer::itemAt(), TestUtils::LoadJson(), ModelView::TagInfo::propertyTag(), ModelView::Constants::PropertyType, TestUtils::SaveJson(), ModelView::SessionItem::setData(), and TestUtils::TestFileName().

Here is the call graph for this function:

◆ TEST_F() [2/4]

TEST_F ( JsonItemContainerConverterTest  ,
propertyContainerToJson   
)

SessionItemContainer (with single property item) to json object.

Definition at line 83 of file jsonitemcontainerconverter.test.cpp.

84 {
85  // creating container
86  TagInfo tag = TagInfo::propertyTag("thickness", Constants::PropertyType);
87  SessionItemContainer container(tag);
88 
89  // inserting single property item
90  auto item = new PropertyItem;
91  item->setData(42);
92  EXPECT_TRUE(container.insertItem(item, 0));
93 
94  // converting top JSON and checking that it is valid JSON object
95  auto converter = createConverter();
96  auto json = converter->to_json(container);
97 
98  JsonItemFormatAssistant assistant;
99  EXPECT_TRUE(assistant.isSessionItemContainer(json));
100 }
Utility class to determine, whether given JSON object can represent various parts of SessionModel.
bool isSessionItemContainer(const QJsonObject &json) const
Returns true if given json object represents SessionItemContainer.

References UnitConverterUtils::createConverter(), ModelView::SessionItemContainer::insertItem(), ModelView::JsonItemFormatAssistant::isSessionItemContainer(), ModelView::TagInfo::propertyTag(), ModelView::Constants::PropertyType, and ModelView::SessionItem::setData().

Here is the call graph for this function:

◆ TEST_F() [3/4]

TEST_F ( JsonItemContainerConverterTest  ,
propertyContainerToJsonAndBack   
)

SessionItemContainer (with single property item) to json object and back.

Definition at line 104 of file jsonitemcontainerconverter.test.cpp.

105 {
106  // creating container
107  TagInfo tag = TagInfo::propertyTag("thickness", Constants::PropertyType);
108  SessionItemContainer container(tag);
109 
110  // inserting single property item
111  auto item = new PropertyItem;
112  item->setData(42);
113  EXPECT_TRUE(container.insertItem(item, 0));
114 
115  // converting top JSON
116  auto converter = createConverter();
117  auto json = converter->to_json(container);
118 
119  // creating second container with same layout, and updating it from JSON
120  SessionItemContainer container2(tag);
121  auto item2 = new PropertyItem;
122  item2->setData(43);
123  EXPECT_TRUE(container2.insertItem(item2, 0));
124  converter->from_json(json, container2);
125 
126  // Checking that item in container2 has been reused, and get same properties as item.
127  EXPECT_EQ(container2.itemAt(0), item2);
128  EXPECT_EQ(item->displayName(), item2->displayName());
129  EXPECT_EQ(item->identifier(), item2->identifier());
130  EXPECT_EQ(42, item2->data<int>());
131 }

References UnitConverterUtils::createConverter(), ModelView::SessionItemContainer::insertItem(), ModelView::SessionItemContainer::itemAt(), ModelView::TagInfo::propertyTag(), ModelView::Constants::PropertyType, and ModelView::SessionItem::setData().

Here is the call graph for this function:

◆ TEST_F() [4/4]

TEST_F ( JsonItemContainerConverterTest  ,
universalContainerToJsonAndBack   
)

SessionItemContainer (with universal tag and several items) to json object and back.

Definition at line 173 of file jsonitemcontainerconverter.test.cpp.

174 {
175  // creating container
176  TagInfo tag = TagInfo::universalTag("items");
177  SessionItemContainer container(tag);
178 
179  // inserting single property item
180  const int n_max_items = 3;
181  for (int i = 0; i < n_max_items; ++i) {
182  auto item = new PropertyItem;
183  item->setData(i + 42);
184  EXPECT_TRUE(container.insertItem(item, 0));
185  }
186 
187  // converting top JSON
188  auto converter = createConverter();
189  auto json = converter->to_json(container);
190 
191  // creating second container with same layout, but without items
192  SessionItemContainer container2(tag);
193  converter->from_json(json, container2);
194 
195  // Checking that container2 got same content as container
196  EXPECT_EQ(container2.itemCount(), n_max_items);
197  for (int i = 0; i < n_max_items; ++i) {
198  EXPECT_EQ(container.itemAt(i)->identifier(), container2.itemAt(i)->identifier());
199  EXPECT_EQ(container.itemAt(i)->data<int>(), container2.itemAt(i)->data<int>());
200  }
201 }

References UnitConverterUtils::createConverter(), ModelView::SessionItem::data(), ModelView::SessionItem::identifier(), ModelView::SessionItemContainer::insertItem(), ModelView::SessionItemContainer::itemAt(), ModelView::SessionItemContainer::itemCount(), ModelView::SessionItem::setData(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function: