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

Implements class CLASS? More...

Include dependency graph for insertnewitemcommand.test.cpp:

Go to the source code of this file.

Classes

class  InsertNewItemCommandTest
 

Functions

 TEST_F (InsertNewItemCommandTest, attemptInsertSecondProperty)
 Attempt to insert second property to the compount item. More...
 
 TEST_F (InsertNewItemCommandTest, attemptToExecuteTwice)
 Attempt to execute command twice. More...
 
 TEST_F (InsertNewItemCommandTest, attemptToUndoTwice)
 Attempt to undo command twice. More...
 
 TEST_F (InsertNewItemCommandTest, insertNewItemCommand)
 Insert new item through InsertNewItemCommand command. More...
 
 TEST_F (InsertNewItemCommandTest, insertNewItemWithTagCommand)
 Insert new item through InsertNewItemCommand command. More...
 
 TEST_F (InsertNewItemCommandTest, insertNewPropertyItemIdInPool)
 Insert new item through InsertNewItemCommand command. More...
 
 TEST_F (InsertNewItemCommandTest, insertNewPropertyItemPreservedId)
 Insert new item through InsertNewItemCommand command. More...
 

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 insertnewitemcommand.test.cpp.

Function Documentation

◆ TEST_F() [1/7]

TEST_F ( InsertNewItemCommandTest  ,
attemptInsertSecondProperty   
)

Attempt to insert second property to the compount item.

Definition at line 131 of file insertnewitemcommand.test.cpp.

132 {
133  SessionModel model;
134  auto parent = model.insertItem<CompoundItem>();
135  parent->registerTag(TagInfo::propertyTag("radius", Constants::PropertyType));
136 
137  // command to insert second property
138  auto factory_func = [parent]() {
139  return parent->model()->factory()->createItem(Constants::PropertyType);
140  };
141 
142  // adding property to another tag is valid
143  InsertNewItemCommand command1(factory_func, parent, TagRow{"radius", -1});
144  EXPECT_NO_THROW(command1.execute());
145  EXPECT_FALSE(command1.isObsolete());
146  EXPECT_EQ(std::get<SessionItem*>(command1.result()), parent->getItem("radius"));
147 
148  // adding second property to the same tag is not possible. Command should be in obsolete state
149  InsertNewItemCommand command2(factory_func, parent, TagRow{"radius", -1});
150  EXPECT_NO_THROW(command2.execute());
151  EXPECT_TRUE(command2.isObsolete());
152  EXPECT_EQ(std::get<SessionItem*>(command2.result()), nullptr);
153 
154  // undoing failed command shouldn't be possible
155  EXPECT_THROW(command2.undo(), std::runtime_error);
156 }
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
Command for unddo/redo to insert new item.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
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
const model_type PropertyType
Definition: mvvm_types.h:59

References ModelView::SessionModel::insertItem(), ModelView::TagInfo::propertyTag(), ModelView::Constants::PropertyType, and ModelView::SessionItem::registerTag().

Here is the call graph for this function:

◆ TEST_F() [2/7]

TEST_F ( InsertNewItemCommandTest  ,
attemptToExecuteTwice   
)

Attempt to execute command twice.

Definition at line 103 of file insertnewitemcommand.test.cpp.

104 {
105  SessionModel model;
106  // command to set same value
107  auto command = create_command(model.rootItem(), "", 0);
108 
109  // executing command
110  command->execute();
111  EXPECT_THROW(command->execute(), std::runtime_error);
112 }
SessionItem * rootItem() const
Returns root item of the model.

References ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST_F() [3/7]

TEST_F ( InsertNewItemCommandTest  ,
attemptToUndoTwice   
)

Attempt to undo command twice.

Definition at line 116 of file insertnewitemcommand.test.cpp.

117 {
118  SessionModel model;
119 
120  // command to set same value
121  auto command = create_command(model.rootItem(), "", 0);
122 
123  // executing command
124  command->execute();
125  command->undo();
126  EXPECT_THROW(command->undo(), std::runtime_error);
127 }

References ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST_F() [4/7]

TEST_F ( InsertNewItemCommandTest  ,
insertNewItemCommand   
)

Insert new item through InsertNewItemCommand command.

Definition at line 45 of file insertnewitemcommand.test.cpp.

46 {
47  SessionModel model;
48 
49  // command to insert item in a model
50  auto command = create_command(model.rootItem(), "", 0);
51 
52  // executing command
53  command->execute();
54  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
55  EXPECT_EQ(std::get<SessionItem*>(command->result()), model.rootItem()->getItem("", 0));
56  EXPECT_EQ(command->isObsolete(), false);
57 
58  // undoing command
59  command->undo();
60  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
61  EXPECT_EQ(std::get<SessionItem*>(command->result()), nullptr);
62  EXPECT_EQ(command->isObsolete(), false);
63 
64  // executing again
65  command->execute();
66  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
67  EXPECT_EQ(std::get<SessionItem*>(command->result()), model.rootItem()->getItem("", 0));
68  EXPECT_EQ(command->isObsolete(), false);
69 }
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
int childrenCount() const
Returns total number of children in all tags.

