1
0
mirror of synced 2024-12-11 23:46:00 +01:00
ImHex/tests/pattern_language/include/test_patterns/test_pattern.hpp

67 lines
1.7 KiB
C++
Raw Normal View History

2021-09-11 14:41:18 +02:00
#pragma once
2021-09-11 14:41:18 +02:00
#include <string>
#include <vector>
2021-09-11 14:41:18 +02:00
#include <hex/pattern_language/pattern_data.hpp>
#define TEST(name) (hex::test::TestPattern *)new hex::test::TestPattern##name()
2021-09-11 23:13:49 +02:00
2021-09-11 14:41:18 +02:00
namespace hex::test {
2021-09-11 23:13:49 +02:00
using namespace pl;
enum class Mode {
Succeeding,
Failing
};
2021-09-11 14:41:18 +02:00
class TestPattern {
public:
2021-09-11 23:13:49 +02:00
explicit TestPattern(const std::string &name, Mode mode = Mode::Succeeding) : m_mode(mode) {
TestPattern::s_tests.insert({ name, this });
}
2021-09-11 14:41:18 +02:00
virtual ~TestPattern() {
for (auto &pattern : this->m_patterns)
delete pattern;
}
template<typename T>
static T *create(const std::string &typeName, const std::string &varName, auto... args) {
2021-09-12 20:28:32 +02:00
auto pattern = new T(args...);
2021-09-11 14:41:18 +02:00
pattern->setTypeName(typeName);
pattern->setVariableName(varName);
return pattern;
}
[[nodiscard]] virtual std::string getSourceCode() const = 0;
2021-09-11 14:41:18 +02:00
[[nodiscard]] virtual const std::vector<PatternData *> &getPatterns() const final { return this->m_patterns; }
2021-09-11 23:13:49 +02:00
virtual void addPattern(PatternData *pattern) final {
2021-09-11 14:41:18 +02:00
this->m_patterns.push_back(pattern);
}
[[nodiscard]] auto failing() {
2021-09-11 23:13:49 +02:00
this->m_mode = Mode::Failing;
return this;
}
[[nodiscard]] Mode getMode() {
2021-09-11 23:13:49 +02:00
return this->m_mode;
}
[[nodiscard]] static auto &getTests() {
2021-09-11 23:13:49 +02:00
return TestPattern::s_tests;
}
2021-09-11 14:41:18 +02:00
private:
std::vector<PatternData *> m_patterns;
2021-09-11 23:13:49 +02:00
Mode m_mode;
static inline std::map<std::string, TestPattern *> s_tests;
2021-09-11 14:41:18 +02:00
};
}