BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
compounditem.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/compounditem.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/itemutils.h"
20 #include "test_utils.h"
21 #include <memory>
22 #include <stdexcept>
23 
24 using namespace ModelView;
25 
26 namespace {
27 const std::string property_name("name");
28 }
29 
30 //! Test of CompountItem machinery (property children etc).
31 
32 class CompoundItemTest : public ::testing::Test {
33 };
34 
35 TEST_F(CompoundItemTest, initialState)
36 {
37  CompoundItem item;
38  EXPECT_EQ(item.childrenCount(), 0);
39 }
40 
41 TEST_F(CompoundItemTest, addIntProperty)
42 {
43  CompoundItem item;
44 
45  const int expected = 42;
46  auto propertyItem = item.addProperty(property_name, expected);
47  EXPECT_TRUE(Utils::HasTag(item, "name"));
48 
49  EXPECT_EQ(propertyItem->modelType(), Constants::PropertyType);
50  EXPECT_TRUE(Utils::IsIntVariant(propertyItem->data<QVariant>()));
51  EXPECT_EQ(propertyItem->displayName(), property_name);
52  EXPECT_EQ(propertyItem->data<int>(), expected);
53 
54  EXPECT_FALSE(propertyItem->data<QVariant>(ItemDataRole::LIMITS).isValid());
55 }
56 
57 TEST_F(CompoundItemTest, setIntProperty)
58 {
59  CompoundItem item;
60  auto propertyItem = item.addProperty(property_name, 41);
61 
62  const int expected = 42;
63  item.setProperty(property_name, expected);
64 
65  EXPECT_EQ(item.property<int>(property_name), expected);
66  EXPECT_EQ(propertyItem->data<int>(), expected);
67 }
68 
69 TEST_F(CompoundItemTest, addDoubleProperty)
70 {
71  CompoundItem item;
72 
73  const double expected = 42.1;
74  auto propertyItem = item.addProperty(property_name, expected);
75  EXPECT_TRUE(Utils::HasTag(item, property_name));
76 
77  EXPECT_EQ(propertyItem->modelType(), Constants::PropertyType);
78  EXPECT_TRUE(Utils::IsDoubleVariant(propertyItem->data<QVariant>()));
79  EXPECT_EQ(propertyItem->displayName(), property_name);
80  EXPECT_EQ(propertyItem->data<double>(), expected);
81 
82  EXPECT_TRUE(propertyItem->data<QVariant>(ItemDataRole::LIMITS).isValid());
83 
84  // limits should be "negative 'unlimited' by default
85  auto limits = propertyItem->data<RealLimits>(ItemDataRole::LIMITS);
86  EXPECT_FALSE(limits.hasLowerLimit());
87  EXPECT_FALSE(limits.hasUpperLimit());
88 }
89 
90 TEST_F(CompoundItemTest, setDoubleProperty)
91 {
92  CompoundItem item;
93  auto propertyItem = item.addProperty(property_name, 41.11);
94 
95  const double expected = 42.0;
96  item.setProperty(property_name, expected);
97 
98  EXPECT_EQ(item.property<double>(property_name), expected);
99  EXPECT_EQ(propertyItem->data<double>(), expected);
100 }
101 
102 TEST_F(CompoundItemTest, addCharProperty)
103 {
104  CompoundItem item;
105 
106  auto propertyItem = item.addProperty(property_name, "abc");
107  EXPECT_TRUE(Utils::HasTag(item, property_name));
108 
109  EXPECT_EQ(propertyItem->modelType(), Constants::PropertyType);
110  EXPECT_TRUE(Utils::IsStdStringVariant(propertyItem->data<QVariant>()));
111  EXPECT_EQ(propertyItem->data<std::string>(), std::string("abc"));
112 
113  EXPECT_FALSE(propertyItem->data<QVariant>(ItemDataRole::LIMITS).isValid());
114 }
115 
116 TEST_F(CompoundItemTest, setCharProperty)
117 {
118  CompoundItem item;
119  auto propertyItem = item.addProperty(property_name, "aaa");
120 
121  const char* expected{"bbb"};
122  item.setProperty(property_name, expected);
123 
124  EXPECT_EQ(item.property<std::string>(property_name), std::string(expected));
125  EXPECT_EQ(propertyItem->data<std::string>(), std::string(expected));
126 }
127 
128 TEST_F(CompoundItemTest, addStringProperty)
129 {
130  CompoundItem item;
131 
132  auto propertyItem = item.addProperty(property_name, std::string("abc"));
133  EXPECT_TRUE(Utils::HasTag(item, property_name));
134 
135  EXPECT_EQ(propertyItem->modelType(), Constants::PropertyType);
136  EXPECT_TRUE(Utils::IsStdStringVariant(propertyItem->data<QVariant>()));
137  EXPECT_EQ(propertyItem->data<std::string>(), std::string("abc"));
138 
139  EXPECT_FALSE(propertyItem->data<QVariant>(ItemDataRole::LIMITS).isValid());
140 }
141 
142 TEST_F(CompoundItemTest, setStringProperty)
143 {
144  CompoundItem item;
145  auto propertyItem = item.addProperty(property_name, std::string("aaa"));
146 
147  const std::string expected{"bbb"};
148  item.setProperty(property_name, expected);
149 
150  EXPECT_EQ(item.property<std::string>(property_name), expected);
151  EXPECT_EQ(propertyItem->data<std::string>(), expected);
152 }
153 
154 TEST_F(CompoundItemTest, addBoolProperty)
155 {
156  CompoundItem item;
157 
158  const bool expected = true;
159  auto propertyItem = item.addProperty(property_name, expected);
160  EXPECT_TRUE(Utils::HasTag(item, property_name));
161 
162  EXPECT_EQ(propertyItem->modelType(), Constants::PropertyType);
163  EXPECT_TRUE(Utils::IsBoolVariant(propertyItem->data<QVariant>()));
164  EXPECT_EQ(propertyItem->data<bool>(), expected);
165 
166  EXPECT_FALSE(propertyItem->data<QVariant>(ItemDataRole::LIMITS).isValid());
167 }
168 
169 TEST_F(CompoundItemTest, setBoolProperty)
170 {
171  CompoundItem item;
172  auto propertyItem = item.addProperty(property_name, false);
173 
174  const bool expected = true;
175  item.setProperty(property_name, expected);
176 
177  EXPECT_EQ(item.property<bool>(property_name), expected);
178  EXPECT_EQ(propertyItem->data<bool>(), expected);
179 }
180 
182 {
183  const std::string tag = "tag";
184 
185  // creating parent with one tag
186  SessionItem parent;
187  parent.registerTag(TagInfo::universalTag(tag));
188 
189  // inserting two children
190  auto property = new PropertyItem;
191  parent.insertItem(property, {tag, 0});
192 
193  EXPECT_TRUE(parent.item<PropertyItem>(tag) == property);
194  EXPECT_THROW(parent.item<CompoundItem>(tag), std::runtime_error);
195 }
196 
197 TEST_F(CompoundItemTest, itemVectorAccess)
198 {
199  const std::string tag = "tag";
200 
201  // creating parent with one tag
202  SessionItem parent;
203  parent.registerTag(TagInfo::universalTag(tag));
204 
205  // inserting two children
206  auto property1 = new PropertyItem;
207  auto property2 = new PropertyItem;
208  parent.insertItem(property1, TagRow::append(tag));
209  parent.insertItem(property2, TagRow::append(tag));
210 
211  auto items = parent.items<PropertyItem>(tag);
212  std::vector<PropertyItem*> expected = {property1, property2};
213  EXPECT_EQ(items, expected);
214  EXPECT_EQ(parent.items<CompoundItem>(tag).size(), 0);
215 }
216 
217 //! Tests automatic index addition to default display name.
218 
219 TEST_F(CompoundItemTest, displayNameIndexAddition)
220 {
221  const std::string tag = "tag";
222 
223  // creating parent with one tag
224  SessionItem parent;
225  parent.registerTag(TagInfo::universalTag(tag));
226 
227  // inserting two children
228  auto child0 = new CompoundItem;
229  parent.insertItem(child0, TagRow::append(tag));
230  auto child1 = new CompoundItem;
231  parent.insertItem(child1, TagRow::append(tag));
232 
233  // Default display names of items of the same type should have indices
234  EXPECT_EQ(child0->displayName(), Constants::CompoundItemType + "0");
235  EXPECT_EQ(child1->displayName(), Constants::CompoundItemType + "1");
236 
237  // however, if children have custom display name, they should remain intact
238  child0->setDisplayName("Jekyll");
239  child1->setDisplayName("Hyde");
240  EXPECT_EQ(child0->displayName(), "Jekyll");
241  EXPECT_EQ(child1->displayName(), "Hyde");
242 }
243 
244 //! Test all children method.
245 //! Property items are also children.
246 
248 {
249  CompoundItem item;
250  EXPECT_TRUE(item.children().empty());
251  auto propertyItem = item.addProperty(property_name, false);
252  EXPECT_EQ(item.children(), std::vector<SessionItem*>({propertyItem}));
253 }
Test of CompountItem machinery (property children etc).
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
Limits for double.
Definition: reallimits.h:25
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
std::vector< T * > items(const std::string &tag) const
Returns all items under given tag casted to specific type.
Definition: sessionitem.h:169
int childrenCount() const
Returns total number of children in all tags.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
bool insertItem(SessionItem *item, const TagRow &tagrow)
Insert item into given tag under the given row.
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
void setProperty(const std::string &tag, const T &value)
Sets value to property item.
Definition: sessionitem.h:190
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?
TEST_F(CompoundItemTest, initialState)
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const model_type PropertyType
Definition: mvvm_types.h:59
const model_type CompoundItemType
Definition: mvvm_types.h:48
const int LIMITS
possibly limits on item's data
Definition: mvvm_types.h:33
MVVM_MODEL_EXPORT bool IsIntVariant(const Variant &variant)
Returns true in the case of double value based variant.
MVVM_MODEL_EXPORT bool HasTag(const SessionItem &item, const std::string &tag)
Returns true if given item has registered tag.
Definition: itemutils.cpp:86
MVVM_MODEL_EXPORT bool IsDoubleVariant(const Variant &variant)
Returns true in the case of double value based variant.
MVVM_MODEL_EXPORT bool IsStdStringVariant(const Variant &variant)
Returns true in the case of double value based variant.
MVVM_MODEL_EXPORT bool IsBoolVariant(const Variant &variant)
Returns true in the case of double value based variant.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?