Skip to content

New test setting types: enum list/enum set #16015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: refactor-soltest-error-handling
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions test/TestCaseReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@

#include <test/libsolidity/util/SoltestErrors.h>

#include <libsolutil/CommonData.h>
#include <libsolutil/StringUtils.h>

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem.hpp>
#include <boost/throw_exception.hpp>

#include <fmt/format.h>

#include <range/v3/range/conversion.hpp>
#include <range/v3/view/map.hpp>
#include <range/v3/view/transform.hpp>

#include <fstream>
#include <map>
Expand Down Expand Up @@ -73,6 +78,10 @@ class TestCaseReader

template <typename E>
E enumSetting(std::string const& _name, std::map<std::string, E> const& _choices, std::string const& _defaultChoice);
template <typename E>
std::vector<E> enumListSetting(std::string const& _name, std::map<E, std::string> const& _choices, std::vector<E> const& _defaultValue);
template <typename E>
std::set<E> enumSetSetting(std::string const& _name, std::map<E, std::string> const& _choices, std::set<E> const& _defaultValue);

void ensureAllSettingsRead() const;

Expand Down Expand Up @@ -105,4 +114,52 @@ E TestCaseReader::enumSetting(std::string const& _name, std::map<std::string, E>
return _choices.at(value);
}

template <typename E>
std::vector<E> TestCaseReader::enumListSetting(std::string const& _name, std::map<E, std::string> const& _choices, std::vector<E> const& _defaultValue)
{
std::map<std::string, E> const labelToItem = util::invertMap(_choices);
auto const translateToLabel = [&](E _item) { return _choices.at(_item); };

for (std::string const& label: labelToItem | ranges::views::keys)
soltestAssert(label.find(",") == std::string::npos && boost::algorithm::trim_copy(label) == label);

for (E item: _defaultValue)
soltestAssert(_choices.contains(item));

std::string value = stringSetting(
_name,
util::joinHumanReadable(_defaultValue | ranges::views::transform(translateToLabel) | ranges::to<std::vector>)
);

std::vector<std::string> selectedLabels;
boost::split(selectedLabels, value, boost::is_any_of(","));

std::vector<E> selectedItems;
std::vector<std::string> invalidLabels;
for (std::string const& label: selectedLabels)
{
std::string trimmedLabel = boost::trim_copy(label);
if (labelToItem.contains(trimmedLabel))
selectedItems.push_back(labelToItem.at(trimmedLabel));
else
invalidLabels.push_back(trimmedLabel);
}

if (!invalidLabels.empty())
solThrow(solidity::test::ValidationError, fmt::format(
"Invalid choices in '{}' setting: {}.\nAvailable choices: {}.",
_name,
util::joinHumanReadable(invalidLabels),
util::joinHumanReadable(labelToItem | ranges::views::keys)
));

return selectedItems;
}

template <typename E>
std::set<E> TestCaseReader::enumSetSetting(std::string const& _name, std::map<E, std::string> const& _choices, std::set<E> const& _defaultValue)
{
return enumListSetting(_name, _choices, _defaultValue | ranges::to<std::vector>) | ranges::to<std::set>;
}

}
50 changes: 24 additions & 26 deletions test/libevmasm/EVMAssemblyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <libevmasm/EVMAssemblyStack.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>

#include <range/v3/view/map.hpp>
Expand All @@ -40,12 +39,12 @@ using namespace solidity::frontend::test;
using namespace solidity::langutil;
using namespace solidity::util;

