Skip to content

Commit

Permalink
Export EOF validation unit tets to json EOFTests
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Feb 13, 2024
1 parent 04247dd commit 2c6214c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/unittests/eof_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@
// SPDX-License-Identifier: Apache-2.0

#include "eof_validation.hpp"
#include <test/statetest/statetest.hpp>
#include <fstream>

namespace fs = std::filesystem;

namespace evmone::test
{
namespace
{
/// Creates the file path for the exported test based on its name.
fs::path get_export_test_path(const testing::TestInfo& test_info, std::string_view export_dir)
{
const std::string_view test_suite_name{test_info.test_suite_name()};

const auto stem = fs::path{test_info.file()}.stem().string();
auto filename = std::string_view{stem};
if (filename.ends_with("_test"))
filename.remove_suffix(5);

const auto dir = fs::path{export_dir} / test_suite_name / filename;

fs::create_directories(dir);
return dir / (std::string{test_info.name()} + ".json");
}
} // namespace

void eof_validation::TearDown()
{
for (const auto& test_case : test_cases)
Expand All @@ -14,5 +37,39 @@ void eof_validation::TearDown()
<< test_case.name << "\n"
<< hex(test_case.container);
}

if (const auto export_dir = std::getenv("EVMONE_EXPORT_TESTS"); export_dir != nullptr)
export_eof_validation_test(export_dir);
}

void eof_validation::export_eof_validation_test(std::string_view export_dir)
{
const auto& test_info = *testing::UnitTest::GetInstance()->current_test_info();
const std::string test_name = test_info.name();

json::json j;
auto& jt = j[test_name];

auto& jvectors = jt["vectors"];
for (size_t i = 0; i < test_cases.size(); ++i)
{
const auto& test_case = test_cases[i];
const auto case_name =
test_case.name.empty() ? (test_name + "_" + std::to_string(i)) : test_case.name;

auto& jcase = jvectors[case_name];
jcase["code"] = hex0x(test_case.container);

auto& jresults = jcase["results"][evmc::to_string(rev)];
if (test_case.error == EOFValidationError::success)
jresults["result"] = true;
else
{
jresults["result"] = false;
jresults["exception"] = get_error_message(test_case.error);
}
}

std::ofstream{get_export_test_path(test_info, export_dir)} << std::setw(2) << j;
}
} // namespace evmone::test
3 changes: 3 additions & 0 deletions test/unittests/eof_validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class eof_validation : public testing::Test

/// The test runner.
void TearDown() override;

/// Exports the test in the JSON EOF Test format in the given directory.
void export_eof_validation_test(std::string_view export_dir);
};

} // namespace evmone::test

0 comments on commit 2c6214c

Please sign in to comment.