[JEDI.NET-commits] nunit/source Jedi.System.CommandLine.NUnit.pas,NONE,1.1
Status: Pre-Alpha
Brought to you by:
jedi_mbe
From: Marcel B. <jed...@us...> - 2005-03-01 14:26:57
|
Update of /cvsroot/jedidotnet/nunit/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2369/nunit/source Added Files: Jedi.System.CommandLine.NUnit.pas Log Message: Added test-set for CommandLine parser --- NEW FILE: Jedi.System.CommandLine.NUnit.pas --- unit Jedi.System.CommandLine.NUnit; interface uses Jedi.System.CommandLine, NUnit.Framework, System.Drawing, System.Globalization, System.IO, System.Resources; type ObjectArray = array of &Object; type BaseTestFixture = class strict protected filePath: string; responsesFile1Path: string; strict protected function StoreTo: ObjectArray; virtual; abstract; strict protected function AssemblyOutputPath: string; virtual; abstract; function Optimization: Boolean; virtual; abstract; function SearchPath: string; virtual; abstract; function UnitOutputPath: string; virtual; abstract; public [TestFixtureSetUp] procedure CreateDir; [TestFixtureTearDown] procedure DeleteDir; public [Test] procedure OptionNotSpecified; virtual; [Test] procedure OptionSpecifiedOnce; virtual; [Test] procedure OptionSpecifiedMoreThanOnce; virtual; [Test] procedure OptionSpecifiedMoreThanOnceNoToggle; virtual; [Test] procedure ResponseFile; virtual; end; type InstanceSettings = class public constructor Create; strict private FAssemblyOutputPath: string; FOptimization: Boolean; FSearchPath: string; FUnitOutputPath: string; public [CommandLineArgument(CaseSensitive = False, Name = 'O', Prefix = '-', Prefix = '/')] property Optimization: Boolean read FOptimization write FOptimization; [CommandLineArgument(CaseSensitive = False, Name = 'S', Prefix = '-', Prefix = '/')] property SearchPath: string read FSearchPath write FSearchPath; [CommandLineArgument(CaseSensitive = False, Name = 'OA', Prefix = '-', Prefix = '/')] property AssemblyOutputPath: string read FAssemblyOutputPath write FAssemblyOutputPath; [CommandLineArgument(CaseSensitive = False, Name = 'OU', Prefix = '-', Prefix = '/')] property UnitOutputPath: string read FUnitOutputPath write FUnitOutputPath; end; type [TestFixture] ToInstance = class (BaseTestFixture) strict protected FInstance: InstanceSettings; strict protected function AssemblyOutputPath: string; override; function Optimization: Boolean; override; function SearchPath: string; override; function StoreTo: ObjectArray; override; function UnitOutputPath: string; override; public [SetUp] procedure CreateInstance; [TearDown] procedure DisposeInstance; end; type CompilerSettings = class strict private class constructor Create; strict private class var FOptimization: Boolean; protected class procedure Initialize; public [CommandLineArgument(CaseSensitive = False, Name = 'O', Prefix = '-', Prefix = '/')] class property Optimization: Boolean read FOptimization write FOptimization; end; DirectorySettings = class strict private class constructor Create; strict private class var FAssemblyOutputPath: string; class var FSearchPath: string; class var FUnitOutputPath: string; protected class procedure Initialize; public [CommandLineArgument(CaseSensitive = False, Name = 'OA', Prefix = '-', Prefix = '/')] class property AssemblyOutputPath: string read FAssemblyOutputPath write FAssemblyOutputPath; [CommandLineArgument(CaseSensitive = False, Name = 'S', Prefix = '-', Prefix = '/')] class property SearchPath: string read FSearchPath write FSearchPath; [CommandLineArgument(CaseSensitive = False, Name = 'OU', Prefix = '-', Prefix = '/')] class property UnitOutputPath: string read FUnitOutputPath write FUnitOutputPath; end; type [TestFixture] ToMultipleClasses = class (BaseTestFixture) strict protected function AssemblyOutputPath: string; override; function Optimization: Boolean; override; function SearchPath: string; override; function StoreTo: ObjectArray; override; function UnitOutputPath: string; override; public [SetUp] procedure Initialize; end; type DoubleOptionSettings = class strict private class var FDummy: Integer; public [CommandLineArgument(CaseSensitive = True, Name = 'o', Prefix = '-')] class property Dummy: Integer read FDummy write FDummy; end; type [TestFixture] Errors = class public [Test, ExpectedException(TypeOf(CommandLineException), 'Duplicate command line argument (-o) specified by ' + '''Jedi.System.CommandLine.DoubleOptionSettings.Dummy''. The argument has already been assigned to '+ '''Jedi.System.CommandLine.CompilerSettings.Optimization''')] procedure DoubleOptions; [Test, ExpectedException(TypeOf(CommandLineException), 'Unknown argument: -o')] procedure NoCompilerSettings; [Test, ExpectedException(TypeOf(CommandLineException), 'Unknown argument: -z+')] procedure UnknownArgument; end; type CaseSensitiveSettings = class strict private class var FOptimization: Boolean; class var FOrigin: Integer; public class procedure Initialize; static; public [CommandLineArgument(CaseSensitive = True, Name = 'O', Prefix = '-')] class property Optimization: Boolean read FOptimization write FOptimization; [CommandLineArgument(CaseSensitive = True, Name = 'o', Prefix = '-')] class property Origin: Integer read FOrigin write FOrigin; end; SettingsCustomProcessing = class strict private class var FSize: Size; strict protected [CommandLineArgument(CaseSensitive = False, Name = 'size', Prefix = '-', ValueSeparator = ':', ValueSeparator = '=', ValueSeparator = ' ')] class procedure ProcessSize(match, commandLine: string; var index: Integer); static; public class procedure Initialize; static; public class function get_Height: Integer; static; class function get_Width: Integer; static; class procedure set_Height(value: Integer); static; class procedure set_Width(value: Integer); static; public [CommandLineArgument(CaseSensitive = False, Name = 'height', Prefix = '--', ValueSeparator = ':', ValueSeparator = '=')] class property Height: Integer read get_Height write set_Height; class property Size: Size read FSize write FSize; [CommandLineArgument(CaseSensitive = False, Name = 'width', Prefix = '--', ValueSeparator = ':', ValueSeparator = '=')] class property Width: Integer read get_Width write set_Width; end; [TestFixture] SpecialCases = class public [Test] procedure CaseSensitive; [Test] procedure CustomProcessing; [Test] procedure MixedNormalAndCustom; end; implementation {$AUTOBOX ON} { BaseTestFixture } procedure BaseTestFixture.CreateDir; var rm: ResourceManager; tw: TextWriter; begin filePath := System.String.Join(Path.DirectorySeparatorChar, [Path.GetTempPath, 'Jedi.NET', 'CommandLine tests']); Directory.CreateDirectory(filePath); responsesFile1Path := System.String.Join(Path.DirectorySeparatorChar, [filePath, 'responses1.rsp']); rm := ResourceManager.Create('Jedi.System.CommandLine', TypeOf(BaseTestFixture).Assembly); tw := StreamWriter.Create(responsesFile1Path); tw.Write(rm.GetString('responses1.rsp')); tw.Close; end; procedure BaseTestFixture.DeleteDir; begin Directory.GetParent(filePath).Delete(True); end; procedure BaseTestFixture.OptionNotSpecified; var files: array of string; begin files := CommandLine.Parse( 'process "file to compile.dpk"', StoreTo); Assert.IsFalse(Optimization, 'Optimization option changed.'); Assert.AreEqual('', SearchPath, 'SearchPath has changed.'); Assert.AreEqual('', AssemblyOutputPath, 'AssemblyOutputPath has changed.'); Assert.AreEqual('', UnitOutputPath, 'UnitOutputPath has changed.'); Assert.AreEqual(1, &Array(files).Length, 'wrong number of files returned.'); Assert.AreEqual('file to compile.dpk', files[0], 'Wrong filename.'); end; procedure BaseTestFixture.OptionSpecifiedOnce; var files: array of string; begin files := CommandLine.Parse( 'process -o -sSomePath;NextPath -oa..\Bin -ou"..\Temp Units" file_to_compile.dpk', StoreTo); Assert.IsTrue(Optimization, 'Optimization option not changed.'); Assert.AreEqual('SomePath;NextPath', SearchPath, 'SearchPath has not changed.'); Assert.AreEqual('..\Bin', AssemblyOutputPath, 'AssemblyOutputPath has not changed.'); Assert.AreEqual('..\Temp Units', UnitOutputPath, 'UnitOutputPath has not changed.'); Assert.AreEqual(1, &Array(files).Length, 'wrong number of files returned.'); Assert.AreEqual('file_to_compile.dpk', files[0], 'Wrong filename.'); end; procedure BaseTestFixture.OptionSpecifiedMoreThanOnce; var files: array of string; begin files := CommandLine.Parse( 'process -o -sSomePath;NextPath -oa..\Bin -o -sOtherPath -ou"..\Temp Units" "file to compile.dpk"', StoreTo); Assert.IsFalse(Optimization, 'Optimization option not toggled.'); Assert.AreEqual('OtherPath', SearchPath, 'SearchPath has not changed.'); Assert.AreEqual('..\Bin', AssemblyOutputPath, 'AssemblyOutputPath has not changed.'); Assert.AreEqual('..\Temp Units', UnitOutputPath, 'UnitOutputPath has not changed.'); Assert.AreEqual(1, &Array(files).Length, 'wrong number of files returned.'); Assert.AreEqual('file to compile.dpk', files[0], 'Wrong filename.'); end; procedure BaseTestFixture.OptionSpecifiedMoreThanOnceNoToggle; var files: array of string; begin files := CommandLine.Parse( 'process -o -sSomePath;NextPath -oa..\Bin -o+ -sOtherPath -ou"..\Temp Units" "file to compile.dpk"', StoreTo); Assert.IsTrue(Optimization, 'Optimization option toggled.'); Assert.AreEqual('OtherPath', SearchPath, 'SearchPath has not changed.'); Assert.AreEqual('..\Bin', AssemblyOutputPath, 'AssemblyOutputPath has not changed.'); Assert.AreEqual('..\Temp Units', UnitOutputPath, 'UnitOutputPath has not changed.'); Assert.AreEqual(1, &Array(files).Length, 'wrong number of files returned.'); Assert.AreEqual('file to compile.dpk', files[0], 'Wrong filename.'); end; procedure BaseTestFixture.ResponseFile; var files: array of string; begin files := CommandLine.Parse( System.String.Format('process @"{0}" "file to compile.dpk"', responsesFile1Path), StoreTo, '@'); Assert.IsTrue(Optimization, 'Optimization option not changed.'); Assert.AreEqual('SomePath;NextPath', SearchPath, 'SearchPath has not changed.'); Assert.AreEqual('..\Bin', AssemblyOutputPath, 'AssemblyOutputPath has not changed.'); Assert.AreEqual('..\Temp Units', UnitOutputPath, 'UnitOutputPath has not changed.'); Assert.AreEqual(1, &Array(files).Length, 'wrong number of files returned.'); Assert.AreEqual('file to compile.dpk', files[0], 'Wrong filename.'); end; { CaseSensitiveSettings } class procedure CaseSensitiveSettings.Initialize; begin FOptimization := False; FOrigin := 0; end; { CompilerSettings } class constructor CompilerSettings.Create; begin Initialize; end; class procedure CompilerSettings.Initialize; begin FOptimization := False; end; { DirectorySettings } class constructor DirectorySettings.Create; begin Initialize; end; class procedure DirectorySettings.Initialize; begin FAssemblyOutputPath := ''; FSearchPath := ''; FUnitOutputPath := ''; end; { Errors } procedure Errors.DoubleOptions; begin CommandLine.Parse('process -o123 -ou..\Lib', [TypeOf(CompilerSettings), TypeOf(DirectorySettings), TypeOf(DoubleOptionSettings)]); end; procedure Errors.NoCompilerSettings; begin CommandLine.Parse( 'process -o -sSomePath;NextPath -oa..\Bin -ou"..\Temp Units" file_to_compile.dpk', [TypeOf(DirectorySettings)]); end; procedure Errors.UnknownArgument; begin CommandLine.Parse( 'process /o -z+ "some file.dpk"', [TypeOf(CompilerSettings), TypeOf(DirectorySettings)]); end; { InstanceSettings } constructor InstanceSettings.Create; begin inherited Create; FAssemblyOutputPath := ''; FSearchPath := ''; FUnitOutputPath := ''; end; { SettingsCustomProcessing } class procedure SettingsCustomProcessing.ProcessSize(match, commandLine: string; var index: Integer); var part: string; colonPos: Integer; newWidth: Integer; newHeight: Integer; begin part := Jedi.System.CommandLine.CommandLine.GetLiteral(commandLine, index); colonPos := part.IndexOf(','); if colonPos < 0 then raise CommandLineException.Create(System.String.Format('Invalid value for argument {0}: {1}', match.Trim, part)); if colonPos = 0 then newWidth := FSize.Width else newWidth := System.Int32.Parse(part.Substring(0, colonPos), NumberFormatInfo.InvariantInfo); if colonPos = part.Length - 1 then newHeight := FSize.Height else newHeight := System.Int32.Parse(part.Substring(colonPos + 1), NumberFormatInfo.InvariantInfo); FSize := System.Drawing.Size.Create(newWidth, newHeight); end; class function SettingsCustomProcessing.get_Height: Integer; begin Result := FSize.Height; end; class function SettingsCustomProcessing.get_Width: Integer; begin Result := FSize.Width; end; class procedure SettingsCustomProcessing.Initialize; begin FSize := System.Drawing.Size.Create(0, 0); end; class procedure SettingsCustomProcessing.set_Height(value: Integer); begin FSize := System.Drawing.Size.Create(FSize.Width, value); end; class procedure SettingsCustomProcessing.set_Width(value: Integer); begin FSize := System.Drawing.Size.Create(value, FSize.Height); end; { SpecialCases } procedure SpecialCases.CaseSensitive; begin CaseSensitiveSettings.Initialize; CommandLine.Parse('process -o123 -O+', [TypeOf(CaseSensitiveSettings)]); Assert.AreEqual(123, CaseSensitiveSettings.Origin, 'Origin not set.'); Assert.IsTrue(CaseSensitiveSettings.Optimization, 'Optimization flag not set.'); end; procedure SpecialCases.CustomProcessing; begin SettingsCustomProcessing.Initialize; CommandLine.Parse('process -size=64,32', [TypeOf(SettingsCustomProcessing)]); Assert.AreEqual(64, SettingsCustomProcessing.Width, 'Width not set correctly by -size option.'); Assert.AreEqual(32, SettingsCustomProcessing.Height, 'Height not set correctly by -size option.'); end; procedure SpecialCases.MixedNormalAndCustom; begin SettingsCustomProcessing.Initialize; CommandLine.Parse('process --width:128 -size=,32', [TypeOf(SettingsCustomProcessing)]); Assert.AreEqual(128, SettingsCustomProcessing.Width, 'Width not set correctly by --width option.'); Assert.AreEqual(32, SettingsCustomProcessing.Height, 'Height not set correctly by -size option.'); end; { ToInstance } function ToInstance.AssemblyOutputPath: string; begin Result := FInstance.AssemblyOutputPath; end; procedure ToInstance.CreateInstance; begin FInstance := InstanceSettings.Create; end; procedure ToInstance.DisposeInstance; begin FInstance.Free; FInstance := nil; end; function ToInstance.Optimization: Boolean; begin Result := FInstance.Optimization; end; function ToInstance.SearchPath: string; begin Result := FInstance.SearchPath; end; function ToInstance.StoreTo: ObjectArray; begin Result := ObjectArray.Create(FInstance); end; function ToInstance.UnitOutputPath: string; begin Result := FInstance.UnitOutputPath; end; { ToMultipleClasses } function ToMultipleClasses.AssemblyOutputPath: string; begin Result := DirectorySettings.AssemblyOutputPath; end; procedure ToMultipleClasses.Initialize; begin CompilerSettings.Initialize; DirectorySettings.Initialize; end; function ToMultipleClasses.Optimization: Boolean; begin Result := CompilerSettings.Optimization; end; function ToMultipleClasses.SearchPath: string; begin Result := DirectorySettings.SearchPath; end; function ToMultipleClasses.StoreTo: ObjectArray; begin Result := ObjectArray.Create(TypeOf(CompilerSettings), TypeOf(DirectorySettings)); end; function ToMultipleClasses.UnitOutputPath: string; begin Result := DirectorySettings.UnitOutputPath; end; end. |