Add unit test and implement most functions

This commit is contained in:
Magne Sjaastad 2024-03-08 15:13:36 +01:00
parent 3e734737ae
commit 2f86743b44
4 changed files with 317 additions and 0 deletions

View File

@ -11,4 +11,179 @@ AppEnumMapper* AppEnumMapper::instance()
return singleton;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void AppEnumMapper::addItem( const std::string& enumKey,
int enumValue,
const QString& text,
const QString& uiText,
const QStringList& aliases /*= {} */ )
{
// Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read back
// from XML text https://github.com/OPM/ResInsight/issues/7829
m_enumMap[enumKey].emplace_back( EnumData( enumValue, text.trimmed(), uiText, aliases ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void AppEnumMapper::addDefaultItem( const std::string& enumKey,
int enumValue,
const QString& text,
const QString& uiText,
const QStringList& aliases /*= {} */ )
{
// Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read back
// from XML text https://github.com/OPM/ResInsight/issues/7829
auto enumData = AppEnumMapper::EnumData( enumValue, text.trimmed(), uiText, aliases );
enumData.m_isDefault = true;
m_enumMap[enumKey].emplace_back( enumData );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t AppEnumMapper::size( const std::string& enumKey ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
return it->second.size();
}
return 0;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int AppEnumMapper::defaultEnumValue( const std::string& enumKey ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
for ( const auto& enumData : it->second )
{
if ( enumData.m_isDefault )
{
return enumData.m_enumVal;
}
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t AppEnumMapper::index( const std::string& enumKey, int enumValue ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
for ( size_t i = 0; i < it->second.size(); ++i )
{
if ( it->second[i].m_enumVal == enumValue )
{
return i;
}
}
}
return std::numeric_limits<size_t>::max();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString AppEnumMapper::text( const std::string& enumKey, int enumValue ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
for ( const auto& enumData : it->second )
{
if ( enumData.m_enumVal == enumValue )
{
return enumData.m_text;
}
}
}
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString AppEnumMapper::uiText( const std::string& enumKey, int enumValue ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
for ( const auto& enumData : it->second )
{
if ( enumData.m_enumVal == enumValue )
{
return enumData.m_uiText;
}
}
}
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int AppEnumMapper::enumValue( const std::string& enumKey, const QString& text ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
for ( const auto& enumData : it->second )
{
if ( enumData.isMatching( text ) )
{
return enumData.m_enumVal;
}
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int AppEnumMapper::enumValue( const std::string& enumKey, int enumIndex ) const
{
auto it = m_enumMap.find( enumKey );
if ( it != m_enumMap.end() )
{
if ( enumIndex < it->second.size() )
{
return it->second[enumIndex].m_enumVal;
}
}
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
AppEnumMapper::EnumData::EnumData( int enumVal, const QString& text, const QString& uiText, const QStringList& aliases )
: m_enumVal( enumVal )
, m_text( text )
, m_uiText( uiText )
, m_aliases( aliases )
, m_isDefault( false )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool AppEnumMapper::EnumData::isMatching( const QString& text ) const
{
return ( text == m_text || m_aliases.contains( text ) );
}
} //namespace caf

View File

@ -1,14 +1,61 @@
#pragma once
#include <QString>
#include <QStringList>
#include <map>
#include <optional>
#include <string>
namespace caf
{
//==================================================================================================
//
//==================================================================================================
class AppEnumMapper
{
private:
class EnumData
{
public:
EnumData( int enumVal, const QString& text, const QString& uiText, const QStringList& aliases );
bool isMatching( const QString& text ) const;
int m_enumVal;
QString m_text;
QString m_uiText;
QStringList m_aliases;
bool m_isDefault;
};
public:
static AppEnumMapper* instance();
void addItem( const std::string& enumKey,
int enumValue,
const QString& text,
const QString& uiText,
const QStringList& aliases = {} );
void addDefaultItem( const std::string& enumKey,
int enumValue,
const QString& text,
const QString& uiText,
const QStringList& aliases = {} );
size_t size( const std::string& enumKey ) const;
size_t index( const std::string& enumKey, int enumValue ) const;
QString text( const std::string& enumKey, int enumValue ) const;
QString uiText( const std::string& enumKey, int enumValue ) const;
int defaultEnumValue( const std::string& enumKey ) const;
int enumValue( const std::string& enumKey, const QString& text ) const;
int enumValue( const std::string& enumKey, int enumIndex ) const;
private:
std::map<std::string, std::vector<EnumData>> m_enumMap;
};
} // end namespace caf

View File

@ -41,6 +41,7 @@ set(PROJECT_FILES
Parent.h
TestObj.cpp
TestObj.h
cafPdmEnumMapperTest.cpp
)
if(CEE_USE_QT6)

View File

@ -0,0 +1,94 @@
#include "gtest/gtest.h"
#include "cafAppEnum.h"
#include "cafAppEnumMapper.h"
#include "cafTypeNameHelper.h"
enum class TestEnum
{
Value1 = 5,
Value2,
Value3,
Value4
};
enum class TestEnum2
{
ValueA,
ValueB,
ValueC
};
namespace caf
{
template <>
void caf::AppEnum<TestEnum>::setUp()
{
addItem( TestEnum::Value1, "VALUE_1", "Val 1" );
addItem( TestEnum::Value2, "VALUE_2", "Val 2" );
addItem( TestEnum::Value3, "VALUE_3", "Val 3" );
addItem( TestEnum::Value4, "VALUE_4", "Val 4" );
setDefault( TestEnum::Value2 );
}
template <>
void caf::AppEnum<TestEnum2>::setUp()
{
addItem( TestEnum2::ValueA, "VALUE_A", "Val A" );
addItem( TestEnum2::ValueB, "VALUE_B", "Val B" );
addItem( TestEnum2::ValueC, "VALUE_C", "Val C" );
setDefault( TestEnum2::ValueC );
}
} //namespace caf
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( PdmEnumMapperTest, CreateInstance )
{
caf::AppEnumMapper* instance = caf::AppEnumMapper::instance();
EXPECT_NE( nullptr, instance );
auto TestEnum_key = caf::cafTypeName<TestEnum>();
auto TestEnum2_key = caf::cafTypeName<TestEnum2>();
{
instance->addItem( TestEnum_key, caf::convertToInteger<TestEnum>( TestEnum::Value1 ), "VALUE_1", "Val 1" );
instance->addItem( TestEnum_key, caf::convertToInteger<TestEnum>( TestEnum::Value2 ), "VALUE_2", "Val 2" );
instance->addItem( TestEnum_key, caf::convertToInteger<TestEnum>( TestEnum::Value3 ), "VALUE_3", "Val 3" );
instance->addItem( TestEnum_key, caf::convertToInteger<TestEnum>( TestEnum::Value4 ), "VALUE_4", "Val 4" );
}
{
instance->addItem( TestEnum2_key, caf::convertToInteger<TestEnum2>( TestEnum2::ValueA ), "VALUE_A", "Val A" );
instance->addItem( TestEnum2_key, caf::convertToInteger<TestEnum2>( TestEnum2::ValueB ), "VALUE_B", "Val B" );
instance->addItem( TestEnum2_key, caf::convertToInteger<TestEnum2>( TestEnum2::ValueC ), "VALUE_C", "Val C" );
}
EXPECT_EQ( 4, instance->size( TestEnum_key ) );
EXPECT_EQ( 3, instance->size( TestEnum2_key ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( PdmEnumMapperTest, ConverEnumToInteger )
{
caf::AppEnumMapper* instance = caf::AppEnumMapper::instance();
{
auto sourceEnumValue = TestEnum::Value1;
auto intValue = caf::convertToInteger( sourceEnumValue );
auto enumValue = caf::convertToEnum<TestEnum>( intValue );
EXPECT_EQ( enumValue, sourceEnumValue );
}
{
auto sourceEnumValue = TestEnum2::ValueC;
auto intValue = caf::convertToInteger( sourceEnumValue );
auto enumValue = caf::convertToEnum<TestEnum2>( intValue );
EXPECT_EQ( enumValue, sourceEnumValue );
}
}