mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-20 18:09:27 +02:00
* Fix bug ID #30469 (allow quoting to start inside values)
git-svn-id: trunk@34509 -
This commit is contained in:
parent
98020a9b57
commit
7fec0f92af
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -2075,6 +2075,7 @@ packages/fcl-base/src/wince/fileinfo.pp svneol=native#text/plain
|
||||
packages/fcl-base/src/wtex.pp svneol=native#text/plain
|
||||
packages/fcl-base/tests/fclbase-unittests.lpi svneol=native#text/plain
|
||||
packages/fcl-base/tests/fclbase-unittests.pp svneol=native#text/plain
|
||||
packages/fcl-base/tests/tccsvreadwrite.pp svneol=native#text/plain
|
||||
packages/fcl-base/tests/tchashlist.pp svneol=native#text/plain
|
||||
packages/fcl-base/tests/tcinifile.pp svneol=native#text/plain
|
||||
packages/fcl-base/tests/tcmaskutils.pp svneol=native#text/plain
|
||||
|
@ -396,15 +396,17 @@ end;
|
||||
|
||||
procedure TCSVParser.ParseValue;
|
||||
begin
|
||||
while not ((FCurrentChar = FDelimiter) or EndOfLine or EndOfFile) do
|
||||
while not ((FCurrentChar = FDelimiter) or EndOfLine or EndOfFile or (FCurrentChar = FQuoteChar)) do
|
||||
begin
|
||||
AppendStr(FWhitespaceBuffer, FCurrentChar);
|
||||
AppendStr(FCellBuffer, FCurrentChar);
|
||||
NextChar;
|
||||
end;
|
||||
if FCurrentChar = FQuoteChar then
|
||||
ParseQuotedValue;
|
||||
// merge whitespace buffer
|
||||
if FIgnoreOuterWhitespace then
|
||||
RemoveTrailingChars(FWhitespaceBuffer, WhitespaceChars);
|
||||
AppendStr(FCellBuffer, FWhitespaceBuffer);
|
||||
AppendStr(FWhitespaceBuffer,FCellBuffer);
|
||||
FWhitespaceBuffer := '';
|
||||
end;
|
||||
|
||||
|
@ -30,10 +30,10 @@
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
<CommandLineParams Value="--suite=TTestExpressionScanner.TestNumber"/>
|
||||
<CommandLineParams Value="--suite=TTestCSVReadWrite.TestInlineQuotedLine"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="5">
|
||||
<Units Count="6">
|
||||
<Unit0>
|
||||
<Filename Value="fclbase-unittests.pp"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
@ -55,6 +55,10 @@
|
||||
<Filename Value="tcinifile.pp"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit4>
|
||||
<Unit5>
|
||||
<Filename Value="tccsvreadwrite.pp"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit5>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
|
@ -4,7 +4,7 @@ program fclbase_unittests;
|
||||
|
||||
uses
|
||||
Classes, consoletestrunner, tests_fptemplate, tchashlist,
|
||||
testexprpars, tcmaskutils, tcinifile;
|
||||
testexprpars, tcmaskutils, tcinifile, tccsvreadwrite;
|
||||
|
||||
var
|
||||
Application: TTestRunner;
|
||||
|
118
packages/fcl-base/tests/tccsvreadwrite.pp
Normal file
118
packages/fcl-base/tests/tccsvreadwrite.pp
Normal file
@ -0,0 +1,118 @@
|
||||
unit tccsvreadwrite;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fpcunit, testutils, testregistry, csvreadwrite;
|
||||
|
||||
type
|
||||
|
||||
{ TTestCSVReadWrite }
|
||||
|
||||
TTestCSVReadWrite= class(TTestCase)
|
||||
private
|
||||
FData: TStrings;
|
||||
FParser: TCSVParser;
|
||||
procedure AssertLine(ARow: Integer; AValues: array of string);
|
||||
procedure HaveNext(ARow, ACol: integer; AValue: String);
|
||||
protected
|
||||
procedure SetUp; override;
|
||||
procedure TearDown; override;
|
||||
Property Parser : TCSVParser Read FParser;
|
||||
Property Data : TStrings Read FData;
|
||||
published
|
||||
procedure TestEmpty;
|
||||
Procedure TestNormalLine;
|
||||
Procedure TestQuotedLine;
|
||||
Procedure TestInlineQuotedLine;
|
||||
Procedure TestQuotedNewLine;
|
||||
Procedure Test2Lines;
|
||||
Procedure TestEscapedQuotes;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
procedure TTestCSVReadWrite.TestEmpty;
|
||||
begin
|
||||
AssertNotNull('Have parser',Parser);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.HaveNext(ARow,ACol: integer; AValue : String);
|
||||
|
||||
Var
|
||||
CN : String;
|
||||
|
||||
begin
|
||||
CN:=Format('Cell(row: %d, col: %d)',[ARow,ACol]);
|
||||
AssertTrue('Have '+CN,Parser.ParseNextCell);
|
||||
AssertEquals(CN+': Row matches',ARow,Parser.CurrentRow);
|
||||
AssertEquals(CN+': Col matched',ACol,Parser.CurrentCol);
|
||||
AssertEquals(CN+': Value',AValue,Parser.CurrentCellText);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.AssertLine(ARow: Integer; AValues: array of string);
|
||||
|
||||
Var
|
||||
I : Integer;
|
||||
|
||||
begin
|
||||
For I:=0 to Length(AValues)-1 do
|
||||
HaveNext(ARow,I,AValues[i]);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.TestNormalLine;
|
||||
begin
|
||||
FParser.SetSource('this,is,a,normal,line');
|
||||
AssertLine(0,['this','is','a','normal','line']);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.TestQuotedLine;
|
||||
begin
|
||||
FParser.SetSource('"this","is","a","quoted","line"');
|
||||
AssertLine(0,['this','is','a','quoted','line']);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.TestInlineQuotedLine;
|
||||
begin
|
||||
FParser.SetSource('"this","line",has,mixed" quoting"');
|
||||
AssertLine(0,['this','line','has','mixed quoting']);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.TestQuotedNewLine;
|
||||
begin
|
||||
FParser.SetSource('"this","line",has,"an embedded'+lineEnding+'newline"');
|
||||
AssertLine(0,['this','line','has','an embedded'+lineending+'newline']);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.Test2Lines;
|
||||
begin
|
||||
FParser.SetSource('"this","line",has,an embedded'+lineEnding+'newline');
|
||||
AssertLine(0,['this','line','has','an embedded']);
|
||||
AssertLine(1,['newline']);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.TestEscapedQuotes;
|
||||
begin
|
||||
FParser.SetSource('"this","line",has,"an embedded "" quote"');
|
||||
AssertLine(0,['this','line','has','an embedded " quote']);
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.SetUp;
|
||||
begin
|
||||
FParser:=TCSVParser.Create;
|
||||
FData:=Tstrings.Create;
|
||||
end;
|
||||
|
||||
procedure TTestCSVReadWrite.TearDown;
|
||||
begin
|
||||
FreeAndNil(FData);
|
||||
FreeAndNil(Fparser);
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
RegisterTest(TTestCSVReadWrite);
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user