Files
ResInsight/ApplicationLibCode/UnitTests/RifFormationNamesFileParser-Test.cpp
T
Magne Sjaastad fd0ec10d6b #13889: Import zone colors from stratigraphy.json and zones from yaml
Import zone colors from an FMU stratigraphy.json file located next to the
formation files, and import formation zones from a yaml file.

- Add RifFormationNamesFileParser and move the layer zone table, yaml and
  stratigraphy.json parsers there from RifFormationNamesReader.
- Return std::expected from convertStringToColor.
- Prefer zonation_ecl_map yaml when several yaml files are present, and ignore
  other yaml files.
- Document the FMU location of the formation files.
- Add unit tests and test data for fmu, malformed and missing-color inputs.

Issues: #13889, #13888
2026-10-20 13:36:02 +02:00

105 lines
3.9 KiB
C++

#include "gtest/gtest.h"
#include "RiaTestDataDirectory.h"
#include "RifFormationNamesFileParser.h"
#include "RigFormationNames.h"
#include "cvfColor3.h"
#include <QDir>
#include <QString>
namespace
{
QString testFile( const QString& relativePath )
{
return QDir( TEST_DATA_DIR ).absoluteFilePath( "RifFormationNamesReader/" + relativePath );
}
} // namespace
TEST( RifFormationNamesFileParser, ConvertStringToColor )
{
auto named = RifFormationNamesFileParser::convertStringToColor( "red" );
ASSERT_TRUE( named.has_value() );
EXPECT_EQ( 1.0f, named->r() );
EXPECT_EQ( 0.0f, named->g() );
auto hex = RifFormationNamesFileParser::convertStringToColor( "#0000FF" );
ASSERT_TRUE( hex.has_value() );
EXPECT_EQ( 1.0f, hex->b() );
EXPECT_FALSE( RifFormationNamesFileParser::convertStringToColor( "" ).has_value() );
EXPECT_FALSE( RifFormationNamesFileParser::convertStringToColor( "not-a-color" ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadLyrFile )
{
auto fm = RifFormationNamesFileParser::readLyrFormationNameFile( testFile( "Norne_ATW2013ColorName.lyr" ) );
ASSERT_TRUE( fm.has_value() );
EXPECT_EQ( "Garn 3", fm->formationNameFromKLayerIdx( 0 ) );
cvf::Color3f color;
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 1, &color ) );
EXPECT_EQ( 1.0f, color.r() );
EXPECT_FALSE( RifFormationNamesFileParser::readLyrFormationNameFile( testFile( "missing.lyr" ) ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadFmuFileWithExplicitColors )
{
std::map<QString, cvf::Color3f> colors = { { "Therys", cvf::Color3f( 0.0f, 1.0f, 0.0f ) } };
auto fm = RifFormationNamesFileParser::readFmuFormationNameFile( testFile( "fmu_nocolors/layer_zone_table.txt" ), colors );
ASSERT_TRUE( fm.has_value() );
cvf::Color3f color;
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 0, &color ) );
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 1, &color ) );
EXPECT_EQ( 1.0f, color.g() );
EXPECT_FALSE( RifFormationNamesFileParser::readFmuFormationNameFile( testFile( "missing.txt" ) ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadYamlFile )
{
auto fm = RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "fmu/zonation_ecl_map.yml" ) );
ASSERT_TRUE( fm.has_value() );
// Invalid and unrelated lines are skipped
EXPECT_EQ( 3u, fm->formationNames().size() );
EXPECT_EQ( "Valysar", fm->formationNameFromKLayerIdx( 0 ) );
EXPECT_EQ( "Therys", fm->formationNameFromKLayerIdx( 20 ) );
EXPECT_EQ( "Volon", fm->formationNameFromKLayerIdx( 54 ) );
cvf::Color3f color;
EXPECT_FALSE( fm->formationColorFromKLayerIdx( 0, &color ) );
}
TEST( RifFormationNamesFileParser, ReadYamlFileWithColorsAndErrors )
{
std::map<QString, cvf::Color3f> colors = { { "Valysar", cvf::Color3f( 1.0f, 0.0f, 0.0f ) } };
auto fm = RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "fmu/zonation_ecl_map.yml" ), colors );
ASSERT_TRUE( fm.has_value() );
cvf::Color3f color;
ASSERT_TRUE( fm->formationColorFromKLayerIdx( 5, &color ) );
EXPECT_EQ( 1.0f, color.r() );
EXPECT_FALSE( RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "fmu_malformed/zonation_ecl_map.yaml" ) ).has_value() );
EXPECT_FALSE( RifFormationNamesFileParser::readYamlFormationNameFile( testFile( "missing.yml" ) ).has_value() );
}
TEST( RifFormationNamesFileParser, ReadStratigraphyColorsSubzonePrecedence )
{
auto colors = RifFormationNamesFileParser::readStratigraphyColors( testFile( "fmu/stratigraphy.json" ) );
ASSERT_TRUE( colors.has_value() );
EXPECT_EQ( 1.0f, colors->at( "Valysar" ).r() );
EXPECT_EQ( 1.0f, colors->at( "Therys" ).b() );
EXPECT_EQ( 1u, colors->count( "Other" ) );
EXPECT_EQ( 0u, colors->count( "Volon" ) );
EXPECT_FALSE( RifFormationNamesFileParser::readStratigraphyColors( testFile( "missing.json" ) ).has_value() );
}