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