2019-04-04 16:55:46 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 4/4/2019.
|
|
|
|
* Copyright 2019 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#include "catch_enum_values_registry.h"
|
|
|
|
#include "catch_string_manip.h"
|
|
|
|
#include "catch_stream.h"
|
|
|
|
|
|
|
|
#include <map>
|
2019-04-25 15:19:00 +02:00
|
|
|
#include <cassert>
|
2019-04-04 16:55:46 +02:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() {}
|
|
|
|
|
|
|
|
namespace Detail {
|
|
|
|
|
2019-09-08 14:07:18 +02:00
|
|
|
namespace {
|
|
|
|
// Extracts the actual name part of an enum instance
|
|
|
|
// In other words, it returns the Blue part of Bikeshed::Colour::Blue
|
|
|
|
StringRef extractInstanceName(StringRef enumInstance) {
|
|
|
|
// Find last occurence of ":"
|
|
|
|
size_t name_start = enumInstance.size();
|
|
|
|
while (name_start > 0 && enumInstance[name_start - 1] != ':') {
|
|
|
|
--name_start;
|
|
|
|
}
|
|
|
|
return enumInstance.substr(name_start, enumInstance.size() - name_start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> parseEnums( StringRef enums ) {
|
2019-04-21 19:03:44 +02:00
|
|
|
auto enumValues = splitStringRef( enums, ',' );
|
2019-09-08 14:07:18 +02:00
|
|
|
std::vector<StringRef> parsed;
|
2019-04-04 16:55:46 +02:00
|
|
|
parsed.reserve( enumValues.size() );
|
|
|
|
for( auto const& enumValue : enumValues ) {
|
2019-09-08 14:07:18 +02:00
|
|
|
parsed.push_back(trim(extractInstanceName(enumValue)));
|
2019-04-04 16:55:46 +02:00
|
|
|
}
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2019-04-21 18:56:19 +02:00
|
|
|
EnumInfo::~EnumInfo() {}
|
2019-04-04 16:55:46 +02:00
|
|
|
|
2019-04-21 18:56:19 +02:00
|
|
|
StringRef EnumInfo::lookup( int value ) const {
|
|
|
|
for( auto const& valueToName : m_values ) {
|
|
|
|
if( valueToName.first == value )
|
|
|
|
return valueToName.second;
|
2019-04-04 16:55:46 +02:00
|
|
|
}
|
2019-09-07 14:38:39 +02:00
|
|
|
return "{** unexpected enum value **}"_sr;
|
2019-04-21 18:56:19 +02:00
|
|
|
}
|
2019-04-04 16:55:46 +02:00
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
std::unique_ptr<EnumInfo> makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
|
2019-04-04 16:55:46 +02:00
|
|
|
std::unique_ptr<EnumInfo> enumInfo( new EnumInfo );
|
|
|
|
enumInfo->m_name = enumName;
|
2019-04-25 11:13:11 +02:00
|
|
|
enumInfo->m_values.reserve( values.size() );
|
2019-04-04 16:55:46 +02:00
|
|
|
|
|
|
|
const auto valueNames = Catch::Detail::parseEnums( allValueNames );
|
|
|
|
assert( valueNames.size() == values.size() );
|
|
|
|
std::size_t i = 0;
|
|
|
|
for( auto value : values )
|
2019-04-21 18:56:19 +02:00
|
|
|
enumInfo->m_values.push_back({ value, valueNames[i++] });
|
2019-04-04 16:55:46 +02:00
|
|
|
|
2019-04-25 11:13:11 +02:00
|
|
|
return enumInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
|
2019-09-08 14:07:18 +02:00
|
|
|
m_enumInfos.push_back(makeEnumInfo(enumName, allValueNames, values));
|
|
|
|
return *m_enumInfos.back();
|
2019-04-04 16:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Detail
|
|
|
|
} // Catch
|
|
|
|
|