Files
ResInsight/ApplicationLibCode/UnitTests/RimcNestedCollectionBase-Test.cpp
T
Stefan Crooijmans ece73a749c #15008 Python: Return the displayed folder name from collection_name()
collection_name() returned the stored m_collectionName, which is empty
for a RimPolygonFile or RimPolygonCloudAddress without a name. Both
classes fall back to the file name or a default ("Polygon File", "Sumo
Polygon Address") in name().

PdmNestedCollectionBase::displayName() is a virtual that defaults to
collectionName(); RimPolygonFile and RimPolygonCloudAddress override it
with name(), and CollectionNameInternal returns displayName().
collectionName() itself is unchanged, since findSubCollectionByName()
and the name conflict policy compare stored names.

ResInsight-tests: RimcNestedCollectionBase-Test covers a polygon
collection, a polygon file (default, base name, file name fallback) and
a cloud polygon address.
2026-11-05 08:50:36 +01:00

85 lines
3.3 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "gtest/gtest.h"
#include "Polygons/Cloud/RimPolygonCloudAddress.h"
#include "Polygons/RimPolygonCollection.h"
#include "Polygons/RimPolygonFile.h"
#include "RimcDataContainerString.h"
#include "RimcNestedCollectionBase.h"
#include <memory>
namespace
{
QString scriptCollectionName( caf::PdmNestedCollectionBase* collection )
{
RimcNestedCollectionBase_collectionName method( collection );
auto result = method.execute();
if ( !result.has_value() ) return "<error>";
std::unique_ptr<RimcDataContainerString> strings( dynamic_cast<RimcDataContainerString*>( result.value() ) );
if ( !strings || strings->m_stringValues().size() != 1 ) return "<error>";
return strings->m_stringValues().front();
}
} // namespace
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RimcNestedCollectionBase, CollectionNameReturnsStoredName )
{
RimPolygonCollection collection;
collection.setCollectionName( "Folder A" );
EXPECT_EQ( "Folder A", scriptCollectionName( &collection ) );
}
//--------------------------------------------------------------------------------------------------
/// A polygon file falls back to the file name, or to a default, when no name is stored.
//--------------------------------------------------------------------------------------------------
TEST( RimcNestedCollectionBase, CollectionNameUsesPolygonFileFallback )
{
RimPolygonFile polygonFile;
EXPECT_TRUE( polygonFile.collectionName().isEmpty() );
EXPECT_EQ( "Polygon File", scriptCollectionName( &polygonFile ) );
// Setting the file stores the base name of the file as the folder name
polygonFile.setFileName( "/data/polygons/outline.pol" );
EXPECT_EQ( "outline", scriptCollectionName( &polygonFile ) );
polygonFile.setCollectionName( "" );
EXPECT_EQ( "outline.pol", scriptCollectionName( &polygonFile ) );
EXPECT_EQ( polygonFile.name(), scriptCollectionName( &polygonFile ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RimcNestedCollectionBase, CollectionNameUsesCloudAddressFallback )
{
RimPolygonCloudAddress address;
EXPECT_EQ( "Sumo Polygon Address", scriptCollectionName( &address ) );
address.setCollectionName( "Outline" );
EXPECT_EQ( "Outline", scriptCollectionName( &address ) );
}