2021-09-11 14:41:18 +02:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-09-11 23:13:49 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2021-09-11 14:41:18 +02:00
|
|
|
#include <hex/pattern_language/pattern_language.hpp>
|
2021-09-11 23:13:49 +02:00
|
|
|
#include <hex/pattern_language/evaluator.hpp>
|
2022-02-27 23:25:39 +01:00
|
|
|
#include <hex/pattern_language/ast/ast_node.hpp>
|
|
|
|
#include <hex/pattern_language/patterns/pattern.hpp>
|
2021-09-11 23:13:49 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-09-11 14:41:18 +02:00
|
|
|
|
|
|
|
#include "test_provider.hpp"
|
2021-09-11 23:13:49 +02:00
|
|
|
#include "test_patterns/test_pattern.hpp"
|
2021-09-11 14:41:18 +02:00
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
#include <fmt/args.h>
|
|
|
|
|
2021-09-11 14:41:18 +02:00
|
|
|
using namespace hex::test;
|
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
static std::string format(hex::pl::Evaluator *ctx, const auto ¶ms) {
|
|
|
|
auto format = hex::pl::Token::literalToString(params[0], true);
|
|
|
|
std::string message;
|
|
|
|
|
|
|
|
fmt::dynamic_format_arg_store<fmt::format_context> formatArgs;
|
|
|
|
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
|
|
|
auto ¶m = params[i];
|
|
|
|
|
|
|
|
std::visit(hex::overloaded {
|
2022-03-03 13:35:12 +01:00
|
|
|
[&](hex::pl::Pattern *value) {
|
2022-02-27 23:25:39 +01:00
|
|
|
formatArgs.push_back(value->toString(ctx->getProvider()));
|
|
|
|
},
|
|
|
|
[&](auto &&value) {
|
|
|
|
formatArgs.push_back(value);
|
|
|
|
} },
|
|
|
|
param);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return fmt::vformat(format, formatArgs);
|
|
|
|
} catch (fmt::format_error &error) {
|
|
|
|
hex::pl::LogConsole::abortEvaluation(hex::format("format error: {}", error.what()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 23:13:49 +02:00
|
|
|
void addFunctions() {
|
2021-12-20 20:40:28 +01:00
|
|
|
hex::ContentRegistry::PatternLanguage::Namespace nsStd = { "std" };
|
|
|
|
hex::ContentRegistry::PatternLanguage::addFunction(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> Token::Literal {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto condition = Token::literalToBoolean(params[0]);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto message = Token::literalToString(params[1], false);
|
2021-09-11 14:41:18 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (!condition)
|
|
|
|
LogConsole::abortEvaluation(hex::format("assertion failed \"{0}\"", message));
|
2021-09-11 23:13:49 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-09-11 23:13:49 +02:00
|
|
|
});
|
2022-02-27 23:25:39 +01:00
|
|
|
|
|
|
|
hex::ContentRegistry::PatternLanguage::addFunction(nsStd, "print", hex::ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
ctx->getConsole().log(LogConsole::Level::Info, format(ctx, params));
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
2021-09-11 23:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int test(int argc, char **argv) {
|
|
|
|
auto &testPatterns = TestPattern::getTests();
|
2021-09-11 14:41:18 +02:00
|
|
|
|
|
|
|
// Check if a test to run has been provided
|
|
|
|
if (argc != 2) {
|
|
|
|
hex::log::fatal("Invalid number of arguments specified! {}", argc);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if that test exists
|
|
|
|
std::string testName = argv[1];
|
|
|
|
if (!testPatterns.contains(testName)) {
|
|
|
|
hex::log::fatal("No test with name {} found!", testName);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &currTest = testPatterns[testName];
|
2022-02-01 22:09:44 +01:00
|
|
|
bool failing = currTest->getMode() == Mode::Failing;
|
2021-09-11 14:41:18 +02:00
|
|
|
|
|
|
|
auto provider = new TestProvider();
|
|
|
|
ON_SCOPE_EXIT { delete provider; };
|
|
|
|
if (provider->getActualSize() == 0) {
|
|
|
|
hex::log::fatal("Failed to load Testing Data");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hex::pl::PatternLanguage language;
|
|
|
|
|
|
|
|
// Check if compilation succeeded
|
2022-02-01 18:09:40 +01:00
|
|
|
auto result = language.executeString(provider, testPatterns[testName]->getSourceCode());
|
|
|
|
if (!result) {
|
2021-09-11 14:41:18 +02:00
|
|
|
hex::log::fatal("Error during compilation!");
|
|
|
|
|
2021-09-11 23:13:49 +02:00
|
|
|
if (auto error = language.getError(); error.has_value())
|
2022-01-31 14:37:12 +01:00
|
|
|
hex::log::info("Compile error: {} : {}", error->getLineNumber(), error->what());
|
2021-10-20 11:06:24 +02:00
|
|
|
for (auto &[level, message] : language.getConsoleLog())
|
|
|
|
hex::log::info("Evaluate error: {}", message);
|
2021-09-11 23:13:49 +02:00
|
|
|
|
|
|
|
return failing ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failing) {
|
|
|
|
hex::log::fatal("Failing test succeeded!");
|
2021-09-11 14:41:18 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the right number of patterns have been produced
|
2022-02-01 18:09:40 +01:00
|
|
|
if (language.getPatterns().size() != currTest->getPatterns().size() && !currTest->getPatterns().empty()) {
|
2021-09-11 14:41:18 +02:00
|
|
|
hex::log::fatal("Source didn't produce expected number of patterns");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the produced patterns are the ones expected
|
2021-09-12 21:54:18 +02:00
|
|
|
for (u32 i = 0; i < currTest->getPatterns().size(); i++) {
|
2022-02-01 18:09:40 +01:00
|
|
|
auto &evaluatedPattern = *language.getPatterns()[i];
|
2022-02-01 22:09:44 +01:00
|
|
|
auto &controlPattern = *currTest->getPatterns()[i];
|
2021-09-11 14:41:18 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (evaluatedPattern != controlPattern) {
|
2021-09-25 23:31:37 +02:00
|
|
|
hex::log::fatal("Pattern with name {}:{} didn't match template", evaluatedPattern.getTypeName(), evaluatedPattern.getVariableName());
|
2021-09-11 14:41:18 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2021-09-11 23:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2021-10-03 23:10:08 +02:00
|
|
|
int result = EXIT_SUCCESS;
|
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
addFunctions();
|
|
|
|
|
2021-10-03 23:10:08 +02:00
|
|
|
for (u32 i = 0; i < 16; i++) {
|
|
|
|
result = test(argc, argv);
|
|
|
|
if (result != EXIT_SUCCESS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
for (auto &[key, value] : TestPattern::getTests())
|
|
|
|
delete value;
|
|
|
|
};
|
2021-09-11 23:13:49 +02:00
|
|
|
|
|
|
|
if (result == EXIT_SUCCESS)
|
|
|
|
hex::log::info("Success!");
|
|
|
|
else
|
|
|
|
hex::log::info("Failed!");
|
|
|
|
|
|
|
|
return result;
|
2021-09-11 14:41:18 +02:00
|
|
|
}
|