Skip to content

Commit 9c6f8c0

Browse files
committed
TestCaseReader: Add enumSetting()
1 parent 5023cef commit 9c6f8c0

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

test/TestCaseReader.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818

1919
#include <test/TestCaseReader.h>
2020

21-
#include <libsolidity/parsing/Parser.h>
22-
#include <libsolutil/StringUtils.h>
2321
#include <libsolutil/CommonIO.h>
2422

2523
#include <boost/algorithm/string.hpp>
26-
#include <boost/throw_exception.hpp>
2724
#include <boost/filesystem.hpp>
2825

29-
#include <range/v3/view/map.hpp>
30-
3126
using namespace std;
32-
using namespace solidity::langutil;
3327
using namespace solidity::frontend::test;
3428

3529
namespace fs = boost::filesystem;
@@ -162,7 +156,7 @@ pair<SourceMap, size_t> TestCaseReader::parseSourcesAndSettingsWithLineNumber(is
162156
else
163157
externalSourceName = externalSourceString;
164158

165-
solAssert(!externalSourceName.empty(), "");
159+
soltestAssert(!externalSourceName.empty(), "");
166160
fs::path externalSourceTarget(externalSourceString);
167161
fs::path testCaseParentDir = m_fileName.parent_path();
168162
if (!externalSourceTarget.is_relative())

test/TestCaseReader.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616
*/
1717
// SPDX-License-Identifier: GPL-3.0
1818

19+
#pragma once
20+
21+
#include <test/libsolidity/util/SoltestErrors.h>
22+
23+
#include <libsolutil/StringUtils.h>
24+
25+
#include <range/v3/view/map.hpp>
26+
1927
#include <boost/filesystem.hpp>
28+
#include <boost/throw_exception.hpp>
29+
2030
#include <fstream>
2131
#include <map>
2232
#include <string>
2333

24-
#pragma once
25-
2634
namespace solidity::frontend::test
2735
{
2836

@@ -58,6 +66,9 @@ class TestCaseReader
5866
size_t sizetSetting(std::string const& _name, size_t _defaultValue);
5967
std::string stringSetting(std::string const& _name, std::string const& _defaultValue);
6068

69+
template <typename E>
70+
E enumSetting(std::string const& _name, std::map<std::string, E> const& _choices, std::string const& _defaultChoice);
71+
6172
void ensureAllSettingsRead() const;
6273

6374
private:
@@ -71,4 +82,20 @@ class TestCaseReader
7182
std::map<std::string, std::string> m_settings;
7283
std::map<std::string, std::string> m_unreadSettings; ///< tracks which settings are left unread
7384
};
85+
86+
template <typename E>
87+
E TestCaseReader::enumSetting(std::string const& _name, std::map<std::string, E> const& _choices, std::string const& _defaultChoice)
88+
{
89+
soltestAssert(_choices.count(_defaultChoice) > 0, "");
90+
91+
std::string value = stringSetting(_name, _defaultChoice);
92+
93+
if (_choices.count(value) == 0)
94+
BOOST_THROW_EXCEPTION(std::runtime_error(
95+
"Invalid Enum value: " + value + ". Available choices: " + util::joinHumanReadable(_choices | ranges::views::keys) + "."
96+
));
97+
98+
return _choices.at(value);
99+
}
100+
74101
}

0 commit comments

Comments
 (0)