@@ -1,49 +1,84 | |||||
1 | #include "FuzzingValidators.h" |
|
1 | #include "FuzzingValidators.h" | |
2 |
|
2 | |||
3 | #include <QTest> |
|
3 | #include <QTest> | |
4 |
|
4 | |||
5 | #include <functional> |
|
5 | #include <functional> | |
6 |
|
6 | |||
7 | Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators") |
|
7 | Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators") | |
8 |
|
8 | |||
9 | namespace { |
|
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 | * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its |
|
46 | * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its | |
13 | * construction a function of validation which is called in the validate() method |
|
47 | * construction a function of validation which is called in the validate() method | |
14 | */ |
|
48 | */ | |
15 | class FuzzingValidator : public IFuzzingValidator { |
|
49 | class FuzzingValidator : public IFuzzingValidator { | |
16 | public: |
|
50 | public: | |
17 | /// Signature of a validation function |
|
51 | /// Signature of a validation function | |
18 | using ValidationFunction = std::function<void(const VariableState &variableState)>; |
|
52 | using ValidationFunction = std::function<void(const VariableState &variableState)>; | |
19 |
|
53 | |||
20 | explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {} |
|
54 | explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {} | |
21 |
|
55 | |||
22 | void validate(const VariableState &variableState) const override { m_Fun(variableState); } |
|
56 | void validate(const VariableState &variableState) const override { m_Fun(variableState); } | |
23 |
|
57 | |||
24 | private: |
|
58 | private: | |
25 | ValidationFunction m_Fun; |
|
59 | ValidationFunction m_Fun; | |
26 | }; |
|
60 | }; | |
27 |
|
61 | |||
28 | } // namespace |
|
62 | } // namespace | |
29 |
|
63 | |||
30 | std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type) |
|
64 | std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type) | |
31 | { |
|
65 | { | |
32 | switch (type) { |
|
66 | switch (type) { | |
33 | case FuzzingValidatorType::DATA: |
|
67 | case FuzzingValidatorType::DATA: | |
34 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { |
|
68 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { | |
35 | /// @todo: complete |
|
69 | /// @todo: complete | |
36 | }); |
|
70 | }); | |
37 | case FuzzingValidatorType::RANGE: |
|
71 | case FuzzingValidatorType::RANGE: | |
38 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { |
|
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 | default: |
|
76 | default: | |
42 | // Default case returns invalid validator |
|
77 | // Default case returns invalid validator | |
43 | break; |
|
78 | break; | |
44 | } |
|
79 | } | |
45 |
|
80 | |||
46 | // Invalid validator |
|
81 | // Invalid validator | |
47 | return std::make_unique<FuzzingValidator>( |
|
82 | return std::make_unique<FuzzingValidator>( | |
48 | [](const VariableState &) { QFAIL("Invalid validator"); }); |
|
83 | [](const VariableState &) { QFAIL("Invalid validator"); }); | |
49 | } |
|
84 | } |
General Comments 0
You need to be logged in to leave comments.
Login now