BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
setvaluecommand.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/setvaluecommand.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"
17 #include "mvvm/model/sessionitem.h"
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
23 class SetValueCommandTest : public ::testing::Test {
24 public:
26 };
27 
29 
30 //! Set item value through SetValueCommand command.
31 
32 TEST_F(SetValueCommandTest, setValueCommand)
33 {
34  SessionModel model;
35  const int role = ItemDataRole::DATA;
36 
37  // inserting single item
38  auto item = model.insertItem<SessionItem>();
39  EXPECT_FALSE(model.data(item, role).isValid());
40 
41  QVariant expected(42.0);
42  auto command = std::make_unique<SetValueCommand>(item, expected, role);
43 
44  // executing command
45  command->execute();
46  EXPECT_TRUE(std::get<bool>(command->result())); // value was changed
47  EXPECT_EQ(command->isObsolete(), false);
48  EXPECT_EQ(model.data(item, role), expected);
49 
50  // undoing command
51  command->undo();
52  EXPECT_TRUE(std::get<bool>(command->result())); // value was changed
53  EXPECT_FALSE(model.data(item, role).isValid());
54  EXPECT_EQ(command->isObsolete(), false);
55 }
56 
57 //! Set same item value through SetValueCommand command.
58 
59 TEST_F(SetValueCommandTest, setSameValueCommand)
60 {
61  SessionModel model;
62  const int role = ItemDataRole::DATA;
63 
64  // inserting single item
65  auto item = model.insertItem<SessionItem>();
66  QVariant expected(42.0);
67  item->setData(expected, role);
68 
69  // command to set same value
70  auto command = std::make_unique<SetValueCommand>(item, expected, role);
71 
72  // executing command
73  command->execute();
74  EXPECT_FALSE(std::get<bool>(command->result())); // value wasn't changed
75  EXPECT_EQ(model.data(item, role), expected);
76  EXPECT_EQ(command->isObsolete(), true);
77 
78  // undoing command which is in isObsolete state is not possible
79  EXPECT_THROW(command->undo(), std::runtime_error);
80 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
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
Variant data(SessionItem *item, int role) const
Returns the data for given item and role.
Defines class CLASS?
const int DATA
main data role
Definition: mvvm_types.h:30
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(SetValueCommandTest, setValueCommand)
Set item value through SetValueCommand command.