BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionitemcontainer.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/sessionitemcontainer.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"
16 #include "mvvm/model/sessionitem.h"
18 #include "test_utils.h"
19 
20 using namespace ModelView;
21 
22 //! Tests for TestSessionItemContainer class.
23 
24 class SessionItemContainerTest : public ::testing::Test {
25 public:
27 };
28 
30 
31 //! Initial state of emty SessionItemTag.
32 
34 {
35  const std::string name("tag");
37 
38  EXPECT_EQ(tag.itemCount(), 0);
39  EXPECT_TRUE(tag.empty());
40  EXPECT_EQ(tag.name(), name);
41  EXPECT_EQ(tag.items(), std::vector<SessionItem*>());
42 }
43 
44 //! Checking ::insertItem.
45 
47 {
48  const std::string tag_name("tag");
50 
51  // inserting non-existing item is not allowed
52  EXPECT_FALSE(tag.insertItem(nullptr, tag.itemCount()));
53  EXPECT_EQ(tag.itemCount(), 0);
54 
55  // insertion at the end
56  SessionItem* child1 = new SessionItem;
57  SessionItem* child2 = new SessionItem;
58  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
59  EXPECT_TRUE(tag.insertItem(child2, tag.itemCount()));
60  EXPECT_EQ(tag.itemCount(), 2);
61  EXPECT_FALSE(tag.empty());
62  std::vector<SessionItem*> expected = {child1, child2};
63  EXPECT_EQ(tag.items(), expected);
64 
65  // insertion at the beginning
66  SessionItem* child3 = new SessionItem;
67  EXPECT_TRUE(tag.insertItem(child3, 0));
68  expected = {child3, child1, child2};
69  EXPECT_EQ(tag.items(), expected);
70 
71  // insertion in between
72  SessionItem* child4 = new SessionItem;
73  EXPECT_TRUE(tag.insertItem(child4, 1));
74  expected = {child3, child4, child1, child2};
75  EXPECT_EQ(tag.items(), expected);
76 
77  // using index equal to number of items
78  SessionItem* child5 = new SessionItem;
79  EXPECT_TRUE(tag.insertItem(child5, tag.itemCount()));
80  expected = {child3, child4, child1, child2, child5};
81  EXPECT_EQ(tag.items(), expected);
82 
83  // insertion with wrong index
84  SessionItem* child6 = new SessionItem;
85  EXPECT_FALSE(tag.insertItem(child6, 42));
86  EXPECT_EQ(tag.items(), expected);
87  delete child6;
88 }
89 
90 //! Checking ::insertItem when item has specific model type.
91 
92 TEST_F(SessionItemContainerTest, insertItemModelType)
93 {
94  const std::string tag_name("tag");
95  const std::vector<std::string> model_types = {"model_a"};
96 
97  SessionItemContainer tag(TagInfo::universalTag(tag_name, model_types));
98 
99  // insertion of wrong model type is not allowed
100  SessionItem* child1 = new SessionItem("model_a");
101  SessionItem* child2 = new SessionItem("model_b");
102  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
103  EXPECT_FALSE(tag.insertItem(child2, tag.itemCount()));
104  delete child2;
105 
106  std::vector<SessionItem*> expected = {child1};
107  EXPECT_EQ(tag.items(), expected);
108 }
109 
110 //! Checking ::insertItem when tag is related to property tag.
111 
112 TEST_F(SessionItemContainerTest, insertItemPropertyType)
113 {
114  const std::string name("tag");
115  const std::string property_type("Property");
116 
117  SessionItemContainer tag(TagInfo::propertyTag(name, property_type));
118 
119  // insertion of second property item is not allowed (because of reached maximum)
120  SessionItem* child1 = new SessionItem(property_type);
121  SessionItem* child2 = new SessionItem(property_type);
122  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
123  EXPECT_FALSE(tag.insertItem(child2, tag.itemCount()));
124  delete child2;
125 
126  // insertion of wrong model type is not allowed
127  SessionItem* child3 = new SessionItem("another_model");
128  EXPECT_FALSE(tag.insertItem(child3, tag.itemCount()));
129  delete child3;
130  std::vector<SessionItem*> expected = {child1};
131  EXPECT_EQ(tag.items(), expected);
132 }
133 
134 //! Checking ::indexOfItem.
135 
137 {
138  const std::string tag_name("tag");
139  const std::string model_type("model_a");
140 
142 
143  // index of two items
144  SessionItem* child1 = new SessionItem(model_type);
145  SessionItem* child2 = new SessionItem(model_type);
146  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
147  EXPECT_EQ(tag.indexOfItem(child1), 0);
148  EXPECT_TRUE(tag.insertItem(child2, tag.itemCount()));
149  EXPECT_EQ(tag.indexOfItem(child1), 0);
150  EXPECT_EQ(tag.indexOfItem(child2), 1);
151 
152  // not existing items
153  EXPECT_EQ(tag.indexOfItem(nullptr), -1);
154  auto child3 = std::make_unique<SessionItem>(model_type);
155  EXPECT_EQ(tag.indexOfItem(child3.get()), -1);
156 }
157 
158 //! Checking ::itemAt.
159 
161 {
162  const std::string tag_name("tag");
163  const std::string model_type("model_a");
164 
166 
167  // items at given indices
168  SessionItem* child1 = new SessionItem(model_type);
169  SessionItem* child2 = new SessionItem(model_type);
170  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
171  EXPECT_TRUE(tag.insertItem(child2, tag.itemCount()));
172  EXPECT_EQ(tag.itemAt(0), child1);
173  EXPECT_EQ(tag.itemAt(1), child2);
174 
175  // non-existing indices
176  EXPECT_EQ(tag.itemAt(2), nullptr);
177  EXPECT_EQ(tag.itemAt(3), nullptr);
178  EXPECT_EQ(tag.itemAt(-1), nullptr);
179 }
180 
181 //! Checking ::takeItem.
182 
184 {
185  const std::string tag_name("tag");
186  const std::string model_type("model_a");
187 
189 
190  // taking non existing items
191  EXPECT_EQ(tag.takeItem(0), nullptr);
192 
193  // inserting items
194  SessionItem* child1 = new SessionItem(model_type);
195  SessionItem* child2 = new SessionItem(model_type);
196  SessionItem* child3 = new SessionItem(model_type);
197  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
198  EXPECT_TRUE(tag.insertItem(child2, tag.itemCount()));
199  EXPECT_TRUE(tag.insertItem(child3, tag.itemCount()));
200 
201  // taking item in between
202  auto taken2 = tag.takeItem(1);
203  EXPECT_EQ(child2, taken2);
204  delete taken2;
205 
206  // order of remaining children
207  std::vector<SessionItem*> expected = {child1, child3};
208  EXPECT_EQ(tag.items(), expected);
209 
210  // taking non existing items
211  EXPECT_EQ(tag.takeItem(-1), nullptr);
212  EXPECT_EQ(tag.takeItem(tag.itemCount()), nullptr);
213 }
214 
215 //! Checking ::canTakeItem.
216 
218 {
219  const std::string tag_name("tag");
220  const std::string model_type("model_a");
221 
223 
224  // taking non existing items
225  EXPECT_FALSE(tag.canTakeItem(0));
226 
227  // inserting items
228  SessionItem* child1 = new SessionItem(model_type);
229  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
230  EXPECT_TRUE(tag.canTakeItem(0));
231 
232  // taking non existing items
233  EXPECT_FALSE(tag.canTakeItem(-1));
234  EXPECT_FALSE(tag.canTakeItem(tag.itemCount()));
235 }
236 
237 //! Checking ::canInsertItem.
238 
240 {
241  const std::string tag_name("tag");
242  const std::string model_type("model_a");
243 
245 
246  // inserting non-existing item
247  EXPECT_FALSE(tag.canInsertItem(nullptr, 0));
248 
249  // we should be allowed to insert valid child
250  auto child1 = std::make_unique<SessionItem>(model_type);
251  EXPECT_TRUE(tag.canInsertItem(child1.get(), 0));
252 
253  // wrong index is not allowed for insertion
254  EXPECT_FALSE(tag.canInsertItem(child1.get(), 1));
255  EXPECT_FALSE(tag.canInsertItem(child1.get(), -1));
256 
257  // inserting child
258  EXPECT_TRUE(tag.insertItem(child1.release(), tag.itemCount()));
259 
260  // can we insert second child?
261  auto child2 = std::make_unique<SessionItem>(model_type);
262  EXPECT_TRUE(tag.canInsertItem(child2.get(), 0));
263  EXPECT_TRUE(tag.canInsertItem(child2.get(), 1));
264  EXPECT_FALSE(tag.canInsertItem(child2.get(), 2));
265  EXPECT_FALSE(tag.canInsertItem(child2.get(), -1));
266 }
267 
268 //! Checking ::canInsertItem.
269 
270 TEST_F(SessionItemContainerTest, canInsertItemForPropertyTag)
271 {
272  const std::string name("tag");
273  const std::string property_type("Property");
274 
275  SessionItemContainer tag(TagInfo::propertyTag(name, property_type));
276 
277  // we should be allowed to insert valid child
278  auto child1 = std::make_unique<SessionItem>(property_type);
279  EXPECT_TRUE(tag.canInsertItem(child1.get(), 0));
280 
281  // inserting child
282  EXPECT_TRUE(tag.insertItem(child1.release(), tag.itemCount()));
283 
284  // second property shouldn't be posible to insert because of exceeded maximum
285  auto child2 = std::make_unique<SessionItem>(property_type);
286  EXPECT_FALSE(tag.canInsertItem(child2.get(), 0));
287 }
288 
289 //! Checking ::takeItem when tag is related to property tag.
290 
291 TEST_F(SessionItemContainerTest, takeItemPropertyType)
292 {
293  const std::string name("tag");
294  const std::string property_type("Property");
295 
296  SessionItemContainer tag(TagInfo::propertyTag(name, property_type));
297 
298  // insertion of second property item is not allowed (because of reached maximum)
299  SessionItem* child1 = new SessionItem(property_type);
300  EXPECT_TRUE(tag.insertItem(child1, tag.itemCount()));
301 
302  // attempt to take property item
303  EXPECT_FALSE(tag.canTakeItem(0));
304  EXPECT_EQ(tag.takeItem(0), nullptr);
305 }
Holds collection of SessionItem objects related to the same tag.
bool canInsertItem(const SessionItem *item, int index) const
Returns true if given item can be inserted under given index.
bool insertItem(SessionItem *item, int index)
Inserts item in a vector of children at given index, returns true in the case of success.
std::vector< SessionItem * > items() const
Returns vector of items in this container.
int itemCount() const
Returns number of items in given tag.
int indexOfItem(const SessionItem *item) const
Returns index of item in vector of items.
SessionItem * itemAt(int index) const
Returns item at given index. Returns nullptr if index is invalid.
SessionItem * takeItem(int index)
Removes item at given index and returns it to the user.
bool canTakeItem(int index) const
Returns true if item can be taken.
std::string name() const
Returns the name of SessionItemTag.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
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 TagInfo propertyTag(std::string name, std::string model_type)
Constructs tag intended for single property.
Definition: taginfo.cpp:40
Tests for TestSessionItemContainer class.
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::string model_type
Definition: types.h:23
QString const & name(EShape k)
Definition: particles.cpp:21
Defines class CLASS?
Defines class CLASS?
TEST_F(SessionItemContainerTest, initialState)
Initial state of emty SessionItemTag.
Defines class CLASS?