1
0
mirror of synced 2025-02-08 15:08:11 +01:00

65 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>
#include <hex/pattern_language/patterns/pattern.hpp>
2021-09-11 14:41:18 +02:00
#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
{
2021-09-11 23:13:49 +02:00
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 });
}
virtual ~TestPattern() = default;
2021-09-11 14:41:18 +02:00
template<typename T>
static std::unique_ptr<T> create(const std::string &typeName, const std::string &varName, auto... args) {
auto pattern = std::make_unique<T>(nullptr, args...);
2021-09-11 14:41:18 +02:00
pattern->setTypeName(typeName);
pattern->setVariableName(varName);
return std::move(pattern);
2021-09-11 14:41:18 +02:00
}
[[nodiscard]] virtual std::string getSourceCode() const = 0;
2021-09-11 14:41:18 +02:00
[[nodiscard]] virtual const std::vector<std::unique_ptr<Pattern>> &getPatterns() const final { return this->m_patterns; }
virtual void addPattern(std::unique_ptr<Pattern> &&pattern) final {
this->m_patterns.push_back(std::move(pattern));
2021-09-11 14:41:18 +02:00
}
[[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<std::unique_ptr<Pattern>> 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
};
}