BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
standardchildrenstrategies.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/testviewmodel/standardchildrenstrategies.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/sessionitem.h"
19 #include "mvvm/model/taginfo.h"
22 #include "toyitems.h"
23 #include "toymodel.h"
24 
25 using namespace ModelView;
26 
27 class StandardChildrenStrategiesTest : public ::testing::Test {
28 public:
30 
31  //! Helper class with two properties and one top level item on board.
32  class TestItem : public CompoundItem {
33  public:
34  TestItem() : CompoundItem("test")
35  {
36  addProperty("length", 8.0);
37  registerTag(TagInfo::universalTag("children"), /*set_as_default*/ true);
38  insertItem(new SessionItem, TagRow::append());
39  addProperty("height", 12.0);
40  }
42  };
43 
44  struct ChildrenData {
45  std::string model_type;
46  std::string tag;
47  bool operator==(const ChildrenData& other) const
48  {
49  return model_type == other.model_type && tag == other.tag;
50  }
51  };
52 
53  std::vector<ChildrenData> children_data(std::vector<SessionItem*> children)
54  {
55  std::vector<ChildrenData> result;
56  for (auto child : children)
57  result.push_back({child->modelType(), child->tagRow().tag});
58  return result;
59  }
60 };
61 
64 
65 //! Testing AllChildrenStrategy.
66 
68 {
69  AllChildrenStrategy strategy;
70 
71  // nullptr
72  auto children = strategy.children(nullptr);
73  EXPECT_EQ(children.size(), 0);
74 
75  // empty item
76  SessionItem item1("model_type");
77  children = strategy.children(&item1);
78  EXPECT_EQ(children.size(), 0);
79 
80  // VectorItem
81  VectorItem item2;
82  children = strategy.children(&item2);
83  EXPECT_EQ(children.size(), 3);
84 
85  // CompoundItem
86  CompoundItem item3;
87  item3.addProperty("height", 42.0);
88  children = strategy.children(&item3);
89  EXPECT_EQ(children.size(), 1);
90 
91  // TestItem
92  TestItem item4;
93  children = strategy.children(&item4);
94  EXPECT_EQ(children.size(), 3);
95 
96  // GroupItem
99  children = strategy.children(&item5);
100  EXPECT_EQ(children.size(), 3); // number of registered children
101 }
102 
103 //! Testing TopItemsStrategy.
104 
106 {
107  TopItemsStrategy strategy;
108 
109  // nullptr
110  auto children = strategy.children(nullptr);
111  EXPECT_EQ(children.size(), 0);
112 
113  // empty item
114  SessionItem item1("model_type");
115  children = strategy.children(&item1);
116  EXPECT_EQ(children.size(), 0);
117 
118  // VectorItem
119  VectorItem item2;
120  children = strategy.children(&item2);
121  EXPECT_EQ(children.size(), 0);
122 
123  // CompoundItem
124  CompoundItem item3;
125  item3.addProperty("height", 42.0);
126  children = strategy.children(&item3);
127  EXPECT_EQ(children.size(), 0);
128 
129  // TestItem
130  TestItem item4;
131  children = strategy.children(&item4);
132  EXPECT_EQ(children.size(), 1);
133 
134  // GroupItem
137  children = strategy.children(&item5);
138  EXPECT_EQ(children.size(), 3); // number of registered children
139 }
140 
141 //! Testing PropertyItemsStrategy.
142 
144 {
145  PropertyItemsStrategy strategy;
146 
147  // nullptr
148  {
149  auto children = strategy.children(nullptr);
150  EXPECT_EQ(children.size(), 0);
151  }
152 
153  // empty item
154  {
155  SessionItem item("model_type");
156  auto children = strategy.children(&item);
157  EXPECT_EQ(children.size(), 0);
158  }
159 
160  // VectorItem
161  {
162  VectorItem item;
163  auto children = strategy.children(&item);
164  EXPECT_EQ(children.size(), 3);
165  }
166 
167  // CompoundItem
168  {
169  CompoundItem item;
170  item.addProperty("height", 42.0);
171  auto children = strategy.children(&item);
172  EXPECT_EQ(children.size(), 1);
173  }
174 
175  // TestItem
176  {
177  TestItem item;
178  auto children = strategy.children(&item);
179  EXPECT_EQ(children.size(), 2);
180  }
181 
182  // GroupItem
183  {
186  auto children = strategy.children(&item);
187  EXPECT_EQ(children.size(), 2);
188 
189  std::vector<ChildrenData> expected_children_data{
192  EXPECT_EQ(children_data(children), expected_children_data);
193  }
194 }
195 
196 //! Testing PropertyItemsFlatStrategy.
197 
199 {
200  PropertyItemsFlatStrategy strategy;
201 
202  // nullptr
203  {
204  auto children = strategy.children(nullptr);
205  EXPECT_EQ(children.size(), 0);
206  }
207 
208  // empty item
209  {
210  SessionItem item("model_type");
211  auto children = strategy.children(&item);
212  EXPECT_EQ(children.size(), 0);
213  }
214 
215  // VectorItem
216  {
217  VectorItem item;
218  auto children = strategy.children(&item);
219  EXPECT_EQ(children.size(), 3);
220  }
221 
222  // CompoundItem
223  {
224  CompoundItem item;
225  item.addProperty("height", 42.0);
226  auto children = strategy.children(&item);
227  EXPECT_EQ(children.size(), 1);
228  }
229 
230  // TestItem
231  {
232  TestItem item;
233  auto children = strategy.children(&item);
234  EXPECT_EQ(children.size(), 2);
235  }
236 
237  // GroupItem
238  {
241  auto children = strategy.children(&item);
242  EXPECT_EQ(children.size(), 2);
243 
244  std::vector<ChildrenData> expected_children_data{
247  EXPECT_EQ(children_data(children), expected_children_data);
248  }
249 
250  // ParticleItem
251  {
253  auto children = strategy.children(&item);
254  EXPECT_EQ(children.size(), 3);
255 
256  std::vector<ChildrenData> expected_children_data{
260 
261  EXPECT_EQ(children_data(children), expected_children_data);
262  }
263 }
Strategy to find children of given item: gives all actual children back.
std::vector< SessionItem * > children(const SessionItem *item) const override
Returns vector of children of given item.
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
void setCurrentType(const std::string &model_type)
Sets item corresponding to given model type.
Definition: groupitem.cpp:58
Strategy to find children of given item: flat alignment.
std::vector< SessionItem * > children(const SessionItem *item) const override
Returns vector of children of given item.
Strategy to find children of given item: only property item will be given, all top level items will b...
std::vector< SessionItem * > children(const SessionItem *item) const override
Returns vector of children of given item.
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 TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Strategy to find children of given item: only top level items will be given, all property items will ...
std::vector< SessionItem * > children(const SessionItem *item) const override
Returns vector of children of given item.
Vector item with three x,y,z property items.
Definition: vectoritem.h:24
Helper class with two properties and one top level item on board.
std::vector< ChildrenData > children_data(std::vector< SessionItem * > children)
static const std::string P_HEIGHT
Definition: toyitems.h:91
static const std::string P_RADIUS
Definition: toyitems.h:90
Represents a particle, with a position, and a selection of possible shapes.
Definition: toyitems.h:62
static const std::string P_SHAPES
Definition: toyitems.h:65
static const std::string P_POSITION
Definition: toyitems.h:64
Represents a group item holding a collection of shapes.
Definition: toyitems.h:119
static const std::string P_RADIUS
Definition: toyitems.h:100
Defines class CLASS?
Defines class CLASS?
const model_type PropertyType
Definition: mvvm_types.h:59
const model_type VectorItemType
Definition: mvvm_types.h:61
materialitems.h Collection of materials to populate MaterialModel.
std::string model_type
Definition: types.h:23
const ModelView::model_type CylinderItemType
Definition: toyitems.h:35
const ModelView::model_type ShapeGroupItemType
Definition: toyitems.h:39
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(StandardChildrenStrategiesTest, AllChildrenStrategy)
Testing AllChildrenStrategy.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?