tests: SimpleFormat

git-svn-id: trunk@47020 -
This commit is contained in:
mattias 2014-11-28 14:29:40 +00:00
parent e13175eeb4
commit bedd8d3376
5 changed files with 29 additions and 64 deletions

View File

@ -43,7 +43,7 @@ uses
Classes, SysUtils, strutils, contnrs, LCLProc, Forms, Controls, Buttons, Classes, SysUtils, strutils, contnrs, LCLProc, Forms, Controls, Buttons,
Dialogs, FileUtil, Laz2_XMLCfg, lazutf8classes, LazFileUtils, LazFileCache, Dialogs, FileUtil, Laz2_XMLCfg, lazutf8classes, LazFileUtils, LazFileCache,
LazLogger, Graphics, ComCtrls, ExtCtrls, StdCtrls, DefineTemplates, LazLogger, Graphics, ComCtrls, ExtCtrls, StdCtrls, DefineTemplates,
CodeToolManager, TransferMacros, MacroDefIntf, GDBMIDebugger, CodeToolManager, FileProcs, TransferMacros, MacroDefIntf, GDBMIDebugger,
DbgIntfDebuggerBase, LazarusIDEStrConsts, LazConf, EnvironmentOpts, IDEProcs, DbgIntfDebuggerBase, LazarusIDEStrConsts, LazConf, EnvironmentOpts, IDEProcs,
AboutFrm, IDETranslations, InitialSetupProc; AboutFrm, IDETranslations, InitialSetupProc;

View File

@ -34,8 +34,8 @@ interface
uses uses
Classes, SysUtils, strutils, LazFileCache, LazUTF8Classes, LazFileUtils, Classes, SysUtils, strutils, LazFileCache, LazUTF8Classes, LazFileUtils,
LazLoggerBase, LazUTF8, Laz2_XMLCfg, LazLogger, DefineTemplates, LazLoggerBase, LazUTF8, Laz2_XMLCfg, LazLogger, DefineTemplates,
CodeToolManager, LazarusIDEStrConsts, LazConf, EnvironmentOpts, IDEProcs, CodeToolManager, FileProcs, LazarusIDEStrConsts, LazConf, EnvironmentOpts,
contnrs; IDEProcs, contnrs;
type type
TSDFilenameQuality = ( TSDFilenameQuality = (
@ -106,7 +106,6 @@ function GetValueFromSecondaryConfig(OptionFilename, Path: string): string;
function GetValueFromIDEConfig(OptionFilename, Path: string): string; function GetValueFromIDEConfig(OptionFilename, Path: string): string;
function SafeFormat(const Fmt: String; const Args: Array of const): String; function SafeFormat(const Fmt: String; const Args: Array of const): String;
function SimpleFormat(const Fmt: String; const Args: Array of const): String;
implementation implementation
@ -912,44 +911,6 @@ begin
Result:=SimpleFormat(Fmt,Args); Result:=SimpleFormat(Fmt,Args);
end; end;
function SimpleFormat(const Fmt: String; const Args: array of const): String;
var
p: Integer;
i: Integer;
s: String;
begin
Result:=Fmt;
p:=1;
for i:=Low(Args) to High(Args) do
begin
case Args[i].VType of
vtString: s:=Args[i].VString^;
vtAnsiString: s:=AnsiString(Args[i].VAnsiString);
vtChar: s:=Args[i].VChar;
else continue;
end;
while (p<length(Result)) do begin
if (Result[p]='%') then
begin
if (Result[p+1]='s') then
break
else
inc(p,2);
end else
inc(p);
end;
if p<length(Result) then begin
// found %s => replace
ReplaceSubstring(Result,p,2,s);
inc(p,length(s));
end else begin
// missing %s => append
Result+=', '+s;
p:=length(s)+1;
end;
end;
end;
{ TSDFileInfoList } { TSDFileInfoList }
function TSDFileInfoList.AddNewItem(aFilename, aCaption: string): TSDFileInfo; function TSDFileInfoList.AddNewItem(aFilename, aCaption: string): TSDFileInfo;

View File

@ -8,6 +8,7 @@
./runtests --format=plain --suite=TestCompareTextIgnoringSpace ./runtests --format=plain --suite=TestCompareTextIgnoringSpace
./runtests --format=plain --suite=TestGuessIndentSize ./runtests --format=plain --suite=TestGuessIndentSize
./runtests --format=plain --suite=TestReindent ./runtests --format=plain --suite=TestReindent
./runtests --format=plain --suite=TestSimpleFormat
} }
unit TestBasicCodetools; unit TestBasicCodetools;
@ -31,6 +32,7 @@ type
procedure TestCompareTextIgnoringSpace; procedure TestCompareTextIgnoringSpace;
procedure TestGuessIndentSize; procedure TestGuessIndentSize;
procedure TestReIndent; procedure TestReIndent;
procedure TestSimpleFormat;
end; end;
implementation implementation
@ -215,6 +217,29 @@ begin
t('A'#10' B'#10,2,4, 3,0,'A'#10' B'#10); t('A'#10' B'#10,2,4, 3,0,'A'#10' B'#10);
end; end;
procedure TTestBasicCodeTools.TestSimpleFormat;
procedure t(const Fmt: string; Args: array of const; const Expected: string);
var
Actual: String;
begin
Actual:=SimpleFormat(Fmt,Args);
if Expected=Actual then exit;
writeln(dbgsDiff(Expected,Actual));
AssertEquals('"'+DbgStr(Fmt)+'"('+dbgs(High(Args)-Low(Args)+1)+')',true,false);
end;
begin
t('A',['Foo'],'A,Foo');
t('A%sB',['Foo'],'AFooB');
t('A%sB%sC',['Foo'],'AFooB%sC');
t('A%sB',['Foo','Bar'],'AFooB,Bar');
t('A%0B',['Foo','Bar'],'AFooB,Bar');
t('A%1B',['Foo','Bar'],'ABarB,Foo');
t('A%1%0B',['Foo','Bar'],'ABarFooB');
t('A%1:s%0:sB',['Foo','Bar'],'ABarFooB');
end;
initialization initialization
AddToCodetoolsTestSuite(TTestBasicCodeTools); AddToCodetoolsTestSuite(TTestBasicCodeTools);

View File

@ -66,6 +66,7 @@ begin
AssertEquals('last multi','adef',r('abc',2,2,'def')); AssertEquals('last multi','adef',r('abc',2,2,'def'));
AssertEquals('middle chars same','abcde',r('abcde',2,3,'bcd')); AssertEquals('middle chars same','abcde',r('abcde',2,3,'bcd'));
AssertEquals('middle chars shorten','axe',r('abcde',2,3,'x')); AssertEquals('middle chars shorten','axe',r('abcde',2,3,'x'));
AssertEquals('after chars','abcx',r('abc',4,3,'x'));
end; end;
procedure TTestLazUtils.TestSplitCmdLineParams; procedure TTestLazUtils.TestSplitCmdLineParams;

View File

@ -48,11 +48,7 @@
</Checks> </Checks>
</CodeGeneration> </CodeGeneration>
<Other> <Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CustomOptions Value="-dNoSemiAutomatedTests"/> <CustomOptions Value="-dNoSemiAutomatedTests"/>
<CompilerPath Value="$(CompPath)"/>
</Other> </Other>
</CompilerOptions> </CompilerOptions>
</Item2> </Item2>
@ -90,37 +86,30 @@
<Unit0> <Unit0>
<Filename Value="runtests.lpr"/> <Filename Value="runtests.lpr"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="runtests"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="testlpi.pas"/> <Filename Value="testlpi.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestLpi"/>
</Unit1> </Unit1>
<Unit2> <Unit2>
<Filename Value="bugtestcase.pas"/> <Filename Value="bugtestcase.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="BugTestCase"/>
</Unit2> </Unit2>
<Unit3> <Unit3>
<Filename Value="testglobals.pas"/> <Filename Value="testglobals.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="testglobals"/>
</Unit3> </Unit3>
<Unit4> <Unit4>
<Filename Value="lcltests\testunicode.pas"/> <Filename Value="lcltests\testunicode.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestUnicode"/>
</Unit4> </Unit4>
<Unit5> <Unit5>
<Filename Value="testunits.pas"/> <Filename Value="testunits.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="testunits"/>
</Unit5> </Unit5>
<Unit6> <Unit6>
<Filename Value="lcltests\testpen.pas"/> <Filename Value="lcltests\testpen.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestPen"/>
</Unit6> </Unit6>
<Unit7> <Unit7>
<Filename Value="lcltests\testtextstrings.pas"/> <Filename Value="lcltests\testtextstrings.pas"/>
@ -135,17 +124,14 @@
<Unit9> <Unit9>
<Filename Value="lazutils\testlazutils.pas"/> <Filename Value="lazutils\testlazutils.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestLazUtils"/>
</Unit9> </Unit9>
<Unit10> <Unit10>
<Filename Value="codetoolstests\testctxmlfixfragments.pas"/> <Filename Value="codetoolstests\testctxmlfixfragments.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestCTXMLFixFragments"/>
</Unit10> </Unit10>
<Unit11> <Unit11>
<Filename Value="codetoolstests\testcfgscript.pas"/> <Filename Value="codetoolstests\testcfgscript.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestCfgScript"/>
</Unit11> </Unit11>
<Unit12> <Unit12>
<Filename Value="codetoolstests\teststdcodetools.pas"/> <Filename Value="codetoolstests\teststdcodetools.pas"/>
@ -155,22 +141,18 @@
<Unit13> <Unit13>
<Filename Value="lazutils\testlazxml.pas"/> <Filename Value="lazutils\testlazxml.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestLazXML"/>
</Unit13> </Unit13>
<Unit14> <Unit14>
<Filename Value="lazutils\testavglvltree.pas"/> <Filename Value="lazutils\testavglvltree.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestAvgLvlTree"/>
</Unit14> </Unit14>
<Unit15> <Unit15>
<Filename Value="codetoolstests\testmethodjumptool.pas"/> <Filename Value="codetoolstests\testmethodjumptool.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestMethodJumpTool"/>
</Unit15> </Unit15>
<Unit16> <Unit16>
<Filename Value="lazutils\testlconvencoding.pas"/> <Filename Value="lazutils\testlconvencoding.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="TestLConvEncoding"/>
</Unit16> </Unit16>
</Units> </Units>
</ProjectOptions> </ProjectOptions>
@ -206,11 +188,7 @@
</Checks> </Checks>
</CodeGeneration> </CodeGeneration>
<Other> <Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CustomOptions Value="-dNoSemiAutomatedTests"/> <CustomOptions Value="-dNoSemiAutomatedTests"/>
<CompilerPath Value="$(CompPath)"/>
</Other> </Other>
</CompilerOptions> </CompilerOptions>
</CONFIG> </CONFIG>