std::vector<std::string> const EVMAssemblyTest::c_outputLabels = {
"InputAssemblyJSON",
"Assembly",
"Bytecode",
"Opcodes",
"SourceMappings",
std::map<EVMAssemblyTest::Output, std::string> const EVMAssemblyTest::c_outputLabels = {
{EVMAssemblyTest::Output::InputAssemblyJSON, "InputAssemblyJSON"},
{EVMAssemblyTest::Output::Assembly, "Assembly"},
{EVMAssemblyTest::Output::Bytecode, "Bytecode"},
{EVMAssemblyTest::Output::Opcodes, "Opcodes"},
{EVMAssemblyTest::Output::SourceMappings, "SourceMappings"},
};

std::unique_ptr<TestCase> EVMAssemblyTest::create(Config const& _config)
Expand All @@ -66,7 +65,11 @@ EVMAssemblyTest::EVMAssemblyTest(std::string const& _filename):
else
solThrow(ValidationError, "Not an assembly test: \"" + _filename + "\". Allowed extensions: .asm, .asmjson.");

m_selectedOutputs = m_reader.stringSetting("outputs", "Assembly,Bytecode,Opcodes,SourceMappings");
m_selectedOutputs = m_reader.enumSetSetting(
"outputs",
c_outputLabels,
{Output::Assembly, Output::Bytecode, Output::Opcodes, Output::SourceMappings}
);
OptimisationPreset optimizationPreset = m_reader.enumSetting<OptimisationPreset>(
"optimizationPreset",
{
Expand Down Expand Up @@ -145,34 +148,29 @@ TestCase::TestResult EVMAssemblyTest::run(std::ostream& _stream, std::string con
}
soltestAssert(evmAssemblyStack.compilationSuccessful());

auto const produceOutput = [&](std::string const& _output) {
if (_output == "InputAssemblyJSON")
return assemblyJSON;
if (_output == "Assembly")
return evmAssemblyStack.assemblyString({{m_reader.fileName().filename().string(), m_source}});
if (_output == "Bytecode")
return util::toHex(evmAssemblyStack.object().bytecode);
if (_output == "Opcodes")
return disassemble(evmAssemblyStack.object().bytecode, CommonOptions::get().evmVersion());
if (_output == "SourceMappings")
return evmAssemblyStack.sourceMapping();
soltestAssert(false);
auto const produceOutput = [&](Output _output) {
switch (_output)
{
case Output::InputAssemblyJSON: return assemblyJSON;
case Output::Assembly: return evmAssemblyStack.assemblyString({{m_reader.fileName().filename().string(), m_source}});
case Output::Bytecode: return util::toHex(evmAssemblyStack.object().bytecode);
case Output::Opcodes: return disassemble(evmAssemblyStack.object().bytecode, CommonOptions::get().evmVersion());
case Output::SourceMappings: return evmAssemblyStack.sourceMapping();
}
unreachable();
};

std::set<std::string> selectedOutputSet;
boost::split(selectedOutputSet, m_selectedOutputs, boost::is_any_of(","));
for (std::string const& output: c_outputLabels)
if (selectedOutputSet.contains(output))
for (Output output: c_outputLabels | ranges::views::keys)
if (m_selectedOutputs.contains(output))
{
if (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n')
m_obtainedResult += "\n";

// Don't trim on the left to avoid stripping indentation.
std::string content = produceOutput(output);
boost::trim_right(content);
std::string separator = (content.empty() ? "" : (output == "Assembly" ? "\n" : " "));
m_obtainedResult += output + ":" + separator + content;
std::string separator = (content.empty() ? "" : (output == Output::Assembly ? "\n" : " "));
m_obtainedResult += c_outputLabels.at(output) + ":" + separator + content;
}

return checkResult(_stream, _linePrefix, _formatted);
Expand Down
16 changes: 13 additions & 3 deletions test/libevmasm/EVMAssemblyTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

#include <libevmasm/Assembly.h>

#include <map>
#include <memory>
#include <ostream>
#include <set>
#include <string>
#include <vector>

namespace solidity::evmasm::test
{
Expand All @@ -48,10 +49,19 @@ class EVMAssemblyTest: public frontend::test::EVMVersionRestrictedTestCase
Plain,
};

static std::vector<std::string> const c_outputLabels;
enum class Output
{
InputAssemblyJSON,
Assembly,
Bytecode,
Opcodes,
SourceMappings,
};

static std::map<Output, std::string> const c_outputLabels;

AssemblyFormat m_assemblyFormat{};
std::string m_selectedOutputs;
std::set<Output> m_selectedOutputs;
evmasm::Assembly::OptimiserSettings m_optimizerSettings;
};

Expand Down
52 changes: 31 additions & 21 deletions test/libyul/ObjectCompilerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::test;

std::map<ObjectCompilerTest::Output, std::string> const ObjectCompilerTest::c_outputLabels = {
{ObjectCompilerTest::Output::Assembly, "Assembly"},
{ObjectCompilerTest::Output::Bytecode, "Bytecode"},
{ObjectCompilerTest::Output::Opcodes, "Opcodes"},
{ObjectCompilerTest::Output::SourceMappings, "SourceMappings"},
};

ObjectCompilerTest::ObjectCompilerTest(std::string const& _filename):
EVMVersionRestrictedTestCase(_filename)
{
Expand All @@ -59,11 +66,11 @@ ObjectCompilerTest::ObjectCompilerTest(std::string const& _filename):
"minimal"
);

constexpr std::array allowedOutputs = {"Assembly", "Bytecode", "Opcodes", "SourceMappings"};
boost::split(m_outputSetting, m_reader.stringSetting("outputs", "Assembly,Bytecode,Opcodes,SourceMappings"), boost::is_any_of(","));
for (auto const& output: m_outputSetting)
if (std::find(allowedOutputs.begin(), allowedOutputs.end(), output) == allowedOutputs.end())
solThrow(ValidationError, "Invalid output type: \"" + output + "\"");
m_selectedOutputs = m_reader.enumSetSetting(
"outputs",
c_outputLabels,
c_outputLabels | ranges::views::keys | ranges::to<std::set>
);

m_expectation = m_reader.simpleExpectations();
}
Expand All @@ -86,26 +93,29 @@ TestCase::TestResult ObjectCompilerTest::run(std::ostream& _stream, std::string
solAssert(obj.bytecode);
solAssert(obj.sourceMappings);

if (std::find(m_outputSetting.begin(), m_outputSetting.end(), "Assembly") != m_outputSetting.end())
m_obtainedResult = "Assembly:\n" + obj.assembly->assemblyString(yulStack.debugInfoSelection());
if (obj.bytecode->bytecode.empty())
m_obtainedResult += "-- empty bytecode --\n";
else
{
if (std::find(m_outputSetting.begin(), m_outputSetting.end(), "Bytecode") != m_outputSetting.end())
m_obtainedResult += "Bytecode: " + util::toHex(obj.bytecode->bytecode);
if (std::find(m_outputSetting.begin(), m_outputSetting.end(), "Opcodes") != m_outputSetting.end())
auto const produceOutput = [&](Output _output) {
switch (_output)
{
m_obtainedResult += (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n') ? "\n" : "";
m_obtainedResult += "Opcodes: " +
boost::trim_copy(evmasm::disassemble(obj.bytecode->bytecode, CommonOptions::get().evmVersion()));
case Output::Assembly: return obj.assembly->assemblyString(yulStack.debugInfoSelection());
case Output::Bytecode: return util::toHex(obj.bytecode->bytecode);
case Output::Opcodes: return evmasm::disassemble(obj.bytecode->bytecode, CommonOptions::get().evmVersion());
case Output::SourceMappings: return *obj.sourceMappings;
}
if (std::find(m_outputSetting.begin(), m_outputSetting.end(), "SourceMappings") != m_outputSetting.end())
unreachable();
};

for (Output output: c_outputLabels | ranges::views::keys)
if (m_selectedOutputs.contains(output))
{
m_obtainedResult += (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n') ? "\n" : "";
m_obtainedResult += "SourceMappings:" + (obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) + "\n";
if (!m_obtainedResult.empty() && m_obtainedResult.back() != '\n')
m_obtainedResult += "\n";

// Don't trim on the left to avoid stripping indentation.
std::string content = produceOutput(output);
boost::trim_right(content);
std::string separator = (content.empty() ? "" : (output == Output::Assembly ? "\n" : " "));
m_obtainedResult += c_outputLabels.at(output) + ":" + separator + content;
}
}

return checkResult(_stream, _linePrefix, _formatted);
}
12 changes: 11 additions & 1 deletion test/libyul/ObjectCompilerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ class ObjectCompilerTest: public solidity::frontend::test::EVMVersionRestrictedT
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;

private:
enum class Output
{
Assembly,
Bytecode,
Opcodes,
SourceMappings,
};

void disambiguate();

static std::map<Output, std::string> const c_outputLabels;

frontend::OptimisationPreset m_optimisationPreset;
std::vector<std::string> m_outputSetting;
std::set<Output> m_selectedOutputs;
};

}