1
0
mirror of synced 2024-12-04 20:17:57 +01:00
ImHex/tests/include/test_patterns/test_pattern.hpp

72 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>
2021-09-11 23:13:49 +02:00
#define TEST(name) (hex::test::TestPattern*) new hex::test::TestPattern ## name ()
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>
2021-09-12 20:28:32 +02:00
static T* create(const std::string &typeName, const std::string &varName, auto ... args) {
auto pattern = new T(args...);
2021-09-11 14:41:18 +02:00
pattern->setTypeName(typeName);
pattern->setVariableName(varName);
return pattern;
}
2021-09-11 23:13:49 +02:00
[[nodiscard]]
2021-09-11 14:41:18 +02:00
virtual std::string getSourceCode() const = 0;
[[nodiscard]]
2021-09-11 23:13:49 +02:00
virtual const std::vector<PatternData*>& getPatterns() const final { return this->m_patterns; }
virtual void addPattern(PatternData *pattern) final {
2021-09-11 14:41:18 +02:00
this->m_patterns.push_back(pattern);
}
2021-09-11 23:13:49 +02: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 14:41:18 +02:00
private:
2021-09-11 23:13:49 +02:00
std::vector<PatternData*> m_patterns;
Mode m_mode;
static inline std::map<std::string, TestPattern*> s_tests;
2021-09-11 14:41:18 +02:00
};
}