BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jsonitemcopystrategy.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/jsonitemcopystrategy.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 "mvvm/model/itemfactory.h"
22 
23 using namespace ModelView;
24 
25 class JsonItemCopyStrategyTest : public ::testing::Test {
26 public:
28  : m_factory(std::make_unique<ItemFactory>(CreateStandardItemCatalogue()))
29  {
30  }
32 
33  std::unique_ptr<JsonItemCopyStrategy> createCopyStrategy()
34  {
35  return std::make_unique<JsonItemCopyStrategy>(m_factory.get());
36  }
37 
38  std::unique_ptr<ItemFactory> m_factory;
39 };
40 
42 
43 //! Saving/restoring PropertyItem.
44 
46 {
47  auto strategy = createCopyStrategy();
48 
49  PropertyItem item;
50  item.setData(42.0);
51 
52  auto copy = strategy->createCopy(&item);
53 
54  EXPECT_EQ(item.modelType(), copy->modelType());
55  EXPECT_EQ(item.data<QVariant>(), copy->data<QVariant>());
56  EXPECT_FALSE(item.identifier() == copy->identifier());
57 }
58 
59 //! Saving/restoring CompoundItem.
60 
62 {
63  auto strategy = createCopyStrategy();
64 
65  CompoundItem item;
66  auto property = item.addProperty("thickness", 42.0);
67 
68  auto copy = strategy->createCopy(&item);
69 
70  EXPECT_EQ(item.modelType(), copy->modelType());
71  EXPECT_EQ(copy->getItem("thickness")->data<double>(), property->data<double>());
72  EXPECT_FALSE(copy->getItem("thickness")->identifier() == property->identifier());
73  EXPECT_FALSE(item.identifier() == copy->identifier());
74 }
75 
76 //! Saving/restoring CustomItem.
77 
79 {
80  auto strategy = createCopyStrategy();
81 
82  const std::string model_type(Constants::BaseType);
83 
84  // creating parent with one child
85  auto parent = std::make_unique<SessionItem>(model_type);
86  parent->setDisplayName("parent_name");
87  parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);
88  auto child = new SessionItem(model_type);
89  child->setDisplayName("child_name");
90  parent->insertItem(child, TagRow::append());
91 
92  // creating copy
93  auto parent_copy = strategy->createCopy(parent.get());
94 
95  EXPECT_EQ(parent_copy->childrenCount(), 1);
96  EXPECT_EQ(parent_copy->modelType(), model_type);
97  EXPECT_EQ(parent_copy->displayName(), "parent_name");
98  EXPECT_EQ(parent_copy->itemTags()->defaultTag(), "defaultTag");
99  EXPECT_EQ(parent_copy->model(), nullptr);
100  EXPECT_FALSE(parent_copy->identifier() == parent->identifier());
101 
102  // checking child reconstruction
103  auto child_copy = parent_copy->getItem("defaultTag");
104  EXPECT_EQ(child_copy->parent(), parent_copy.get());
105  EXPECT_EQ(child_copy->childrenCount(), 0);
106  EXPECT_EQ(child_copy->modelType(), model_type);
107  EXPECT_EQ(child_copy->displayName(), "child_name");
108  EXPECT_EQ(child_copy->itemTags()->defaultTag(), "");
109  EXPECT_FALSE(child_copy->identifier() == child->identifier());
110 }
std::unique_ptr< JsonItemCopyStrategy > createCopyStrategy()
std::unique_ptr< ItemFactory > m_factory
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
T * addProperty(const std::string &name)
Adds property item of given type.
Definition: compounditem.h:43
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(JsonItemCopyStrategyTest, propertyItem)
Saving/restoring PropertyItem.
const model_type BaseType
Definition: mvvm_types.h:45
materialitems.h Collection of materials to populate MaterialModel.
@ copy
full deep copying with item identifiers regenerated
MVVM_MODEL_EXPORT std::unique_ptr< ItemCatalogue > CreateStandardItemCatalogue()
Creates a catalog of items supported by SessionModel out-of-the-box.
std::string model_type
Definition: types.h:23
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?