codetools: fixed StringToPascalConst, added tests, bug #30955

git-svn-id: trunk@53383 -
This commit is contained in:
mattias 2016-11-18 17:44:17 +00:00
parent c2d88c8f48
commit 7ca0140e65
2 changed files with 46 additions and 18 deletions

View File

@ -5101,7 +5101,8 @@ begin
end;
function StringToPascalConst(const s: string): string;
// converts s to
// converts s to a Pascal string literal
// e.g. foo becomes 'foo', bytes 0..31 become #ord
function Convert(var DestStr: string): integer;
var
@ -5111,8 +5112,9 @@ function StringToPascalConst(const s: string): string;
InString: Boolean;
begin
SrcLen:=length(s);
DestPos:=0;
InString:=false;
DestPos:=1;
if DestStr<>'' then DestStr[DestPos]:='''';
InString:=true;
for SrcPos:=1 to SrcLen do begin
inc(DestPos);
c:=s[SrcPos];
@ -5126,8 +5128,8 @@ function StringToPascalConst(const s: string): string;
if DestStr<>'' then
DestStr[DestPos]:=c;
if c='''' then begin
if DestStr<>'' then DestStr[DestPos]:='''';
inc(DestPos);
if DestStr<>'' then DestStr[DestPos]:='''';
end;
end else begin
// special char

View File

@ -1,19 +1,23 @@
{
Test with:
./runtests --format=plain --suite=TTestBasicCodeTools
./runtests --format=plain --suite=TestFindLineEndOrCodeInFrontOfPosition
./runtests --format=plain --suite=TestHasTxtWord
./runtests --format=plain --suite=TestBasicFindCommentEnd
./runtests --format=plain --suite=TestBasicFindNextComment
./runtests --format=plain --suite=TestCompareTextIgnoringSpace
./runtests --format=plain --suite=TestCleanCodeFromComments
./runtests --format=plain --suite=TestGuessIndentSize
./runtests --format=plain --suite=TestReindent
./runtests --format=plain --suite=TestSimpleFormat
./runtests --format=plain --suite=TestDateToCfgStr
./runtests --format=plain --suite=TestFilenameIsMatching
./runtests --format=plain --suite=TestExtractFileUnitname
./runtests --format=plain --suite=TestChangeLineEndings
./testcodetools --format=plain --suite=TTestBasicCodeTools
./testcodetools --format=plain --suite=TestFindLineEndOrCodeInFrontOfPosition
./testcodetools --format=plain --suite=TestHasTxtWord
./testcodetools --format=plain --suite=TestBasicFindCommentEnd
./testcodetools --format=plain --suite=TestBasicFindNextComment
./testcodetools --format=plain --suite=TestCompareTextIgnoringSpace
./testcodetools --format=plain --suite=TestCleanCodeFromComments
./testcodetools --format=plain --suite=TestGuessIndentSize
./testcodetools --format=plain --suite=TestReindent
./testcodetools --format=plain --suite=TestSimpleFormat
./testcodetools --format=plain --suite=TestStringToPascalConst
./testcodetools --format=plain --suite=TestDateToCfgStr
./testcodetools --format=plain --suite=TestFilenameIsMatching
./testcodetools --format=plain --suite=TestExtractFileUnitname
./testcodetools --format=plain --suite=TestParseFPCParameters
./testcodetools --format=plain --suite=TestChangeLineEndings
}
unit TestBasicCodetools;
@ -41,6 +45,7 @@ type
procedure TestGuessIndentSize;
procedure TestReIndent;
procedure TestSimpleFormat;
procedure TestStringToPascalConst;
// FileProcs
procedure TestDateToCfgStr;
procedure TestFilenameIsMatching;
@ -292,6 +297,27 @@ begin
t('A%1:s%0:sB',['Foo','Bar'],'ABarFooB');
end;
procedure TTestBasicCodeTools.TestStringToPascalConst;
procedure t(s, Expected: string);
var
Actual: String;
begin
Actual:=StringToPascalConst(s);
AssertEquals('StringToPascalConst <'+DbgStr(s)+'>',Expected,Actual);
end;
begin
t('','''''');
t('Foo','''Foo''');
t('Foo"','''Foo"''');
t('Foo''','''Foo''''''');
t('Fo''o','''Fo''''o''');
t('''Foo','''''''Foo''');
t('Foo'#10,'''Foo''#10');
t('Foo'#10'Bar','''Foo''#10''Bar''');
end;
procedure TTestBasicCodeTools.TestDateToCfgStr;
procedure t(const Date: TDateTime; const aFormat, Expected: string);