BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itemcatalogue.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/itemcatalogue.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"
20 #include <stdexcept>
21 
22 using namespace ModelView;
23 
24 //! Testing ItemCatalogue construction
25 
26 class ItemCatalogueTest : public ::testing::Test {
27 public:
29 };
30 
32 
33 TEST_F(ItemCatalogueTest, initialState)
34 {
35  ItemCatalogue catalogue;
36  EXPECT_EQ(catalogue.itemCount(), 0);
37  EXPECT_EQ(catalogue.modelTypes(), std::vector<std::string>({}));
38  EXPECT_EQ(catalogue.labels(), std::vector<std::string>({}));
39 }
40 
42 {
43  ItemCatalogue catalogue;
44 
45  catalogue.registerItem<PropertyItem>();
46 
47  EXPECT_EQ(catalogue.itemCount(), 1);
48 
49  auto item = catalogue.create(Constants::PropertyType);
50  EXPECT_TRUE(dynamic_cast<PropertyItem*>(item.get()) != nullptr);
51 
52  // registration of second item is not allowed
53  EXPECT_THROW(catalogue.registerItem<PropertyItem>(), std::runtime_error);
54 
55  // item was not registered, creation not allowed
56  EXPECT_THROW(catalogue.create("non-registered"), std::runtime_error);
57 
58  // checking model types and labels
59  EXPECT_EQ(catalogue.modelTypes(), std::vector<std::string>({"Property"}));
60  EXPECT_EQ(catalogue.labels(), std::vector<std::string>({""}));
61 }
62 
63 TEST_F(ItemCatalogueTest, copyConstructor)
64 {
65  ItemCatalogue catalogue;
66  catalogue.registerItem<PropertyItem>();
67 
68  ItemCatalogue copy(catalogue);
69 
70  // creation of item using first catalogue
71  auto item = catalogue.create(Constants::PropertyType);
72  EXPECT_TRUE(dynamic_cast<PropertyItem*>(item.get()) != nullptr);
73 
74  // creation of item using catalogue copy
75  item = copy.create(Constants::PropertyType);
76  EXPECT_TRUE(dynamic_cast<PropertyItem*>(item.get()) != nullptr);
77 
78  // checking model types and labels in new catalogue
79  EXPECT_EQ(copy.modelTypes(), std::vector<std::string>({"Property"}));
80  EXPECT_EQ(copy.labels(), std::vector<std::string>({""}));
81 
82  // adding item to first catalogue but not the second
83  catalogue.registerItem<VectorItem>();
84  item = catalogue.create(Constants::VectorItemType);
85  EXPECT_TRUE(dynamic_cast<VectorItem*>(item.get()) != nullptr);
86 
87  // copy of catalogue knows nothing about new VectorType
88  EXPECT_THROW(copy.create(Constants::VectorItemType), std::runtime_error);
89 }
90 
91 TEST_F(ItemCatalogueTest, assignmentOperator)
92 {
93  ItemCatalogue catalogue;
94  catalogue.registerItem<PropertyItem>();
95 
97  copy = catalogue;
98 
99  // creation of item using first catalogue
100  auto item = catalogue.create(Constants::PropertyType);
101  EXPECT_TRUE(dynamic_cast<PropertyItem*>(item.get()) != nullptr);
102 
103  // creation of item using catalogue copy
104  item = copy.create(Constants::PropertyType);
105  EXPECT_TRUE(dynamic_cast<PropertyItem*>(item.get()) != nullptr);
106 }
107 
109 {
110  ItemCatalogue catalogue;
111  catalogue.registerItem<PropertyItem>();
112 
113  EXPECT_TRUE(catalogue.contains(Constants::PropertyType));
114  EXPECT_FALSE(catalogue.contains(Constants::VectorItemType));
115 }
116 
117 TEST_F(ItemCatalogueTest, defaultItemCatalogue)
118 {
119  auto catalogue = CreateStandardItemCatalogue();
120 
121  auto item = catalogue->create(Constants::BaseType);
122  EXPECT_TRUE(dynamic_cast<SessionItem*>(item.get()) != nullptr);
123 
124  item = catalogue->create(Constants::PropertyType);
125  EXPECT_TRUE(dynamic_cast<PropertyItem*>(item.get()) != nullptr);
126 
127  item = catalogue->create(Constants::VectorItemType);
128  EXPECT_TRUE(dynamic_cast<VectorItem*>(item.get()) != nullptr);
129 
130  item = catalogue->create(Constants::CompoundItemType);
131  EXPECT_TRUE(dynamic_cast<CompoundItem*>(item.get()) != nullptr);
132 }
133 
134 TEST_F(ItemCatalogueTest, addLabeledItem)
135 {
136  ItemCatalogue catalogue;
137  catalogue.registerItem<PropertyItem>("property");
138  catalogue.registerItem<VectorItem>("vector item");
139 
140  // checking model types and labels
141  EXPECT_EQ(catalogue.modelTypes(), std::vector<std::string>({"Property", "Vector"}));
142  EXPECT_EQ(catalogue.labels(), std::vector<std::string>({"property", "vector item"}));
143 }
144 
146 {
147  ItemCatalogue catalogue1;
148  catalogue1.registerItem<PropertyItem>("property");
149  catalogue1.registerItem<VectorItem>("vector");
150 
151  ItemCatalogue catalogue2;
152  catalogue2.registerItem<CompoundItem>("compound");
153 
154  // adding two catalogue together
155  catalogue1.merge(catalogue2);
156 
157  std::vector<std::string> expected_models = {Constants::PropertyType, Constants::VectorItemType,
159  std::vector<std::string> expected_labels = {"property", "vector", "compound"};
160 
161  EXPECT_EQ(catalogue1.modelTypes(), expected_models);
162  EXPECT_EQ(catalogue1.labels(), expected_labels);
163 
164  auto item = catalogue1.create(Constants::VectorItemType);
165  EXPECT_TRUE(dynamic_cast<VectorItem*>(item.get()) != nullptr);
166 
167  // duplications is not allowed
168  EXPECT_THROW(catalogue1.merge(catalogue2), std::runtime_error);
169 }
Testing ItemCatalogue construction.
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
Catalogue for item constructions.
Definition: itemcatalogue.h:30
std::vector< std::string > modelTypes() const
void registerItem(const std::string &label={})
Definition: itemcatalogue.h:60
std::vector< std::string > labels() const
void merge(const ItemCatalogue &other)
Adds content of other catalogue to this.
std::unique_ptr< SessionItem > create(const std::string &modelType) const
bool contains(const std::string &modelType) const
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
Vector item with three x,y,z property items.
Definition: vectoritem.h:24
Defines class CLASS?
Defines class CLASS?
TEST_F(ItemCatalogueTest, initialState)
Defines class CLASS?
const model_type PropertyType
Definition: mvvm_types.h:59
const model_type BaseType
Definition: mvvm_types.h:45
const model_type CompoundItemType
Definition: mvvm_types.h:48
const model_type VectorItemType
Definition: mvvm_types.h:61
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.
Defines class CLASS?
Defines class CLASS?