forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisibleWhenPropertyTest.h
206 lines (177 loc) · 8.61 KB
/
VisibleWhenPropertyTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include <cxxtest/TestSuite.h>
#include "MantidKernel/PropertyManagerOwner.h"
#include "MantidKernel/VisibleWhenProperty.h"
using namespace Mantid;
using namespace Mantid::Kernel;
class VisibleWhenPropertyTest : public CxxTest::TestSuite {
public:
void test_when_IS_NOT_DEFAULT() {
PropertyManagerOwner alg;
// Start with a regular property
alg.declareProperty("MyIntProp", 123);
// Make a property with its validator. Will be Visible when that other one
// is NOT the default
alg.declareProperty("MyValidatorProp", 456);
alg.setPropertySettings("MyValidatorProp", std::make_unique<VisibleWhenProperty>("MyIntProp", IS_NOT_DEFAULT));
Property *prop = alg.getPointerToProperty("MyValidatorProp");
TS_ASSERT(prop);
if (!prop)
return;
TSM_ASSERT("Property always returns enabled.", prop->getSettings()->isEnabled(&alg))
TSM_ASSERT("Property always returns valid.", prop->isValid().empty())
TSM_ASSERT("Starts off NOT Visible", !prop->getSettings()->isVisible(&alg));
alg.setProperty("MyIntProp", 234);
TSM_ASSERT("Becomes visible when another property has been changed", prop->getSettings()->isVisible(&alg));
alg.declareProperty("MySecondValidatorProp", 456);
alg.setPropertySettings("MySecondValidatorProp",
std::make_unique<VisibleWhenProperty>("MyIntProp", IS_NOT_DEFAULT));
prop = alg.getPointerToProperty("MySecondValidatorProp");
TSM_ASSERT("Starts off visible", prop->getSettings()->isVisible(&alg));
alg.setProperty("MyIntProp", 123);
TSM_ASSERT("Goes back to not visible", !prop->getSettings()->isVisible(&alg));
}
void test_when_IS_DEFAULT() {
PropertyManagerOwner alg;
alg.declareProperty("MyIntProp", 123);
// Make a property with its validator. Will be Visible when that other one
// is the default
alg.declareProperty("MyValidatorProp", 456);
alg.setPropertySettings("MyValidatorProp", std::make_unique<VisibleWhenProperty>("MyIntProp", IS_DEFAULT));
Property *prop = alg.getPointerToProperty("MyValidatorProp");
TS_ASSERT(prop);
if (!prop)
return;
TSM_ASSERT("Starts off visible", prop->getSettings()->isVisible(&alg));
alg.setProperty("MyIntProp", -1);
TSM_ASSERT("Becomes not visible when another property has been changed", !prop->getSettings()->isVisible(&alg));
}
void test_when_IS_EQUAL_TO() {
PropertyManagerOwner alg;
alg.declareProperty("MyIntProp", 123);
alg.declareProperty("MyValidatorProp", 456);
alg.setPropertySettings("MyValidatorProp", std::make_unique<VisibleWhenProperty>("MyIntProp", IS_EQUAL_TO, "234"));
Property *prop = alg.getPointerToProperty("MyValidatorProp");
TS_ASSERT(prop);
if (!prop)
return;
TSM_ASSERT("Starts off not visible", !prop->getSettings()->isVisible(&alg));
alg.setProperty("MyIntProp", 234);
TSM_ASSERT("Becomes visible when the other property is equal to the given string",
prop->getSettings()->isVisible(&alg));
}
void test_when_IS_NOT_EQUAL_TO() {
PropertyManagerOwner alg;
alg.declareProperty("MyIntProp", 123);
alg.declareProperty("MyValidatorProp", 456);
alg.setPropertySettings("MyValidatorProp",
std::make_unique<VisibleWhenProperty>("MyIntProp", IS_NOT_EQUAL_TO, "234"));
Property *prop = alg.getPointerToProperty("MyValidatorProp");
TS_ASSERT(prop);
if (!prop)
return;
TSM_ASSERT("Starts off not visible", prop->getSettings()->isVisible(&alg));
alg.setProperty("MyIntProp", 234);
TSM_ASSERT("Becomes visible when the other property is equal to the given string",
!prop->getSettings()->isVisible(&alg));
}
void test_combination_AND() {
// Setup with same value first
auto alg = setupCombinationTest(AND, true);
const auto prop = alg.getPointerToProperty(m_resultPropName);
TS_ASSERT(prop);
const auto propSettings = prop->getSettings();
// AND should return true first
TS_ASSERT(propSettings->isVisible(&alg));
// Now set a different value - should be disabled
alg.setPropertyValue(m_propertyOneName, m_propertyTrueValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyFalseValue);
TS_ASSERT(!propSettings->isVisible(&alg));
}
void test_combination_OR() {
// First check with both set to the true value
auto alg = setupCombinationTest(OR, true);
const auto prop = alg.getPointerToProperty(m_resultPropName);
TS_ASSERT(prop);
const auto propSettings = prop->getSettings();
// OR should return true for both values on
TS_ASSERT(propSettings->isVisible(&alg));
// Set property one to false condition and check OR is still true
alg.setPropertyValue(m_propertyOneName, m_propertyFalseValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyTrueValue);
TS_ASSERT(propSettings->isVisible(&alg));
// Set property two to false condition and check OR is still true
alg.setPropertyValue(m_propertyOneName, m_propertyTrueValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyFalseValue);
TS_ASSERT(propSettings->isVisible(&alg));
// Check the with both set to false the OR returns false
alg.setPropertyValue(m_propertyOneName, m_propertyFalseValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyFalseValue);
TS_ASSERT(!propSettings->isVisible(&alg));
}
void test_combination_XOR() {
auto alg = setupCombinationTest(XOR, true);
const auto prop = alg.getPointerToProperty(m_resultPropName);
TS_ASSERT(prop);
const auto propSettings = prop->getSettings();
// With both set to the same value this should return false
TS_ASSERT(!propSettings->isVisible(&alg));
// Set property one to false and two to true so returns true
alg.setPropertyValue(m_propertyOneName, m_propertyFalseValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyTrueValue);
TS_ASSERT(propSettings->isVisible(&alg));
// Set property one to true and one to false so returns true
alg.setPropertyValue(m_propertyOneName, m_propertyTrueValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyFalseValue);
TS_ASSERT(propSettings->isVisible(&alg));
// Check with both set false it returns false
alg.setPropertyValue(m_propertyOneName, m_propertyFalseValue);
alg.setPropertyValue(m_propertyTwoName, m_propertyFalseValue);
TS_ASSERT(!propSettings->isVisible(&alg));
}
private:
const std::string m_propertyTrueValue = "testTrue";
const std::string m_propertyFalseValue = "testFalse";
const std::string m_resultValue = "Result";
const std::string m_propertyOneName = "PropOne";
const std::string m_propertyTwoName = "PropTwo";
const std::string m_resultPropName = "ResultProp";
PropertyManagerOwner setupCombinationTest(eLogicOperator logicOperation, bool secondPropertyIsTrue) {
auto propOne = getVisibleWhenProp(m_propertyOneName, IS_EQUAL_TO, m_propertyTrueValue);
auto propTwo = getVisibleWhenProp(m_propertyTwoName, IS_EQUAL_TO, m_propertyTrueValue);
auto combination = getCombinationProperty(propOne, propTwo, logicOperation);
// Set both to the same value to check
PropertyManagerOwner alg;
alg.declareProperty(m_propertyOneName, m_propertyTrueValue);
if (secondPropertyIsTrue) {
alg.declareProperty(m_propertyTwoName, m_propertyTrueValue);
} else {
alg.declareProperty(m_propertyTwoName, m_propertyFalseValue);
}
alg.declareProperty(m_resultPropName, m_resultValue);
alg.setPropertySettings(m_resultPropName, std::move(combination));
return alg;
}
VisibleWhenProperty getVisibleWhenProp(const std::string &propName, ePropertyCriterion criterion,
const std::string &value = "") {
if (value.length() == 0) {
return VisibleWhenProperty(propName, criterion);
} else {
return VisibleWhenProperty(propName, criterion, value);
}
}
// Check the copy constructor works instead of std::make_unique
std::unique_ptr<IPropertySettings> getCombinationProperty(const VisibleWhenProperty &condOne,
const VisibleWhenProperty &condTwo,
eLogicOperator logicalOperator) {
return std::make_unique<VisibleWhenProperty>(condOne, condTwo, logicalOperator);
}
};