BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
removeitemcommand.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for removeitemcommand.test.cpp:

Go to the source code of this file.

Classes

class  RemoveItemCommandTest
 

Functions

 TEST_F (RemoveItemCommandTest, attemptToRemoveItem)
 Attempt to remove property item. More...
 
 TEST_F (RemoveItemCommandTest, removeAtCommand)
 
 TEST_F (RemoveItemCommandTest, removeAtCommandChild)
 
 TEST_F (RemoveItemCommandTest, removeAtCommandMultitag)
 RemoveAtCommand in multitag context. More...
 
 TEST_F (RemoveItemCommandTest, removeAtCommandParentWithChild)
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file removeitemcommand.test.cpp.

Function Documentation

◆ TEST_F() [1/5]

TEST_F ( RemoveItemCommandTest  ,
attemptToRemoveItem   
)

Attempt to remove property item.

Definition at line 170 of file removeitemcommand.test.cpp.

171 {
172  SessionModel model;
173  auto parent = model.insertItem<CompoundItem>(model.rootItem());
174  parent->addProperty("thickness", 42.0);
175 
176  auto command = std::make_unique<RemoveItemCommand>(parent, TagRow{"thickness", 0});
177  command->execute();
178 
179  EXPECT_TRUE(command->isObsolete());
180  EXPECT_EQ(std::get<bool>(command->result()), false);
181 }
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
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
SessionItem * rootItem() const
Returns root item of the model.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25

References ModelView::CompoundItem::addProperty(), ModelView::SessionModel::insertItem(), and ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST_F() [2/5]

TEST_F ( RemoveItemCommandTest  ,
removeAtCommand   
)

Definition at line 32 of file removeitemcommand.test.cpp.

33 {
34  SessionModel model;
35  auto item = model.insertItem<SessionItem>(model.rootItem());
36 
37  auto item_identifier = item->identifier();
38 
39  // command to insert parent in the model
40  auto command = std::make_unique<RemoveItemCommand>(model.rootItem(), TagRow{"", 0});
41  command->execute(); // removal
42 
43  EXPECT_EQ(std::get<bool>(command->result()), true);
44  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
45  EXPECT_FALSE(command->isObsolete());
46 
47  // undo command
48  command->undo();
49  EXPECT_FALSE(command->isObsolete());
50  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
51  auto restored = Utils::ChildAt(model.rootItem(), 0);
52  EXPECT_EQ(restored->identifier(), item_identifier);
53 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
int childrenCount() const
Returns total number of children in all tags.
MVVM_MODEL_EXPORT SessionItem * ChildAt(const SessionItem *parent, int index)
Returns child at given index of parent.
Definition: itemutils.cpp:70

References ModelView::Utils::ChildAt(), ModelView::SessionItem::childrenCount(), ModelView::SessionItem::identifier(), ModelView::SessionModel::insertItem(), and ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST_F() [3/5]

TEST_F ( RemoveItemCommandTest  ,
removeAtCommandChild   
)

Definition at line 55 of file removeitemcommand.test.cpp.

56 {
57  SessionModel model;
58  auto parent = model.insertItem<SessionItem>(model.rootItem());
59  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
60 
61  auto child1 = model.insertItem<SessionItem>(parent);
62  child1->setData(42.0);
63  model.insertItem<SessionItem>(parent);
64 
65  auto child1_identifier = child1->identifier();
66 
67  // command to remove one child
68  auto command = std::make_unique<RemoveItemCommand>(parent, TagRow{"", 0});
69  command->execute(); // removal
70 
71  // check that one child was removed
72  EXPECT_FALSE(command->isObsolete());
73  EXPECT_EQ(std::get<bool>(command->result()), true);
74  EXPECT_EQ(parent->childrenCount(), 1);
75 
76  // undo command
77  command->undo();
78  EXPECT_FALSE(command->isObsolete());
79  EXPECT_EQ(parent->childrenCount(), 2);
80  auto restored = Utils::ChildAt(parent, 0);
81  EXPECT_EQ(restored->identifier(), child1_identifier);
82 
83  // checking the data of restored item
84  EXPECT_EQ(restored->data<double>(), 42.0);
85 }
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141

References ModelView::Utils::ChildAt(), ModelView::SessionItem::identifier(), ModelView::SessionModel::insertItem(), ModelView::SessionItem::registerTag(), ModelView::SessionModel::rootItem(), ModelView::SessionItem::setData(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [4/5]

TEST_F ( RemoveItemCommandTest  ,
removeAtCommandMultitag   
)

RemoveAtCommand in multitag context.

Definition at line 124 of file removeitemcommand.test.cpp.

125 {
126  SessionModel model;
127  auto parent = model.insertItem<SessionItem>(model.rootItem());
128  parent->registerTag(TagInfo::universalTag("tag1"));
129  parent->registerTag(TagInfo::universalTag("tag2"));
130 
131  auto child1 = model.insertItem<SessionItem>(parent, "tag1");
132  child1->setData(41.0);
133 
134  auto child2 = model.insertItem<SessionItem>(parent, "tag1");
135  child2->setData(42.0);
136 
137  auto child3 = model.insertItem<SessionItem>(parent, "tag2");
138  child3->setData(43.0);
139 
140  auto parent_identifier = parent->identifier();
141  auto child1_identifier = child1->identifier();
142  auto child2_identifier = child2->identifier();
143  auto child3_identifier = child3->identifier();
144 
145  // command to remove parent
146  auto command = std::make_unique<RemoveItemCommand>(parent, TagRow{"tag1", 1});
147  command->execute(); // removal
148 
149  // check that one child was removed
150  EXPECT_FALSE(command->isObsolete());
151  EXPECT_EQ(std::get<bool>(command->result()), true);
152  EXPECT_EQ(parent->childrenCount(), 2);
153 
154  // undo command
155  command->undo();
156  EXPECT_FALSE(command->isObsolete());
157  EXPECT_EQ(parent->childrenCount(), 3);
158  auto restored_parent = Utils::ChildAt(model.rootItem(), 0);
159  auto restored_child2 = Utils::ChildAt(restored_parent, 1);
160 
161  EXPECT_EQ(restored_parent->identifier(), parent_identifier);
162  EXPECT_EQ(restored_child2->identifier(), child2_identifier);
163 
164  // checking the data of restored item
165  EXPECT_EQ(restored_child2->data<double>(), 42.0);
166 }

References ModelView::Utils::ChildAt(), ModelView::SessionItem::childrenCount(), ModelView::SessionItem::identifier(), ModelView::SessionModel::insertItem(), ModelView::SessionItem::registerTag(), ModelView::SessionModel::rootItem(), ModelView::SessionItem::setData(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [5/5]

TEST_F ( RemoveItemCommandTest  ,
removeAtCommandParentWithChild   
)

Definition at line 87 of file removeitemcommand.test.cpp.

88 {
89  SessionModel model;
90  auto parent = model.insertItem<SessionItem>(model.rootItem());
91  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
92 
93  auto child1 = model.insertItem<SessionItem>(parent);
94  child1->setData(42.0);
95 
96  auto parent_identifier = parent->identifier();
97  auto child1_identifier = child1->identifier();
98 
99  // command to remove parent
100  auto command = std::make_unique<RemoveItemCommand>(model.rootItem(), TagRow{"", 0});
101  command->execute(); // removal
102  EXPECT_FALSE(command->isObsolete());
103 
104  // check that one child was removed
105  EXPECT_EQ(std::get<bool>(command->result()), true);
106  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
107 
108  // undo command
109  command->undo();
110  EXPECT_FALSE(command->isObsolete());
111  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
112  auto restored_parent = Utils::ChildAt(model.rootItem(), 0);
113  auto restored_child = Utils::ChildAt(restored_parent, 0);
114 
115  EXPECT_EQ(restored_parent->identifier(), parent_identifier);
116  EXPECT_EQ(restored_child->identifier(), child1_identifier);
117 
118  // checking the data of restored item
119  EXPECT_EQ(restored_child->data<double>(), 42.0);
120 }

References ModelView::Utils::ChildAt(), ModelView::SessionItem::childrenCount(), ModelView::SessionModel::insertItem(), ModelView::SessionItem::registerTag(), ModelView::SessionModel::rootItem(), ModelView::SessionItem::setData(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function: