ImHex/tests/pattern_language/include/test_patterns/test_pattern.hpp

72 lines
1.7 KiB
C++
Raw Normal View History

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