Made sure an exception is also thrown for too few columns. Added a test for this use case.

This commit is contained in:
rubenthoms 2020-08-14 18:08:35 +02:00 committed by rubenthoms
parent 3a5ef7fb86
commit 0da301a785
4 changed files with 4360 additions and 6 deletions

View File

@ -111,8 +111,9 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
if ( file && expectedColumnCount > 0 )
{
QTextStream stream( file );
bool dataBlockFound = false;
int lineNo = 0;
bool dataBlockFound = false;
bool completeDataLineFound = false;
int lineNo = 0;
QStringList collectedCols;
// Init data vectors
@ -126,6 +127,11 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
{
if ( collectedCols.size() > 0 && collectedCols.size() != 8 )
{
if ( dataBlockFound )
{
throw FileParseException(
QString( "Number of columns mismatch at %1:%2" ).arg( metadata->fileName ).arg( lineNo ) );
}
collectedCols.clear();
}
@ -137,10 +143,11 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
}
lineNo++;
if ( !dataBlockFound )
if ( !completeDataLineFound )
{
if ( !line.startsWith( "*" ) && !line.startsWith( "," ) && collectedCols.size() == expectedColumnCount )
{
completeDataLineFound = true;
dataBlockFound = true;
}
else if ( collectedCols.size() > expectedColumnCount )
@ -185,7 +192,7 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
}
}
collectedCols.clear();
dataBlockFound = false;
completeDataLineFound = false;
}
table->hasData = true;

View File

@ -36,12 +36,33 @@ TEST( RicElementPropertyTableReaderTest, BasicUsage )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ParseFailed )
TEST( RicElementPropertyTableReaderTest, ParseFailedForTooManyColumns )
{
try
{
RifElementPropertyMetadata metadata =
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE_error.inp" );
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE_error_too_many_columns.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );
EXPECT_TRUE( false );
}
catch ( FileParseException e )
{
EXPECT_TRUE( e.message.startsWith( "Number of columns mismatch" ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ParseFailedForTooFewColumns )
{
try
{
RifElementPropertyMetadata metadata = RifElementPropertyTableReader::readMetadata(
ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE_error_too_few_columns.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );