BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionitemdata.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/sessionitemdata.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/mvvm_types.h"
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
23 //! Test of SessionItemData.
24 
25 class SessionItemDataTest : public ::testing::Test {
26 public:
28 };
29 
31 
32 //! Initial state of SessionItemData object.
33 
35 {
36  SessionItemData data;
37  EXPECT_TRUE(data.roles().empty());
38  EXPECT_FALSE(data.data(Qt::DisplayRole).isValid());
39 }
40 
41 //! Basic setData, data operations.
42 
43 TEST_F(SessionItemDataTest, setDataDouble)
44 {
45  SessionItemData data;
46 
47  const int role(ItemDataRole::DATA);
48  const QVariant variant(42.0);
49 
50  // setting variant for role
51  EXPECT_TRUE(data.setData(variant, role));
52  std::vector<int> expected{role};
53  EXPECT_EQ(data.roles(), expected);
54  EXPECT_TRUE(data.data(role) == variant);
55 
56  // setting same data twice
57  EXPECT_FALSE(data.setData(variant, role));
58  EXPECT_EQ(data.roles(), expected);
59  EXPECT_TRUE(data.data(role) == variant);
60 
61  // changing the data
62  EXPECT_TRUE(data.setData(QVariant(43.0), role));
63  EXPECT_EQ(data.roles(), expected);
64  EXPECT_TRUE(data.data(role) == QVariant(43.0));
65 
66  // setting invalid variant for the role will remove data
67  EXPECT_TRUE(data.setData(QVariant(), role));
68  EXPECT_TRUE(data.roles().empty());
69  EXPECT_FALSE(data.data(role).isValid());
70 }
71 
72 //! Basic setData, data operations.
73 
74 TEST_F(SessionItemDataTest, setDataComboProperty)
75 {
76  SessionItemData data;
77  ComboProperty c1 = ComboProperty::createFrom({"a1", "a2"});
78  ComboProperty c2 = ComboProperty::createFrom({"a1", "a2"});
79  c1.setValue("a1");
80  c2.setValue("a2");
81 
82  const int role(ItemDataRole::DATA);
83 
84  // setting variant for role
85  EXPECT_TRUE(data.setData(QVariant::fromValue(c1), role));
86  EXPECT_EQ(data.data(role).value<ComboProperty>(), c1);
87 
88  // setting same data twice
89  EXPECT_FALSE(data.setData(QVariant::fromValue(c1), role));
90 
91  // setting another data
92  EXPECT_TRUE(data.setData(QVariant::fromValue(c2), role));
93  EXPECT_EQ(data.data(role).value<ComboProperty>(), c2);
94 }
95 
96 //! Using different roles.
97 
98 TEST_F(SessionItemDataTest, differentRoles)
99 {
100  SessionItemData data;
101 
102  const int role1(1);
103  const int role2 = role1 + 1;
104 
105  EXPECT_TRUE(data.setData(QVariant::fromValue(42.0), role1));
106  EXPECT_TRUE(data.setData(QVariant::fromValue(std::string("str")), role2));
107 
108  std::vector<int> expected{role1, role2};
109  EXPECT_EQ(data.roles(), expected);
110 
111  EXPECT_TRUE(data.data(role1) == QVariant(42.0));
112  EXPECT_TRUE(data.data(role2) == QVariant::fromValue(std::string("str")));
113  EXPECT_FALSE(data.data(role2) == QVariant(42.0));
114  EXPECT_FALSE(data.data(role1) == QVariant::fromValue(std::string("str")));
115 }
116 
117 //! Changing type of variant for role should not be allowed.
118 
120 {
121  SessionItemData data;
122 
123  const int role(1);
124  const QVariant variant(42.0);
125 
126  // setting variant for role
127  EXPECT_TRUE(data.setData(variant, role));
128  std::vector<int> expected{role};
129  EXPECT_EQ(data.roles(), expected);
130  EXPECT_TRUE(data.data(role) == variant);
131 
132  QVariant s = QVariant::fromValue(std::string("str"));
133  EXPECT_THROW(data.setData(s, role), std::runtime_error);
134 }
135 
137 {
138  SessionItemData data;
139  const std::vector<double> expected_values = {1.2, 1.3};
140  const std::vector<int> expected_roles = {1, 2};
141 
142  for (size_t i = 0; i < expected_values.size(); ++i) {
143  data.setData(QVariant::fromValue(expected_values[i]), expected_roles[i]);
144  }
145 
146  std::vector<double> values;
147  std::vector<int> roles;
148 
149  for (const auto& x : data) {
150  values.push_back(x.m_data.value<double>());
151  roles.push_back(x.m_role);
152  }
153 
154  EXPECT_EQ(values, expected_values);
155  EXPECT_EQ(roles, expected_roles);
156 }
157 
159 {
160  SessionItemData data;
161  EXPECT_FALSE(data.hasData(0));
162  EXPECT_FALSE(data.hasData(1));
163 
164  const int role = 99;
165  data.setData(QVariant::fromValue(42), role);
166  EXPECT_TRUE(data.hasData(role));
167  EXPECT_FALSE(data.hasData(1));
168 
169  data.setData(QVariant(), role);
170  EXPECT_FALSE(data.hasData(role));
171 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setValue(const std::string &name)
static ComboProperty createFrom(const std::vector< std::string > &values, const std::string &current_value={})
Handles data roles for SessionItem.
std::vector< int > roles() const
bool hasData(int role) const
Returns true if item has data with given role.
bool setData(const Variant &value, int role)
Sets the data for given role.
Variant data(int role) const
Test of SessionItemData.
Defines class CLASS?
Defines class CLASS?
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?
TEST_F(SessionItemDataTest, initialState)
Initial state of SessionItemData object.