@@ -1,49 +1,84 | |||
|
1 | 1 | #include "FuzzingValidators.h" |
|
2 | 2 | |
|
3 | 3 | #include <QTest> |
|
4 | 4 | |
|
5 | 5 | #include <functional> |
|
6 | 6 | |
|
7 | 7 | Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators") |
|
8 | 8 | |
|
9 | 9 | namespace { |
|
10 | 10 | |
|
11 | // /////////////// // | |
|
12 | // RANGE VALIDATOR // | |
|
13 | // /////////////// // | |
|
14 | ||
|
15 | /** | |
|
16 | * Checks that a range of a variable matches the expected range passed as a parameter | |
|
17 | * @param variable the variable for which to check the range | |
|
18 | * @param expectedRange the expected range | |
|
19 | * @param getVariableRangeFun the function to retrieve the range from the variable | |
|
20 | * @remarks if the variable is null, checks that the expected range is the invalid range | |
|
21 | */ | |
|
22 | void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange, | |
|
23 | std::function<SqpRange(const Variable &)> getVariableRangeFun) | |
|
24 | { | |
|
25 | auto compare = [](const auto &range, const auto &expectedRange, const auto &message) { | |
|
26 | if (range == expectedRange) { | |
|
27 | qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK"; | |
|
28 | } | |
|
29 | else { | |
|
30 | qCInfo(LOG_FuzzingValidators()).noquote() | |
|
31 | << message << "FAIL (current range:" << range | |
|
32 | << ", expected range:" << expectedRange << ")"; | |
|
33 | QFAIL(""); | |
|
34 | } | |
|
35 | }; | |
|
36 | ||
|
37 | if (variable) { | |
|
38 | compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range..."); | |
|
39 | } | |
|
40 | else { | |
|
41 | compare(INVALID_RANGE, expectedRange, "Checking that there is no range set..."); | |
|
42 | } | |
|
43 | } | |
|
44 | ||
|
11 | 45 | /** |
|
12 | 46 | * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its |
|
13 | 47 | * construction a function of validation which is called in the validate() method |
|
14 | 48 | */ |
|
15 | 49 | class FuzzingValidator : public IFuzzingValidator { |
|
16 | 50 | public: |
|
17 | 51 | /// Signature of a validation function |
|
18 | 52 | using ValidationFunction = std::function<void(const VariableState &variableState)>; |
|
19 | 53 | |
|
20 | 54 | explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {} |
|
21 | 55 | |
|
22 | 56 | void validate(const VariableState &variableState) const override { m_Fun(variableState); } |
|
23 | 57 | |
|
24 | 58 | private: |
|
25 | 59 | ValidationFunction m_Fun; |
|
26 | 60 | }; |
|
27 | 61 | |
|
28 | 62 | } // namespace |
|
29 | 63 | |
|
30 | 64 | std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type) |
|
31 | 65 | { |
|
32 | 66 | switch (type) { |
|
33 | 67 | case FuzzingValidatorType::DATA: |
|
34 | 68 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { |
|
35 | 69 | /// @todo: complete |
|
36 | 70 | }); |
|
37 | 71 | case FuzzingValidatorType::RANGE: |
|
38 | 72 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { |
|
39 | /// @todo: complete | |
|
73 | auto getVariableRange = [](const Variable &variable) { return variable.range(); }; | |
|
74 | validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange); | |
|
40 | 75 | }); |
|
41 | 76 | default: |
|
42 | 77 | // Default case returns invalid validator |
|
43 | 78 | break; |
|
44 | 79 | } |
|
45 | 80 | |
|
46 | 81 | // Invalid validator |
|
47 | 82 | return std::make_unique<FuzzingValidator>( |
|
48 | 83 | [](const VariableState &) { QFAIL("Invalid validator"); }); |
|
49 | 84 | } |
General Comments 0
You need to be logged in to leave comments.
Login now