##// END OF EJS Templates
Implements validation of variable's data (1)...
Alexandre Leroux -
r1198:077a4fb03e91
parent child
Show More
@@ -1,84 +1,141
1 1 #include "FuzzingValidators.h"
2 #include <Variable/Variable.h>
2 3
3 4 #include <QTest>
4 5
5 6 #include <functional>
6 7
7 8 Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators")
8 9
9 10 namespace {
10 11
12 // ////////////// //
13 // DATA VALIDATOR //
14 // ////////////// //
15
16 /// Singleton used to validate data of a variable
17 class DataValidatorHelper {
18 public:
19 /// @return the single instance of the helper
20 static DataValidatorHelper &instance();
21 virtual ~DataValidatorHelper() noexcept = default;
22
23 virtual void validate(const VariableState &variableState) const = 0;
24 };
25
26 /**
27 * Default implementation of @sa DataValidatorHelper
28 */
29 class DefaultDataValidatorHelper : public DataValidatorHelper {
30 public:
31 void validate(const VariableState &variableState) const override
32 {
33 Q_UNUSED(variableState);
34 qCWarning(LOG_FuzzingValidators()).noquote() << "Checking variable's data... WARN: no data "
35 "verification is available for this server";
36 }
37 };
38
39 /**
40 * Implementation of @sa DataValidatorHelper for the local AMDA server
41 */
42 class LocalhostServerDataValidatorHelper : public DataValidatorHelper {
43 public:
44 void validate(const VariableState &variableState) const override
45 {
46 /// @todo: complete
47 }
48 };
49
50 /// Creates the @sa DataValidatorHelper according to the server passed in parameter
51 std::unique_ptr<DataValidatorHelper> createDataValidatorInstance(const QString &server)
52 {
53 if (server == QString{"localhost"}) {
54 return std::make_unique<LocalhostServerDataValidatorHelper>();
55 }
56 else {
57 return std::make_unique<DefaultDataValidatorHelper>();
58 }
59 }
60
61 DataValidatorHelper &DataValidatorHelper::instance()
62 {
63 // Creates instance depending on the SCIQLOP_AMDA_SERVER value at compile time
64 static auto instance = createDataValidatorInstance(SCIQLOP_AMDA_SERVER);
65 return *instance;
66 }
67
11 68 // /////////////// //
12 69 // RANGE VALIDATOR //
13 70 // /////////////// //
14 71
15 72 /**
16 73 * Checks that a range of a variable matches the expected range passed as a parameter
17 74 * @param variable the variable for which to check the range
18 75 * @param expectedRange the expected range
19 76 * @param getVariableRangeFun the function to retrieve the range from the variable
20 77 * @remarks if the variable is null, checks that the expected range is the invalid range
21 78 */
22 79 void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange,
23 80 std::function<SqpRange(const Variable &)> getVariableRangeFun)
24 81 {
25 82 auto compare = [](const auto &range, const auto &expectedRange, const auto &message) {
26 83 if (range == expectedRange) {
27 84 qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK";
28 85 }
29 86 else {
30 87 qCInfo(LOG_FuzzingValidators()).noquote()
31 88 << message << "FAIL (current range:" << range
32 89 << ", expected range:" << expectedRange << ")";
33 90 QFAIL("");
34 91 }
35 92 };
36 93
37 94 if (variable) {
38 95 compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range...");
39 96 }
40 97 else {
41 98 compare(INVALID_RANGE, expectedRange, "Checking that there is no range set...");
42 99 }
43 100 }
44 101
45 102 /**
46 103 * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its
47 104 * construction a function of validation which is called in the validate() method
48 105 */
49 106 class FuzzingValidator : public IFuzzingValidator {
50 107 public:
51 108 /// Signature of a validation function
52 109 using ValidationFunction = std::function<void(const VariableState &variableState)>;
53 110
54 111 explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {}
55 112
56 113 void validate(const VariableState &variableState) const override { m_Fun(variableState); }
57 114
58 115 private:
59 116 ValidationFunction m_Fun;
60 117 };
61 118
62 119 } // namespace
63 120
64 121 std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type)
65 122 {
66 123 switch (type) {
67 124 case FuzzingValidatorType::DATA:
68 125 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
69 /// @todo: complete
126 DataValidatorHelper::instance().validate(variableState);
70 127 });
71 128 case FuzzingValidatorType::RANGE:
72 129 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
73 130 auto getVariableRange = [](const Variable &variable) { return variable.range(); };
74 131 validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange);
75 132 });
76 133 default:
77 134 // Default case returns invalid validator
78 135 break;
79 136 }
80 137
81 138 // Invalid validator
82 139 return std::make_unique<FuzzingValidator>(
83 140 [](const VariableState &) { QFAIL("Invalid validator"); });
84 141 }
General Comments 0
You need to be logged in to leave comments. Login now