[JEDI.NET-commits] main/Nunit/source NUnit.Jedi.Strings.pas,NONE,1.1 Nunit.Jedi.IO.PathHandling.pas,
Status: Pre-Alpha
Brought to you by:
jedi_mbe
From: Marcel B. <jed...@us...> - 2004-11-24 10:35:22
|
Update of /cvsroot/jedidotnet/main/Nunit/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5049/main/Nunit/source Added Files: NUnit.Jedi.Strings.pas Nunit.Jedi.IO.PathHandling.pas Nunit.Jedi.IO.pas Log Message: Added unit tests for the Jedi.IO.Path class and Jedi.Strings.StringUtils class --- NEW FILE: Nunit.Jedi.IO.pas --- unit Nunit.Jedi.IO; interface type PathTestCases = class strict private class constructor Create; strict protected const sNormVolumeRoot = 'volume{0}{1}'; const sNormVolumeRoot2 = 'secondvolume{0}'; const sNormVolumeRootNoSlash = 'volume{0}'; const sNormInvalidVolumeRoot = '{1}path{1}volume{0}{1}'; const sNormInvalidRelativePathUpLevel = '..SubFolder2.2'; const sNormDeepVolumeRootedPath = 'volume{0}{1}MyRootFolder{1}SubFolder1{1}SubFolder1.1{1}SubFolder1.1.1'; const sNormVolumePath = 'primary{0}{1}PrimaryRoot{1}My Folder'; const sNormDeepRootedPath = '{1}MyRootFolder{1}SubFolder1{1}SubFolder1.1{1}SubFolder1.1.1'; const sNormRelativePath = 'SubFolder2{1}SubFolder2.1'; const sNormRelativePathUpLevel = '..{1}SubFolder2.2'; const sNormRelativePathUpTwoLevels = '..{1}..{1}SubFolder3{1}SubFolder3.1{1}SubFolder3.1.1{1}SubFolder3.1.4'; const sNormRelativePathUpTwoLevelsSep = '..{1}..{1}SubFolder4{1}SubFolder4.1{1}SubFolder4.1.1{1}SubFolder4.1.4'; const sNormStartWithThisPath = 'My Folder'; const sNormHasDottedFolderName = 'SubFolder5{1}.HiddenStuff'; const sNormWildcardedFile = '*.txt'; const sNormCombinedVolRootAndRelativePath = 'volume{0}{1}SubFolder2{1}SubFolder2.1'; const sNormCombinedDeepVolRootAndRelPathUpTwo = 'volume{0}{1}MyRootFolder{1}SubFolder1{1}SubFolder3{1}SubFolder3.1{1}SubFolder3.1.1{1}SubFolder3.1.4'; const sNormCombinedVolRoot2AndDeepRooted = 'secondvolume{0}{1}MyRootFolder{1}SubFolder1{1}SubFolder1.1{1}SubFolder1.1.1'; const sNormCombinedVolPathAndDeepRooted = 'primary{0}{1}MyRootFolder{1}SubFolder1{1}SubFolder1.1{1}SubFolder1.1.1'; const sNormCombinedRelAndRelPathUp = 'SubFolder2{1}SubFolder2.2'; public const sVolumeRoot = 'volume:\'; const sVolumeRoot2 = 'secondvolume:'; const sVolumeRootNoSlash = 'volume:'; const sInvalidVolumeRoot = '\path\volume:\'; const sInvalidRelativePathUpLevel = '..SubFolder2.2'; const sDeepVolumeRootedPath = 'volume:\MyRootFolder/SubFolder1\SubFolder1.1/SubFolder1.1.1\'; const sVolumePath = 'primary:/PrimaryRoot\My Folder'; const sDeepRootedPath = '\MyRootFolder/SubFolder1/\SubFolder1.1//SubFolder1.1.1\'; const sRelativePath = 'SubFolder2\SubFolder2.1'; const sRelativePathUpLevel = '..\SubFolder2.2'; const sRelativePathUpTwoLevels = '...\SubFolder3\SubFolder3.1\SubFolder3.1.1\SubFolder3.1.4'; const sRelativePathUpTwoLevelsSep = '..\..\SubFolder4\SubFolder4.1\SubFolder4.1.1\SubFolder4.1.4'; const sStartWithThisPath = '.\My Folder'; const sHasDottedFolderName = 'SubFolder5\.HiddenStuff'; const sWildcardedFile = '*.txt'; class var sVolumeRootNormalized: string; sVolumeRoot2Normalized: string; sVolumeRootNoSlashNormalized: string; sInvalidVolumeRootNormalized: string; sInvalidRelativePathUpLevelNormalized: string; sDeepVolumeRootedPathNormalized: string; sDeepRootedPathNormalized: string; sVolumePathNormalized: string; sRelativePathNormalized: string; sRelativePathUpLevelNormalized: string; sRelativePathUpTwoLevelsNormalized: string; sRelativePathUpTwoLevelsSepNormalized: string; sStartWithThisPathNormalized: string; sHasDottedFolderNameNormalized: string; sWildcardedFileNormalized: string; sCombinedVolRootAndRelativePath: string; sCombinedDeepVolRootAndRelPathUpTwo: string; sCombinedVolRoot2AndDeepRooted: string; sCombinedVolPathAndDeepRooted: string; sCombinedRelAndRelPathUp: string; end; implementation uses System.IO; class constructor PathTestCases.Create; begin sVolumeRootNormalized := System.String.Format(sNormVolumeRoot, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sVolumeRoot2Normalized := System.String.Format(sNormVolumeRoot2, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sVolumeRootNoSlashNormalized := System.String.Format(sNormVolumeRootNoSlash, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sInvalidVolumeRootNormalized := System.String.Format(sNormInvalidVolumeRoot, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sInvalidRelativePathUpLevelNormalized := System.String.Format(sNormInvalidRelativePathUpLevel, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sDeepVolumeRootedPathNormalized := System.String.Format(sNormDeepVolumeRootedPath, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sDeepRootedPathNormalized := System.String.Format(sNormDeepRootedPath, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sVolumePathNormalized := System.String.Format(sNormVolumePath, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sRelativePathNormalized := System.String.Format(sNormRelativePath, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sRelativePathUpLevelNormalized := System.String.Format(sNormRelativePathUpLevel, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sRelativePathUpTwoLevelsNormalized := System.String.Format(sNormRelativePathUpTwoLevels, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sRelativePathUpTwoLevelsSepNormalized := System.String.Format(sNormRelativePathUpTwoLevelsSep, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sStartWithThisPathNormalized := System.String.Format(sNormStartWithThisPath, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sHasDottedFolderNameNormalized := System.String.Format(sNormHasDottedFolderName, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sWildcardedFileNormalized := System.String.Format(sNormWildcardedFile, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sCombinedVolRootAndRelativePath := System.String.Format(sNormCombinedVolRootAndRelativePath, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sCombinedDeepVolRootAndRelPathUpTwo := System.String.Format(sNormCombinedDeepVolRootAndRelPathUpTwo, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sCombinedVolRoot2AndDeepRooted := System.String.Format(sNormCombinedVolRoot2AndDeepRooted, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sCombinedVolPathAndDeepRooted := System.String.Format(sNormCombinedVolPathAndDeepRooted, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); sCombinedRelAndRelPathUp := System.String.Format(sNormCombinedRelAndRelPathUp, Path.VolumeSeparatorChar, Path.DirectorySeparatorChar); end; end. --- NEW FILE: Nunit.Jedi.IO.PathHandling.pas --- unit Nunit.Jedi.IO.PathHandling; interface uses System.IO, NUnit.Framework, Jedi.IO, Nunit.Jedi.IO; type [TestFixture] Normalize = class public [Test] procedure DeepVolumeRootedPath; [Test] procedure DeepRootedPath; [Test] procedure RelativePath; [Test] procedure RelativePathUpLevel; [Test] procedure RelativePathUpTwoLevels; [Test] procedure RelativePathUpTwoLevelsSep; [Test] procedure InvalidVolumeRoot; [Test] procedure InvalidRelativePathUpLevel; [Test] procedure VolumeRoot; [Test] procedure StartWithThisPath; [Test] procedure HasDottedFolderName; [Test] procedure WildcardedFile; end; [TestFixture] ValidityChecks = class public [Test] procedure DeepVolumeRootedPath; [Test] procedure DeepRootedPath; [Test] procedure RelativePath; [Test] procedure RelativePathUpLevel; [Test] procedure RelativePathUpTwoLevels; [Test] procedure RelativePathUpTwoLevelsSep; [Test] procedure InvalidVolumeRoot; [Test] procedure InvalidRelativePathUpLevel; [Test] procedure VolumeRoot; [Test] procedure StartWithThisPath; [Test] procedure HasDottedFolderName; [Test] procedure WildcardedFile; end; [TestFixture] Manipulations = class public [Test] procedure CombineTenPaths; [Test] procedure CombineTwo; [Test] procedure CombineTwoAbsolutePaths; end; [TestFixture] PathConfigurations = class public [Test] procedure TestConfiguration; end; implementation {$REGION 'Normalize'} procedure Normalize.DeepVolumeRootedPath; begin Assert.AreEqual(PathTestCases.sDeepVolumeRootedPathNormalized, Path.Normalize(PathTestCases.sDeepVolumeRootedPath)); end; procedure Normalize.DeepRootedPath; begin Assert.AreEqual(PathTestCases.sDeepRootedPathNormalized, Path.Normalize(PathTestCases.sDeepRootedPath)); end; procedure Normalize.RelativePath; begin Assert.AreEqual(PathTestCases.sRelativePathNormalized, Path.Normalize(PathTestCases.sRelativePath)); end; procedure Normalize.RelativePathUpLevel; begin Assert.AreEqual(PathTestCases.sRelativePathUpLevelNormalized, Path.Normalize(PathTestCases.sRelativePathUpLevel)); end; procedure Normalize.RelativePathUpTwoLevels; begin Assert.AreEqual(PathTestCases.sRelativePathUpTwoLevelsNormalized, Path.Normalize(PathTestCases.sRelativePathUpTwoLevels)); end; procedure Normalize.RelativePathUpTwoLevelsSep; begin Assert.AreEqual(PathTestCases.sRelativePathUpTwoLevelsSepNormalized, Path.Normalize(PathTestCases.sRelativePathUpTwoLevelsSep)); end; procedure Normalize.InvalidVolumeRoot; begin Assert.AreEqual(PathTestCases.sInvalidVolumeRootNormalized, Path.Normalize(PathTestCases.sInvalidVolumeRoot)); end; procedure Normalize.InvalidRelativePathUpLevel; begin Assert.AreEqual(PathTestCases.sInvalidRelativePathUpLevelNormalized, Path.Normalize(PathTestCases.sInvalidRelativePathUpLevel)); end; procedure Normalize.VolumeRoot; begin Assert.AreEqual(PathTestCases.sVolumeRootNormalized, Path.Normalize(PathTestCases.sVolumeRoot)); end; procedure Normalize.StartWithThisPath; begin Assert.AreEqual(PathTestCases.sStartWithThisPathNormalized, Path.Normalize(PathTestCases.sStartWithThisPath)); end; procedure Normalize.HasDottedFolderName; begin Assert.AreEqual(PathTestCases.sHasDottedFolderNameNormalized, Path.Normalize(PathTestCases.sHasDottedFolderName)); end; procedure Normalize.WildcardedFile; begin Assert.AreEqual(PathTestCases.sWildcardedFileNormalized, Path.Normalize(PathTestCases.sWildcardedFile)); end; {$ENDREGION} {$REGION 'ValidityChecks'} procedure ValidityChecks.DeepVolumeRootedPath; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sDeepVolumeRootedPath), 'IsValid({0}{{''{1}''}}) should be True', ['sDeepVolumeRootedPath', PathTestCases.sDeepVolumeRootedPath]); end; procedure ValidityChecks.DeepRootedPath; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sDeepRootedPath), 'IsValid({0}{{''{1}''}}) should be True', ['sDeepRootedPath', PathTestCases.sDeepRootedPath]); end; procedure ValidityChecks.RelativePath; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sRelativePath), 'IsValid({0}{{''{1}''}}) should be True', ['sRelativePath', PathTestCases.sRelativePath]); end; procedure ValidityChecks.RelativePathUpLevel; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sRelativePathUpLevel), 'IsValid({0}{{''{1}''}}) should be True', ['sRelativePathUpLevel', PathTestCases.sRelativePathUpLevel]); end; procedure ValidityChecks.RelativePathUpTwoLevels; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sRelativePathUpTwoLevels), 'IsValid({0}{{''{1}''}}) should be True', ['sRelativePathUpTwoLevels', PathTestCases.sRelativePathUpTwoLevels]); end; procedure ValidityChecks.RelativePathUpTwoLevelsSep; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sRelativePathUpTwoLevelsSep), 'IsValid({0}{{''{1}''}}) should be True', ['sRelativePathUpTwoLevelsSep', PathTestCases.sRelativePathUpTwoLevelsSep]); end; procedure ValidityChecks.InvalidVolumeRoot; begin Assert.IsFalse(Jedi.IO.Path.IsValid(PathTestCases.sInvalidVolumeRoot), 'IsValid({0}{{''{1}''}}) should be False', ['sInvalidVolumeRoot', PathTestCases.sInvalidVolumeRoot]); end; procedure ValidityChecks.InvalidRelativePathUpLevel; begin Assert.IsFalse(Jedi.IO.Path.IsValid(PathTestCases.sInvalidRelativePathUpLevel), 'IsValid({0}{{''{1}''}}) should be False', ['sInvalidRelativePathUpLevel', PathTestCases.sInvalidRelativePathUpLevel]); end; procedure ValidityChecks.VolumeRoot; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sVolumeRoot), 'IsValid({0}{{''{1}''}}) should be True', ['sVolumeRoot', PathTestCases.sVolumeRoot]); end; procedure ValidityChecks.StartWithThisPath; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sStartWithThisPath), 'IsValid({0}{{''{1}''}}) should be True', ['sStartWithThisPath', PathTestCases.sStartWithThisPath]); end; procedure ValidityChecks.HasDottedFolderName; begin Assert.IsTrue(Jedi.IO.Path.IsValid(PathTestCases.sHasDottedFolderName), 'IsValid({0}{{''{1}''}}) should be True', ['sHasDottedFolderName', PathTestCases.sHasDottedFolderName]); end; procedure ValidityChecks.WildcardedFile; begin Assert.IsFalse(Jedi.IO.Path.IsValid(PathTestCases.sWildcardedFile), 'IsValid({0}{{''{1}''}}) should be False', ['sWildcardedFile', PathTestCases.sWildcardedFile]); end; {$ENDREGION} {$REGION 'Manipulations'} procedure Manipulations.CombineTenPaths; begin Assert.AreEqual( System.String.Format('myvolume{0}{1}Root{1}Folder1{1}Folder1.1{1}Folder1.1.2{1}Folder1.1.2.1', System.IO.Path.VolumeSeparatorChar, System.IO.Path.DirectorySeparatorChar), Path.Combine(['dummy:NotAvailable\SubNotAvailable', '..\NotUsedEither', 'myvolume:\ignore', '\RootNotUsed\Ignore\Ignore2', '..\NotUsed', '\Root\Folder1\', '.\Folder1.1', 'Folder1.1.1\Folder1.1.1.1\Folder1.1.1.1.1\Folder1.1.1.1.1.1', '.....\Folder1.1.2', '.\Folder1.1.2.1'])); end; procedure Manipulations.CombineTwo; begin Assert.AreEqual( PathTestCases.sCombinedVolRootAndRelativePath, Jedi.IO.Path.Combine(PathTestCases.sVolumeRoot, PathTestCases.sRelativePath), 'Combine(sVolumeRoot, sRelativePath)'); Assert.AreEqual( PathTestCases.sCombinedDeepVolRootAndRelPathUpTwo, Jedi.IO.Path.Combine(PathTestCases.sDeepVolumeRootedPath, PathTestCases.sRelativePathUpTwoLevels), 'Combine(sDeepVolumeRootedPath, sRelativePathUpTwoLevels)'); Assert.AreEqual( PathTestCases.sCombinedRelAndRelPathUp, Jedi.IO.Path.Combine(PathTestCases.sRelativePath, PathTestCases.sRelativePathUpLevel), 'Combine(sRelativePath, sRelativePathUpLevel)'); end; procedure Manipulations.CombineTwoAbsolutePaths; begin Assert.AreEqual( PathTestCases.sCombinedVolRoot2AndDeepRooted, Jedi.IO.Path.Combine(PathTestCases.sVolumeRoot2, PathTestCases.sDeepRootedPath), 'Combine(sVolumeRoot2, sDeepRootedPath)'); Assert.AreEqual( PathTestCases.sCombinedVolPathAndDeepRooted, Jedi.IO.Path.Combine(PathTestCases.sVolumePath, PathTestCases.sDeepRootedPath), 'Combine(sVolumePath, sDeepRootedPath)'); Assert.AreEqual( PathTestCases.sDeepRootedPathNormalized, Jedi.IO.Path.Combine(PathTestCases.sRelativePath, PathTestCases.sDeepRootedPath), 'Combine(sRelativePath, sDeepRootedPath)'); end; {$ENDREGION} {$REGION 'PathConfigurations'} procedure PathConfigurations.TestConfiguration; begin Assert.AreEqual('myvolume#:folder1:folder2:folder3', Path.Normalize('myvolume#:folder1:folder2;folder3', Path.PathConfig.Create(':', ';', '#', ['$']))); end; {$ENDREGION} end. --- NEW FILE: NUnit.Jedi.Strings.pas --- unit NUnit.Jedi.Strings; interface uses Jedi.Strings, NUnit.Framework; type [TestFixture] TabSet = class strict protected TabSet1: StringUtils.TabSet; TabSet2: StringUtils.TabSet; TabSet3: StringUtils.TabSet; public [TestFixtureSetUp] procedure Init; [Test] procedure SimpleTabulation; [Test] procedure SpecifiedTabsWithAutoDefault; [Test] procedure SpecifiedTabsWithDefault; [Test] procedure CloneAndEqual; [Test] procedure RemovingTabs; [Test] procedure AddingTabs; [Test] procedure ConvertToString; end; [TestFixture] QuotedStrings = class strict protected const sSimpleTest = 'This is a simple test string.'; const sSimpleTestQuoted = '''This is a simple test string.'''; const sSimpleTestWithQuotes = 'This is a ''simple'' test string.'; const sSimpleTestWithQuotesQuoted = '''This is a ''''simple'''' test string.'''; const sSimpleTestWithOtherQuotes = 'This is a "simple" test string.'; const sSimpleTestWithOtherQuotesQuoted = '''This is a "simple" test string.'''; const sSimpleTestWithGarbageQuoted = '''This is a simple test string.'' With garbage.'; const sSimpleTestMissingEndQuote = '''This is a simple test string.'; const sComplexTest = 'Assuming single quotation ('') or double quotation (") this string '+ 'should always result in the roundtrip, regardless of actual used '+ 'quotation charachter. Scary combinations: ''''abc'''' and ""def""'; const sComplexTestQuoted = '''Assuming single quotation ('''') or double quotation (") this string '+ 'should always result in the roundtrip, regardless of actual used ' + 'quotation charachter. Scary combinations: ''''''''abc'''''''' and '+ '""def""'''; const sComplexTestAlternateQuoted = '"Assuming single quotation ('') or double quotation ("") this string '+ 'should always result in the roundtrip, regardless of actual used ' + 'quotation charachter. Scary combinations: ''''abc'''' and """"def"""""'; public [Test] procedure QuoteComplexString; [Test] procedure QuoteComplexStringWithAlternateQuoting; [Test] procedure QuoteSimpleString; [Test] procedure QuoteStringWithQuotes; [Test] procedure QuoteStringWithOtherQuotes; [Test] procedure RoundtripSimpleString; [Test] procedure RoundtripStringWithQuotes; [Test] procedure RoundtripStringWithOtherQuotes; [Test] procedure RoundtripComplexString; [Test] procedure RoundtripComplexStringWithAlternateQuoting; [ExpectedException(TypeOf(FormatException), 'String ends in garbage.')] [Test] procedure DequoteStringWithGarbage; [ExpectedException(TypeOf(FormatException), 'End quote missing.')] [Test] procedure DequoteStringWithoutEndQuote; [Test] procedure DequoteStringWithAllowedGarbage; [Test] procedure DequoteStringAllowedMissingEndQuote; [Test] procedure DequoteStringCheckGarbage; [Test] procedure DequoteStringCheckEndQuote; [Test] procedure DequoteNonQuotedString; end; [TestFixture] Substrings = class strict protected const sDelimTest = '|Abc(def){ghi}.jkl;mno=(pqr){stu}.vwx;yz|'; const sDelimTestBeforeParenthesis = '|Abc'; const sDelimTestBeforeBrace = '|Abc(def)'; const sDelimTestBeforeDot = '|Abc(def){ghi}.'; const sDelimTestBeforeSemiColon = '|Abc(def){ghi}.jkl'; const sDelimTestBeforeEquals = '|Abc(def){ghi}.jkl;mno'; const sDelimTestBeforeLastEquals = '|Abc(def){ghi}.jkl;mno'; const sDelimTestBeforeLastParenthesis = '|Abc(def){ghi}.jkl;mno='; const sDelimTestBeforeLastBrace = '|Abc(def){ghi}.jkl;mno=(pqr)'; const sDelimTestBeforeLastDot = '|Abc(def){ghi}.jkl;mno=(pqr){stu}'; const sDelimTestBeforeLastSemiColon = '|Abc(def){ghi}.jkl;mno=(pqr){stu}.vwx'; const sDelimTestBeforeLastPipe = '|Abc(def){ghi}.jkl;mno=(pqr){stu}.vwx;yz'; const sDelimTestAfterFirstPipe = 'Abc(def){ghi}.jkl;mno=(pqr){stu}.vwx;yz|'; const sDelimTestAfterFirstParenthesis = 'def){ghi}.jkl;mno=(pqr){stu}.vwx;yz|'; const sDelimTestAfterFirstBrace = 'ghi}.jkl;mno=(pqr){stu}.vwx;yz|'; const sDelimTestAfterFirstDot = 'jkl;mno=(pqr){stu}.vwx;yz|'; const sDelimTestAfterFirstSemiColon = 'mno=(pqr){stu}.vwx;yz|'; const sDelimTestAfterFirstEquals = '(pqr){stu}.vwx;yz|'; const sDelimTestAfterEquals = '(pqr){stu}.vwx;yz|'; const sDelimTestAfterParenthesis = 'pqr){stu}.vwx;yz|'; const sDelimTestAfterBrace = 'stu}.vwx;yz|'; const sDelimTestAfterDot = 'vwx;yz|'; const sDelimTestAfterSemiColon = 'yz|'; const sSliceTest = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; public [Test] procedure AfterSingleDelimiter; [Test] procedure AfterMultipleDelimiters; [Test] procedure AfterLastSingleDelimiter; [Test] procedure AfterLastMultipleDelimiters; [Test] procedure BeforeSingleDelimiter; [Test] procedure BeforeMultipleDelimiters; [Test] procedure BeforeLastSingleDelimiter; [Test] procedure BeforeLastMultipleDelimiters; [Test] procedure Left; [Test] procedure Mid; [Test] procedure Right; end; [TestFixture] Manipulations = class strict protected const sTabTest1 = 'Abc'#9'Def Ghi'#9'Jkl'#9#9'Mno'#9#9#9'Pqr'; const sTabTest2 = #9'Abc'#9'Def Ghi'#9'Jkl'#9#9'Mno'#9#9#9'Pqr'; // 1 2 3 4 5 6 7 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 const sTabTest1Output1 = 'Abc Def Ghi Jkl Mno Pqr'; const sTabTest1Output2 = 'Abc Def Ghi Jkl Mno Pqr'; const sTabTest1Output3 = 'Abc Def Ghi Jkl Mno Pqr'; const sTabTest2Output1 = ' Abc Def Ghi Jkl Mno Pqr'; const sTabTest2Output2 = ' Abc Def Ghi Jkl Mno Pqr'; const sTabTest2Output3 = ' Abc Def Ghi Jkl Mno Pqr'; strict protected const sTest1 = '***abc***def***ghi***'; const sTest1Output = '*abc*def*ghi*'; strict protected const sRepeatInput = 'ab(cd)ef'; const sRepeatOutput0 = ''; const sRepeatOutput1 = 'ab(cd)ef'; const sRepeatOutput10 = 'ab(cd)efab(cd)efab(cd)efab(cd)efab(cd)efab(cd)efab(cd)efab(cd)efab(cd)efab(cd)ef'; public [Test] procedure ExpandTabs; [Test] procedure RemoveDuplicates; [Test] procedure &Repeat; end; implementation {$REGION 'TabSet'} procedure TabSet.ConvertToString; begin Assert.AreEqual('[]+2', TabSet1.ToString, 'TabSet1.ToString'); Assert.AreEqual('[15,20,40,44]+4 (automatic)', TabSet2.ToString, 'TabSet2.ToString'); Assert.AreEqual('[15,20,40,44]+2', TabSet3.ToString, 'TabSet3.ToString'); Assert.AreEqual('[]+2 (automatic)', StringUtils.TabSet.Create.ToString, 'StringUtils.TabSet.Create.ToString'); end; procedure TabSet.Init; begin TabSet1 := StringUtils.TabSet.Create(2); TabSet2 := StringUtils.TabSet.Create([15, 20, 40, 44]); TabSet3 := StringUtils.TabSet.Create([15, 20, 40, 44], 2); end; procedure TabSet.SimpleTabulation; begin // TabSet1 should always tabulate to even positions Assert.AreEqual(TabSet1.TabFrom(1), 2); Assert.AreEqual(TabSet1.TabFrom(2), 4); Assert.AreEqual(TabSet1.TabFrom(3), 4); Assert.AreEqual(TabSet1.TabFrom(51), 52); Assert.AreEqual(TabSet1.TabFrom(52), 54); end; procedure TabSet.SpecifiedTabsWithAutoDefault; var i: Integer; begin // TabSet2 will tabulate to 15, 20, 40 and 44. Beyond that it will tabulate to a multiple of 4 (44-40) for i := 0 to 14 do Assert.AreEqual(TabSet2.TabFrom(i), 15, 'Tabulate from {0}', [i]); for i := 15 to 19 do Assert.AreEqual(TabSet2.TabFrom(i), 20, 'Tabulate from {0}', [i]); for i := 20 to 39 do Assert.AreEqual(TabSet2.TabFrom(i), 40, 'Tabulate from {0}', [i]); for i := 40 to 43 do Assert.AreEqual(TabSet2.TabFrom(i), 44, 'Tabulate from {0}', [i]); for i := 44 to 47 do Assert.AreEqual(TabSet2.TabFrom(i), 48, 'Tabulate from {0}', [i]); for i := 48 to 51 do Assert.AreEqual(TabSet2.TabFrom(i), 52, 'Tabulate from {0}', [i]); end; procedure TabSet.SpecifiedTabsWithDefault; var i: Integer; begin // TabSet2 will tabulate to 15, 20, 40 and 44. Beyond that it will tabulate to a multiple of 2 for i := 0 to 14 do Assert.AreEqual(TabSet3.TabFrom(i), 15, 'Tabulate from {0}', [i]); for i := 15 to 19 do Assert.AreEqual(TabSet3.TabFrom(i), 20, 'Tabulate from {0}', [i]); for i := 20 to 39 do Assert.AreEqual(TabSet3.TabFrom(i), 40, 'Tabulate from {0}', [i]); for i := 40 to 43 do Assert.AreEqual(TabSet3.TabFrom(i), 44, 'Tabulate from {0}', [i]); for i := 44 to 45 do Assert.AreEqual(TabSet3.TabFrom(i), 46, 'Tabulate from {0}', [i]); for i := 46 to 47 do Assert.AreEqual(TabSet3.TabFrom(i), 48, 'Tabulate from {0}', [i]); for i := 48 to 49 do Assert.AreEqual(TabSet3.TabFrom(i), 50, 'Tabulate from {0}', [i]); for i := 50 to 51 do Assert.AreEqual(TabSet3.TabFrom(i), 52, 'Tabulate from {0}', [i]); end; procedure TabSet.CloneAndEqual; var tempSet: StringUtils.TabSet; begin tempSet := StringUtils.TabSet(TabSet1.Clone); Assert.AreNotSame(TabSet1, tempSet); Assert.AreEqual(TabSet1, tempSet, 'Cloned'); tempSet.Add(2); Assert.AreEqual(TabSet1, tempSet, 'TabPos=[2], DefaultWidth=2'); tempSet.DefaultWidth := 0; Assert.AreEqual(TabSet1, tempSet, 'TabPos=[2], DefaultWidth=0 (automatic)'); tempSet.Add(4); Assert.AreEqual(TabSet1, tempSet, 'TabPos=[2,4], DefaultWidth=0 (automatic)'); tempSet.Clear; Assert.AreEqual(TabSet1, tempSet, 'TabPos=[], DefaultWidth=0 (automatic)'); end; procedure TabSet.RemovingTabs; var tempSet: StringUtils.TabSet; i: Integer; begin tempSet := StringUtils.TabSet(TabSet2.Clone); tempSet.Remove(20); for i := 0 to 14 do Assert.AreEqual(tempSet.TabFrom(i), 15, 'Removed 20; Tabulate from {0}', [i]); for i := 15 to 39 do Assert.AreEqual(tempSet.TabFrom(i), 40, 'Removed 20; Tabulate from {0}', [i]); for i := 40 to 43 do Assert.AreEqual(tempSet.TabFrom(i), 44, 'Removed 20; Tabulate from {0}', [i]); for i := 44 to 47 do Assert.AreEqual(tempSet.TabFrom(i), 48, 'Removed 20; Tabulate from {0}', [i]); for i := 48 to 51 do Assert.AreEqual(tempSet.TabFrom(i), 52, 'Removed 20; Tabulate from {0}', [i]); for i := 52 to 55 do Assert.AreEqual(tempSet.TabFrom(i), 56, 'Removed 20; Tabulate from {0}', [i]); for i := 56 to 59 do Assert.AreEqual(tempSet.TabFrom(i), 60, 'Removed 20; Tabulate from {0}', [i]); tempSet := StringUtils.TabSet(TabSet2.Clone); tempSet.Remove(40); for i := 0 to 14 do Assert.AreEqual(tempSet.TabFrom(i), 15, 'Removed 40; Tabulate from {0}', [i]); for i := 15 to 19 do Assert.AreEqual(tempSet.TabFrom(i), 20, 'Removed 40; Tabulate from {0}', [i]); for i := 20 to 43 do Assert.AreEqual(tempSet.TabFrom(i), 44, 'Removed 40; Tabulate from {0}', [i]); for i := 44 to 67 do Assert.AreEqual(tempSet.TabFrom(i), 68, 'Removed 40; Tabulate from {0}', [i]); for i := 68 to 91 do Assert.AreEqual(tempSet.TabFrom(i), 92, 'Removed 40; Tabulate from {0}', [i]); for i := 92 to 115 do Assert.AreEqual(tempSet.TabFrom(i), 116, 'Removed 40; Tabulate from {0}', [i]); for i := 116 to 139 do Assert.AreEqual(tempSet.TabFrom(i), 140, 'Removed 40; Tabulate from {0}', [i]); tempSet := StringUtils.TabSet(TabSet2.Clone); tempSet.Remove(40, True); for i := 0 to 14 do Assert.AreEqual(tempSet.TabFrom(i), 15, 'Removed 40+; Tabulate from {0}', [i]); for i := 15 to 19 do Assert.AreEqual(tempSet.TabFrom(i), 20, 'Removed 40+; Tabulate from {0}', [i]); for i := 20 to 24 do Assert.AreEqual(tempSet.TabFrom(i), 25, 'Removed 40+; Tabulate from {0}', [i]); for i := 25 to 29 do Assert.AreEqual(tempSet.TabFrom(i), 30, 'Removed 40+; Tabulate from {0}', [i]); for i := 30 to 34 do Assert.AreEqual(tempSet.TabFrom(i), 35, 'Removed 40+; Tabulate from {0}', [i]); for i := 35 to 39 do Assert.AreEqual(tempSet.TabFrom(i), 40, 'Removed 40+; Tabulate from {0}', [i]); for i := 40 to 44 do Assert.AreEqual(tempSet.TabFrom(i), 45, 'Removed 40+; Tabulate from {0}', [i]); end; procedure TabSet.AddingTabs; var tempSet: StringUtils.TabSet; tempSet2: StringUtils.TabSet; i: Integer; begin tempSet := StringUtils.TabSet(TabSet2.Clone); tempSet.Add(42); for i := 0 to 14 do Assert.AreEqual(tempSet.TabFrom(i), 15, 'Added 42; Tabulate from {0}', [i]); for i := 15 to 19 do Assert.AreEqual(tempSet.TabFrom(i), 20, 'Added 42; Tabulate from {0}', [i]); for i := 20 to 39 do Assert.AreEqual(tempSet.TabFrom(i), 40, 'Added 42; Tabulate from {0}', [i]); for i := 40 to 41 do Assert.AreEqual(tempSet.TabFrom(i), 42, 'Added 42; Tabulate from {0}', [i]); for i := 42 to 43 do Assert.AreEqual(tempSet.TabFrom(i), 44, 'Added 42; Tabulate from {0}', [i]); for i := 44 to 45 do Assert.AreEqual(tempSet.TabFrom(i), 46, 'Added 42; Tabulate from {0}', [i]); for i := 46 to 47 do Assert.AreEqual(tempSet.TabFrom(i), 48, 'Added 42; Tabulate from {0}', [i]); for i := 48 to 49 do Assert.AreEqual(tempSet.TabFrom(i), 50, 'Added 42; Tabulate from {0}', [i]); for i := 50 to 51 do Assert.AreEqual(tempSet.TabFrom(i), 52, 'Added 42; Tabulate from {0}', [i]); tempSet2 := StringUtils.TabSet.Create([40, 20, 15], 2); Assert.AreEqual(tempSet2, tempSet); end; {$ENDREGION} {$REGION 'QuotedStrings'} procedure QuotedStrings.QuoteComplexString; var s: string; begin s := StringUtils.QuoteString(sComplexTest, ''''); Assert.AreEqual(s, sComplexTestQuoted); end; procedure QuotedStrings.QuoteComplexStringWithAlternateQuoting; var s: string; begin s := StringUtils.QuoteString(sComplexTest, '"'); Assert.AreEqual(s, sComplexTestAlternateQuoted); end; procedure QuotedStrings.QuoteSimpleString; var s: string; begin s := StringUtils.QuoteString(sSimpleTest); Assert.AreEqual(sSimpleTestQuoted, s); end; procedure QuotedStrings.QuoteStringWithQuotes; var s: string; begin s := StringUtils.QuoteString(sSimpleTestWithQuotes); Assert.AreEqual(sSimpleTestWithQuotesQuoted, s); end; procedure QuotedStrings.QuoteStringWithOtherQuotes; var s: string; begin s := StringUtils.QuoteString(sSimpleTestWithOtherQuotes); Assert.AreEqual(sSimpleTestWithOtherQuotesQuoted, s); end; procedure QuotedStrings.RoundtripSimpleString; var s: string; begin s := StringUtils.ExtractQuotedString(StringUtils.QuoteString(sSimpleTest)); Assert.AreEqual(sSimpleTest, s); end; procedure QuotedStrings.RoundtripStringWithOtherQuotes; var s: string; begin s := StringUtils.ExtractQuotedString(StringUtils.QuoteString(sSimpleTestWithOtherQuotes)); Assert.AreEqual(sSimpleTestWithOtherQuotes, s); end; procedure QuotedStrings.RoundtripStringWithQuotes; var s: string; begin s := StringUtils.ExtractQuotedString(StringUtils.QuoteString(sSimpleTestWithQuotes)); Assert.AreEqual(sSimpleTestWithQuotes, s); end; procedure QuotedStrings.RoundtripComplexString; var s: string; begin s := StringUtils.ExtractQuotedString(StringUtils.QuoteString(sComplexTest, '''')); Assert.AreEqual(s, sComplexTest); end; procedure QuotedStrings.RoundtripComplexStringWithAlternateQuoting; var s: string; begin s := StringUtils.ExtractQuotedString(StringUtils.QuoteString(sComplexTest, '"')); Assert.AreEqual(s, sComplexTest); end; procedure QuotedStrings.DequoteStringWithGarbage; var s: string; begin s := StringUtils.ExtractQuotedString(sSimpleTestWithGarbageQuoted); Assert.Fail('Should result in an exception. Resulting string={0}', [s]); end; procedure QuotedStrings.DequoteStringWithoutEndQuote; var s: string; begin s := StringUtils.ExtractQuotedString(sSimpleTestMissingEndQuote); Assert.Fail('Should result in an exception. Resulting string={0}', [s]); end; procedure QuotedStrings.DequoteStringWithAllowedGarbage; var s: string; begin s := StringUtils.ExtractQuotedString(sSimpleTestWithGarbageQuoted, ExtractQuotedStringFlags.IgnoreGarbage or ExtractQuotedStringFlags.EndQuoteMandetory); Assert.AreEqual(sSimpleTest, s, 'actual garbage'); s := StringUtils.ExtractQuotedString(sSimpleTestQuoted, ExtractQuotedStringFlags.IgnoreGarbage or ExtractQuotedStringFlags.EndQuoteMandetory); Assert.AreEqual(sSimpleTest, s, 'no actual garbage'); end; procedure QuotedStrings.DequoteStringAllowedMissingEndQuote; var s: string; begin s := StringUtils.ExtractQuotedString(sSimpleTestMissingEndQuote, ExtractQuotedStringFlags.IgnoreGarbage); Assert.AreEqual(sSimpleTest, s, 'end quote actually missing'); s := StringUtils.ExtractQuotedString(sSimpleTestQuoted, ExtractQuotedStringFlags.IgnoreGarbage); Assert.AreEqual(sSimpleTest, s, 'end quote provided'); end; procedure QuotedStrings.DequoteStringCheckGarbage; var s: string; i: Integer; begin s := StringUtils.ExtractQuotedString(sSimpleTestWithGarbageQuoted, i); Assert.AreEqual(sSimpleTest, s); Assert.AreEqual(sSimpleTestWithGarbageQuoted.LastIndexOf(''''), i); end; procedure QuotedStrings.DequoteStringCheckEndQuote; var s: string; i: Integer; begin s := StringUtils.ExtractQuotedString(sSimpleTestMissingEndQuote, i); Assert.AreEqual(sSimpleTest, s); Assert.AreEqual(not sSimpleTestMissingEndQuote.Length, i); end; procedure QuotedStrings.DequoteNonQuotedString; var s: string; begin s := StringUtils.ExtractQuotedString(sSimpleTest); Assert.AreEqual(sSimpleTest, s); s := StringUtils.ExtractQuotedString(sSimpleTestWithQuotes); Assert.AreEqual(sSimpleTestWithQuotes, s); end; {$ENDREGION} {$REGION 'Substrings'} procedure Substrings.AfterSingleDelimiter; var s: string; begin s := StringUtils.After(sDelimTest, '='); Assert.AreEqual(sDelimTestAfterFirstEquals, s); s := StringUtils.After(sDelimTest, '('); Assert.AreEqual(sDelimTestAfterFirstParenthesis, s); s := StringUtils.After(sDelimTest, '{'); Assert.AreEqual(sDelimTestAfterFirstBrace, s); s := StringUtils.After(sDelimTest, ' '); Assert.AreEqual('', s); s := StringUtils.After(sDelimTest, '|'); Assert.AreEqual(sDelimTestAfterFirstPipe, s); end; procedure Substrings.AfterMultipleDelimiters; var s: string; begin s := StringUtils.AfterAnyOf(sDelimTest, '={'.ToCharArray); Assert.AreEqual(sDelimTestAfterFirstBrace, s); s := StringUtils.AfterAnyOf(sDelimTest, ' =;'.ToCharArray); Assert.AreEqual(sDelimTestAfterFirstSemiColon, s); s := StringUtils.AfterAnyOf(sDelimTest, ';.{(='.ToCharArray); Assert.AreEqual(sDelimTestAfterFirstParenthesis, s); s := StringUtils.AfterAnyOf(sDelimTest, ' %$#&*_+-'.ToCharArray); Assert.AreEqual('', s); end; procedure Substrings.AfterLastSingleDelimiter; var s: string; begin s := StringUtils.AfterLast(sDelimTest, '='); Assert.AreEqual(sDelimTestAfterEquals, s); s := StringUtils.AfterLast(sDelimTest, '('); Assert.AreEqual(sDelimTestAfterParenthesis, s); s := StringUtils.AfterLast(sDelimTest, '{'); Assert.AreEqual(sDelimTestAfterBrace, s); s := StringUtils.AfterLast(sDelimTest, ' '); Assert.AreEqual('', s); s := StringUtils.AfterLast(sDelimTest, '|'); Assert.AreEqual('', s); end; procedure Substrings.AfterLastMultipleDelimiters; var s: string; begin s := StringUtils.AfterLastAnyOf(sDelimTest, '={'.ToCharArray); Assert.AreEqual(sDelimTestAfterBrace, s); s := StringUtils.AfterLastAnyOf(sDelimTest, ' =.'.ToCharArray); Assert.AreEqual(sDelimTestAfterDot, s); s := StringUtils.AfterLastAnyOf(sDelimTest, ';.{(='.ToCharArray); Assert.AreEqual(sDelimTestAfterSemiColon, s); s := StringUtils.AfterLastAnyOf(sDelimTest, ' %$#&*_+-'.ToCharArray); Assert.AreEqual('', s); end; procedure Substrings.BeforeSingleDelimiter; var s: string; begin s := StringUtils.Before(sDelimTest, '='); Assert.AreEqual(sDelimTestBeforeEquals, s); s := StringUtils.Before(sDelimTest, '('); Assert.AreEqual(sDelimTestBeforeParenthesis, s); s := StringUtils.Before(sDelimTest, '{'); Assert.AreEqual(sDelimTestBeforeBrace, s); s := StringUtils.Before(sDelimTest, ' '); Assert.AreEqual(sDelimTest, s); s := StringUtils.Before(sDelimTest, '|'); Assert.AreEqual('', s); end; procedure Substrings.BeforeMultipleDelimiters; var s: string; begin s := StringUtils.BeforeAnyOf(sDelimTest, '={'.ToCharArray); Assert.AreEqual(sDelimTestBeforeBrace, s); s := StringUtils.BeforeAnyOf(sDelimTest, ' =;'.ToCharArray); Assert.AreEqual(sDelimTestBeforeSemiColon, s); s := StringUtils.BeforeAnyOf(sDelimTest, ';.{(='.ToCharArray); Assert.AreEqual(sDelimTestBeforeParenthesis, s); s := StringUtils.BeforeAnyOf(sDelimTest, ' %$#&*_+-'.ToCharArray); Assert.AreEqual(sDelimTest, s); end; procedure Substrings.BeforeLastSingleDelimiter; var s: string; begin s := StringUtils.BeforeLast(sDelimTest, '='); Assert.AreEqual(sDelimTestBeforeLastEquals, s); s := StringUtils.BeforeLast(sDelimTest, '('); Assert.AreEqual(sDelimTestBeforeLastParenthesis, s); s := StringUtils.BeforeLast(sDelimTest, '{'); Assert.AreEqual(sDelimTestBeforeLastBrace, s); s := StringUtils.BeforeLast(sDelimTest, ' '); Assert.AreEqual(sDelimTest, s); s := StringUtils.BeforeLast(sDelimTest, '|'); Assert.AreEqual(sDelimTestBeforeLastPipe, s); end; procedure Substrings.BeforeLastMultipleDelimiters; var s: string; begin s := StringUtils.BeforeLastAnyOf(sDelimTest, '={'.ToCharArray); Assert.AreEqual(sDelimTestBeforeLastBrace, s); s := StringUtils.BeforeLastAnyOf(sDelimTest, ' =;'.ToCharArray); Assert.AreEqual(sDelimTestBeforeLastSemiColon, s); s := StringUtils.BeforeLastAnyOf(sDelimTest, ';.{(='.ToCharArray); Assert.AreEqual(sDelimTestBeforeLastSemiColon, s); s := StringUtils.BeforeLastAnyOf(sDelimTest, ' %$#&*_+-'.ToCharArray); Assert.AreEqual(sDelimTest, s); end; procedure Substrings.Left; var s: string; begin s := StringUtils.Left(sSliceTest, 10); Assert.AreEqual('0123456789', s); end; procedure Substrings.Mid; var s: string; begin s := StringUtils.Mid(sSliceTest, 10, 14); Assert.AreEqual('ABCDE', s); end; procedure Substrings.Right; var s: string; begin s := StringUtils.Right(sSliceTest, 10); Assert.AreEqual('qrstuvwxyz', s); end; {$ENDREGION} {$REGION 'Manipulations'} procedure Manipulations.ExpandTabs; var tabSet1: StringUtils.TabSet; tabSet2: StringUtils.TabSet; tabSet3: StringUtils.TabSet; begin tabSet1 := StringUtils.TabSet.Create(2); tabSet2 := StringUtils.TabSet.Create([15, 20, 40, 44]); tabSet3 := StringUtils.TabSet.Create([15, 20, 40, 44], 2); Assert.AreEqual(sTabTest1Output1, StringUtils.ExpandTabs(sTabTest1, tabSet1), 'sTabTest1Output1'); Assert.AreEqual(sTabTest2Output1, StringUtils.ExpandTabs(sTabTest2, tabSet1), 'sTabTest2Output1'); Assert.AreEqual(sTabTest1Output2, StringUtils.ExpandTabs(sTabTest1, tabSet2), 'sTabTest1Output2'); Assert.AreEqual(sTabTest2Output2, StringUtils.ExpandTabs(sTabTest2, tabSet2), 'sTabTest2Output2'); Assert.AreEqual(sTabTest1Output3, StringUtils.ExpandTabs(sTabTest1, tabSet3), 'sTabTest1Output3'); Assert.AreEqual(sTabTest2Output3, StringUtils.ExpandTabs(sTabTest2, tabSet3), 'sTabTest2Output3'); end; procedure Manipulations.RemoveDuplicates; begin Assert.AreEqual(sTest1Output, StringUtils.RemoveDuplicateChars(sTest1, '*'), 'sTest1'); end; procedure Manipulations.&Repeat; begin Assert.AreEqual(sRepeatOutput0, StringUtils.&Repeat(sRepeatInput, 0), 'Count=0'); Assert.AreEqual(sRepeatOutput1, StringUtils.&Repeat(sRepeatInput, 1), 'Count=1'); Assert.AreEqual(sRepeatOutput10, StringUtils.&Repeat(sRepeatInput, 10), 'Count=10'); end; {$ENDREGION} end. |