References ModelView::SessionItem::childrenCount(), ModelView::SessionItem::getItem(), and ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST_F() [5/7]

TEST_F ( InsertNewItemCommandTest  ,
insertNewItemWithTagCommand   
)

Insert new item through InsertNewItemCommand command.

Definition at line 73 of file insertnewitemcommand.test.cpp.

74 {
75  SessionModel model;
76 
77  // command to insert parent in the model
78  auto command1 = create_command(model.rootItem(), "", 0);
79  command1->execute(); // insertion
80  EXPECT_EQ(command1->isObsolete(), false);
81 
82  auto parent = std::get<SessionItem*>(command1->result());
83  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
84  EXPECT_EQ(parent->childrenCount(), 0);
85 
86  // command to insert child
87  auto command2 = create_command(parent, "tag1", 0);
88  command2->execute(); // insertion
89  EXPECT_EQ(command2->isObsolete(), false);
90 
91  EXPECT_EQ(parent->childrenCount(), 1);
92  EXPECT_EQ(Utils::ChildAt(parent, 0), std::get<SessionItem*>(command2->result()));
93 
94  // undoing command
95  command2->undo();
96  EXPECT_EQ(parent->childrenCount(), 0);
97  EXPECT_EQ(nullptr, std::get<SessionItem*>(command2->result()));
98  EXPECT_EQ(command2->isObsolete(), false);
99 }
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::SessionModel::rootItem(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function:

◆ TEST_F() [6/7]

TEST_F ( InsertNewItemCommandTest  ,
insertNewPropertyItemIdInPool   
)

Insert new item through InsertNewItemCommand command.

We validate that undoing, and then redoing, would restore very first unique identifier. Same as above, but we additionally controling item pool.

Definition at line 187 of file insertnewitemcommand.test.cpp.

188 {
189  auto pool = std::make_shared<ItemPool>();
190  SessionModel model("Model", pool);
191  // command to insert second property
192  auto factory_func = [&model]() { return model.factory()->createItem(Constants::PropertyType); };
193 
194  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
195  EXPECT_EQ(pool->size(), 1); // rootItem
196 
197  InsertNewItemCommand command1(factory_func, model.rootItem(), TagRow{"", 0});
198  command1.execute();
199 
200  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
201  auto orig_identifier = model.rootItem()->children()[0]->identifier();
202  EXPECT_EQ(pool->size(), 2);
203 
204  command1.undo();
205  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
206 
207  command1.execute();
208  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
209  auto restored_item = model.rootItem()->children()[0];
210  EXPECT_EQ(restored_item->identifier(), orig_identifier);
211  EXPECT_EQ(model.findItem(orig_identifier), restored_item);
212  EXPECT_EQ(pool->item_for_key(orig_identifier), restored_item);
213 }

References ModelView::SessionItem::children(), ModelView::SessionItem::childrenCount(), ModelView::ItemFactoryInterface::createItem(), ModelView::AbstractItemCommand::execute(), ModelView::SessionModel::factory(), ModelView::SessionModel::findItem(), ModelView::Constants::PropertyType, ModelView::SessionModel::rootItem(), and ModelView::AbstractItemCommand::undo().

Here is the call graph for this function:

◆ TEST_F() [7/7]

TEST_F ( InsertNewItemCommandTest  ,
insertNewPropertyItemPreservedId   
)

Insert new item through InsertNewItemCommand command.

We validate that undoing, and then redoing, would restore very first unique identifier.

Definition at line 161 of file insertnewitemcommand.test.cpp.

162 {
163  SessionModel model;
164  // command to insert second property
165  auto factory_func = [&model]() { return model.factory()->createItem(Constants::PropertyType); };
166 
167  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
168 
169  InsertNewItemCommand command1(factory_func, model.rootItem(), TagRow{"", 0});
170  command1.execute();
171 
172  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
173  auto orig_identifier = model.rootItem()->children()[0]->identifier();
174 
175  command1.undo();
176  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
177 
178  command1.execute();
179  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
180  EXPECT_EQ(model.rootItem()->children()[0]->identifier(), orig_identifier);
181 }
virtual std::unique_ptr< SessionItem > createItem(const model_type &modelType) const =0
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
const ItemFactoryInterface * factory() const
Returns item factory which can generate all items supported by this model.

References ModelView::SessionItem::children(), ModelView::SessionItem::childrenCount(), ModelView::ItemFactoryInterface::createItem(), ModelView::AbstractItemCommand::execute(), ModelView::SessionModel::factory(), ModelView::Constants::PropertyType, ModelView::SessionModel::rootItem(), and ModelView::AbstractItemCommand::undo().

Here is the call graph for this function: