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

Implements class CLASS? More...

Include dependency graph for copyitemcommand.test.cpp:

Go to the source code of this file.

Classes

class  CopyItemCommandTest
 

Functions

 TEST_F (CopyItemCommandTest, copyChild)
 
 TEST_F (CopyItemCommandTest, invalidCopyAttempt)
 Attempt to copy item to invalid tag. 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 copyitemcommand.test.cpp.

Function Documentation

◆ TEST_F() [1/2]

TEST_F ( CopyItemCommandTest  ,
copyChild   
)

Definition at line 33 of file copyitemcommand.test.cpp.

34 {
35  SessionModel model;
36 
37  // parent with children and data
38  auto parent = model.insertItem<SessionItem>(model.rootItem(), {"", 0});
39  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
40 
41  auto child0 = model.insertItem<SessionItem>(parent, "tag1");
42  child0->setData(42.0);
43  auto child1 = model.insertItem<SessionItem>(parent, "tag1");
44  child1->setData(43.0);
45 
46  // making copy of child
47  auto command = std::make_unique<CopyItemCommand>(child1, parent, TagRow{"tag1", 1});
48  command->execute();
49 
50  // checking that parent has now three children
51  auto copy = std::get<SessionItem*>(command->result());
52  EXPECT_FALSE(command->isObsolete());
53  EXPECT_TRUE(copy != nullptr);
54  EXPECT_EQ(parent->childrenCount(), 3);
55  std::vector<SessionItem*> expected = {child0, copy, child1};
56  EXPECT_EQ(parent->getItems("tag1"), expected);
57  EXPECT_EQ(copy->data<double>(), 43.0);
58 
59  // undoing command
60  command->undo();
61  expected = {child0, child1};
62  EXPECT_EQ(parent->getItems("tag1"), expected);
63  EXPECT_FALSE(command->isObsolete());
64 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::vector< SessionItem * > getItems(const std::string &tag) const
Returns all children stored at given tag.
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
int childrenCount() const
Returns total number of children in all tags.
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
@ copy
full deep copying with item identifiers regenerated

References ModelView::SessionItem::childrenCount(), ModelView::copy, ModelView::SessionItem::getItems(), 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() [2/2]

TEST_F ( CopyItemCommandTest  ,
invalidCopyAttempt   
)

Attempt to copy item to invalid tag.

Definition at line 68 of file copyitemcommand.test.cpp.

69 {
70  SessionModel model;
71 
72  // parent with children and data
73  auto parent = model.insertItem<CompoundItem>(model.rootItem());
74  parent->addProperty("thickness", 42.0);
75  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
76 
77  auto child0 = model.insertItem<SessionItem>(parent);
78  child0->setData(42.0);
79 
80  // making copy of child
81  auto command = std::make_unique<CopyItemCommand>(child0, parent, TagRow{"thickness", 0});
82  command->execute();
83 
84  // checking that parent has now three children
85  EXPECT_TRUE(command->isObsolete());
86  EXPECT_EQ(std::get<SessionItem*>(command->result()), nullptr);
87 
88  // undoing of obsolete command is not possible
89  EXPECT_THROW(command->undo(), std::runtime_error);
90 }
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

References ModelView::CompoundItem::addProperty(), ModelView::SessionModel::insertItem(), ModelView::SessionModel::rootItem(), ModelView::SessionItem::setData(), and ModelView::TagInfo::universalTag().

Here is the call graph for this function: