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 ) if ( file && expectedColumnCount > 0 )
{ {
QTextStream stream( file ); QTextStream stream( file );
bool dataBlockFound = false; bool dataBlockFound = false;
int lineNo = 0; bool completeDataLineFound = false;
int lineNo = 0;
QStringList collectedCols; QStringList collectedCols;
// Init data vectors // Init data vectors
@ -126,6 +127,11 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
{ {
if ( collectedCols.size() > 0 && collectedCols.size() != 8 ) 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(); collectedCols.clear();
} }
@ -137,10 +143,11 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
} }
lineNo++; lineNo++;
if ( !dataBlockFound ) if ( !completeDataLineFound )
{ {
if ( !line.startsWith( "*" ) && !line.startsWith( "," ) && collectedCols.size() == expectedColumnCount ) if ( !line.startsWith( "*" ) && !line.startsWith( "," ) && collectedCols.size() == expectedColumnCount )
{ {
completeDataLineFound = true;
dataBlockFound = true; dataBlockFound = true;
} }
else if ( collectedCols.size() > expectedColumnCount ) else if ( collectedCols.size() > expectedColumnCount )
@ -185,7 +192,7 @@ void RifElementPropertyTableReader::readData( const RifElementPropertyMetadata*
} }
} }
collectedCols.clear(); collectedCols.clear();
dataBlockFound = false; completeDataLineFound = false;
} }
table->hasData = true; table->hasData = true;

View File

@ -36,12 +36,33 @@ TEST( RicElementPropertyTableReaderTest, BasicUsage )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ParseFailed ) TEST( RicElementPropertyTableReaderTest, ParseFailedForTooManyColumns )
{ {
try try
{ {
RifElementPropertyMetadata metadata = 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; RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table ); RifElementPropertyTableReader::readData( &metadata, &table );