From 7ca0140e658e21c41f0a7b23f6c64ca6a2fa67aa Mon Sep 17 00:00:00 2001 From: mattias Date: Fri, 18 Nov 2016 17:44:17 +0000 Subject: [PATCH] codetools: fixed StringToPascalConst, added tests, bug #30955 git-svn-id: trunk@53383 - --- components/codetools/basiccodetools.pas | 10 ++-- .../codetools/tests/testbasiccodetools.pas | 54 ++++++++++++++----- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/components/codetools/basiccodetools.pas b/components/codetools/basiccodetools.pas index b632f1b027..7f642eb251 100644 --- a/components/codetools/basiccodetools.pas +++ b/components/codetools/basiccodetools.pas @@ -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 diff --git a/components/codetools/tests/testbasiccodetools.pas b/components/codetools/tests/testbasiccodetools.pas index 6a5d6cf34f..eb124f0261 100644 --- a/components/codetools/tests/testbasiccodetools.pas +++ b/components/codetools/tests/testbasiccodetools.pas @@ -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);