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

Implements class CLASS? More...

Include dependency graph for sessionitemdata.test.cpp:

Go to the source code of this file.

Classes

class  SessionItemDataTest
 Test of SessionItemData. More...
 

Functions

 TEST_F (SessionItemDataTest, changingRole)
 Changing type of variant for role should not be allowed. More...
 
 TEST_F (SessionItemDataTest, differentRoles)
 Using different roles. More...
 
 TEST_F (SessionItemDataTest, hasRole)
 
 TEST_F (SessionItemDataTest, initialState)
 Initial state of SessionItemData object. More...
 
 TEST_F (SessionItemDataTest, rangeLoop)
 
 TEST_F (SessionItemDataTest, setDataComboProperty)
 Basic setData, data operations. More...
 
 TEST_F (SessionItemDataTest, setDataDouble)
 Basic setData, data operations. 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 sessionitemdata.test.cpp.

Function Documentation

◆ TEST_F() [1/7]

TEST_F ( SessionItemDataTest  ,
changingRole   
)

Changing type of variant for role should not be allowed.

Definition at line 119 of file sessionitemdata.test.cpp.

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 }
Handles data roles for SessionItem.
std::vector< int > roles() const
bool setData(const Variant &value, int role)
Sets the data for given role.
Variant data(int role) const

References ModelView::SessionItemData::data(), ModelView::SessionItemData::roles(), and ModelView::SessionItemData::setData().

Here is the call graph for this function:

◆ TEST_F() [2/7]

TEST_F ( SessionItemDataTest  ,
differentRoles   
)

Using different roles.

Definition at line 98 of file sessionitemdata.test.cpp.

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 }

References ModelView::SessionItemData::data(), ModelView::SessionItemData::roles(), and ModelView::SessionItemData::setData().

Here is the call graph for this function:

◆ TEST_F() [3/7]

TEST_F ( SessionItemDataTest  ,
hasRole   
)

Definition at line 158 of file sessionitemdata.test.cpp.

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 }
bool hasData(int role) const
Returns true if item has data with given role.

References ModelView::SessionItemData::hasData(), and ModelView::SessionItemData::setData().

Here is the call graph for this function:

◆ TEST_F() [4/7]

TEST_F ( SessionItemDataTest  ,
initialState   
)

Initial state of SessionItemData object.

Definition at line 34 of file sessionitemdata.test.cpp.

35 {
36  SessionItemData data;
37  EXPECT_TRUE(data.roles().empty());
38  EXPECT_FALSE(data.data(Qt::DisplayRole).isValid());
39 }

References ModelView::SessionItemData::data(), and ModelView::SessionItemData::roles().

Here is the call graph for this function:

◆ TEST_F() [5/7]

TEST_F ( SessionItemDataTest  ,
rangeLoop   
)

Definition at line 136 of file sessionitemdata.test.cpp.

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 }

References ModelView::SessionItemData::setData().

Here is the call graph for this function:

◆ TEST_F() [6/7]

TEST_F ( SessionItemDataTest  ,
setDataComboProperty   
)

Basic setData, data operations.

Definition at line 74 of file sessionitemdata.test.cpp.

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 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setValue(const std::string &name)
const int DATA
main data role
Definition: mvvm_types.h:30

References ModelView::ComboProperty::createFrom(), ModelView::ItemDataRole::DATA, ModelView::SessionItemData::data(), ModelView::SessionItemData::setData(), and ModelView::ComboProperty::setValue().

Here is the call graph for this function:

◆ TEST_F() [7/7]

TEST_F ( SessionItemDataTest  ,
setDataDouble   
)

Basic setData, data operations.

Definition at line 43 of file sessionitemdata.test.cpp.

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 }

References ModelView::ItemDataRole::DATA, ModelView::SessionItemData::data(), ModelView::SessionItemData::roles(), and ModelView::SessionItemData::setData().

Here is the call graph for this function: