From 55f4e90cbf33ef2843597170a389269fb5d63078 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Wed, 21 Feb 2024 18:33:38 +0100 Subject: [PATCH] Export EOF validation unit tests to json EOFTests --- test/unittests/eof_validation.cpp | 36 +++++++++++++++++++++++++++++++ test/unittests/eof_validation.hpp | 6 +++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/test/unittests/eof_validation.cpp b/test/unittests/eof_validation.cpp index a890bab395..640595aa73 100644 --- a/test/unittests/eof_validation.cpp +++ b/test/unittests/eof_validation.cpp @@ -3,6 +3,10 @@ // SPDX-License-Identifier: Apache-2.0 #include "eof_validation.hpp" +#include +#include + +namespace fs = std::filesystem; namespace evmone::test { @@ -15,5 +19,37 @@ void eof_validation::TearDown() << "test case " << i << " " << test_case.name << "\n" << hex(test_case.container); } + + if (!export_file_path.empty()) + export_eof_validation_test(); +} + +void eof_validation::export_eof_validation_test() +{ + json::json j; + auto& jt = j[export_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() ? + (std::string{export_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{export_file_path} << std::setw(2) << j; } } // namespace evmone::test diff --git a/test/unittests/eof_validation.hpp b/test/unittests/eof_validation.hpp index 3db91235a6..0d30a91fe0 100644 --- a/test/unittests/eof_validation.hpp +++ b/test/unittests/eof_validation.hpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include "exportable_fixture.hpp" #include #include #include @@ -16,7 +17,7 @@ using evmc::bytes; /// Fixture for defining test cases for EOF validation. /// /// Each test contains multiple cases, which are validated during test teardown. -class eof_validation : public testing::Test +class eof_validation : public ExportableFixture { protected: /// EOF validation test case. @@ -45,6 +46,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(); }; } // namespace evmone::test