BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
copyitemcommand.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/copyitemcommand.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/itemutils.h"
19 #include "mvvm/model/sessionitem.h"
21 #include "mvvm/model/taginfo.h"
22 #include <stdexcept>
23 
24 using namespace ModelView;
25 
26 class CopyItemCommandTest : public ::testing::Test {
27 public:
29 };
30 
32 
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 }
65 
66 //! Attempt to copy item to invalid tag
67 
68 TEST_F(CopyItemCommandTest, invalidCopyAttempt)
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
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
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
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
Defines class CLASS?
Defines class CLASS?
TEST_F(CopyItemCommandTest, copyChild)
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
@ copy
full deep copying with item identifiers regenerated
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?