2007-06-13 07:47:25 +02:00
|
|
|
unit PersistTest;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
Classes,
|
|
|
|
|
|
|
|
TestFramework;
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TPersistTest = class(TTestCase)
|
|
|
|
published
|
2007-08-10 14:48:00 +02:00
|
|
|
procedure QueryReaderWriter;
|
|
|
|
|
2007-06-13 07:47:25 +02:00
|
|
|
procedure WriteNoTypeInfo;
|
|
|
|
procedure WriteSimpleTypes;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
SysUtils,
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
X2UtPersist,
|
|
|
|
X2UtPersistIntf;
|
2007-06-13 07:47:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
type
|
2007-08-10 14:48:00 +02:00
|
|
|
IPersistTestOutput = interface
|
|
|
|
['{F0BFDA30-B2BF-449D-9A6E-0EDEBCDAE701}']
|
|
|
|
function GetLines(): TStrings;
|
|
|
|
|
|
|
|
property Lines: TStrings read GetLines;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TPersistTestOutput = class(TInterfacedObject, IPersistTestOutput)
|
|
|
|
private
|
|
|
|
FLines: TStrings;
|
|
|
|
protected
|
|
|
|
function GetLines(): TStrings;
|
|
|
|
public
|
|
|
|
constructor Create();
|
|
|
|
destructor Destroy(); override;
|
|
|
|
end;
|
|
|
|
|
2007-06-13 07:47:25 +02:00
|
|
|
TX2UtPersistTest = class(TX2CustomPersist)
|
|
|
|
private
|
2007-08-10 14:48:00 +02:00
|
|
|
FOutput: IPersistTestOutput;
|
2007-06-13 07:47:25 +02:00
|
|
|
protected
|
2007-08-10 14:48:00 +02:00
|
|
|
function CreateFiler(AIsReader: Boolean): IX2PersistFiler; override;
|
|
|
|
public
|
|
|
|
constructor Create();
|
|
|
|
|
|
|
|
property Output: IPersistTestOutput read FOutput;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
TX2UtPersistTestFiler = class(TX2CustomPersistFiler)
|
|
|
|
private
|
|
|
|
FOutput: IPersistTestOutput;
|
|
|
|
public
|
2007-06-13 07:47:25 +02:00
|
|
|
function BeginSection(const AName: String): Boolean; override;
|
|
|
|
procedure EndSection(); override;
|
|
|
|
|
|
|
|
function ReadFloat(const AName: String; out AValue: Extended): Boolean; override;
|
|
|
|
function ReadInt64(const AName: String; out AValue: Int64): Boolean; override;
|
|
|
|
function ReadInteger(const AName: String; out AValue: Integer): Boolean; override;
|
|
|
|
function ReadString(const AName: String; out AValue: String): Boolean; override;
|
|
|
|
|
|
|
|
function WriteFloat(const AName: String; AValue: Extended): Boolean; override;
|
|
|
|
function WriteInt64(const AName: String; AValue: Int64): Boolean; override;
|
|
|
|
function WriteInteger(const AName: String; AValue: Integer): Boolean; override;
|
|
|
|
function WriteString(const AName: String; const AValue: String): Boolean; override;
|
|
|
|
|
2016-04-15 15:37:58 +02:00
|
|
|
procedure GetKeys(const ADest: TStrings); override;
|
|
|
|
procedure GetSections(const ADest: TStrings); override;
|
|
|
|
procedure DeleteKey(const AName: string); override;
|
|
|
|
procedure DeleteSection(const AName: string); override;
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
property Output: IPersistTestOutput read FOutput write FOutput;
|
2007-06-13 07:47:25 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
TTypeInfoLess = class(TObject)
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
TSimpleTypes = class(TPersistent)
|
|
|
|
private
|
|
|
|
FBooleanValue: Boolean;
|
|
|
|
FFloatValue: Double;
|
|
|
|
FInt64Value: Int64;
|
|
|
|
FIntValue: Integer;
|
|
|
|
FStringValue: String;
|
|
|
|
public
|
|
|
|
constructor Create();
|
|
|
|
published
|
|
|
|
property BooleanValue: Boolean read FBooleanValue write FBooleanValue;
|
|
|
|
property FloatValue: Double read FFloatValue write FFloatValue;
|
|
|
|
property Int64Value: Int64 read FInt64Value write FInt64Value;
|
|
|
|
property IntValue: Integer read FIntValue write FIntValue;
|
|
|
|
property StringValue: String read FStringValue write FStringValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ TSimpleTypes }
|
|
|
|
constructor TSimpleTypes.Create();
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
FIntValue := 42;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ TPersistTest }
|
2007-08-10 14:48:00 +02:00
|
|
|
procedure TPersistTest.QueryReaderWriter;
|
|
|
|
var
|
|
|
|
persistTest: TX2UtPersistTest;
|
|
|
|
|
|
|
|
begin
|
|
|
|
persistTest := TX2UtPersistTest.Create();
|
|
|
|
try
|
|
|
|
{ Regular filer }
|
|
|
|
CheckTrue(Supports(persistTest.CreateReader(), IX2PersistReader), 'Reader -> Reader');
|
|
|
|
CheckFalse(Supports(persistTest.CreateReader(), IX2PersistWriter), 'Reader -> Writer');
|
|
|
|
|
|
|
|
CheckTrue(Supports(persistTest.CreateWriter(), IX2PersistWriter), 'Writer -> Writer');
|
|
|
|
CheckFalse(Supports(persistTest.CreateWriter(), IX2PersistReader), 'Writer -> Reader');
|
|
|
|
|
|
|
|
{ Section proxy }
|
|
|
|
CheckTrue(Supports(persistTest.CreateSectionReader('Test.Section'), IX2PersistReader), 'Section Reader -> Reader');
|
|
|
|
CheckFalse(Supports(persistTest.CreateSectionReader('Test.Section'), IX2PersistWriter), 'Section Reader -> Writer');
|
|
|
|
|
|
|
|
CheckTrue(Supports(persistTest.CreateSectionWriter('Test.Section'), IX2PersistWriter), 'Section Writer -> Writer');
|
|
|
|
CheckFalse(Supports(persistTest.CreateSectionWriter('Test.Section'), IX2PersistReader), 'Section Writer -> Reader');
|
|
|
|
finally
|
|
|
|
FreeAndNil(persistTest);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-06-13 07:47:25 +02:00
|
|
|
procedure TPersistTest.WriteNoTypeInfo;
|
|
|
|
var
|
|
|
|
testObject: TTypeInfoLess;
|
|
|
|
|
|
|
|
begin
|
|
|
|
testObject := TTypeInfoLess.Create;
|
|
|
|
try
|
|
|
|
with TX2UtPersistTest.Create do
|
|
|
|
try
|
|
|
|
Write(testObject);
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
CheckEquals('', Output.Lines.Text);
|
2007-06-13 07:47:25 +02:00
|
|
|
finally
|
|
|
|
Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
FreeAndNil(testObject);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TPersistTest.WriteSimpleTypes;
|
|
|
|
var
|
|
|
|
testObject: TSimpleTypes;
|
|
|
|
|
|
|
|
begin
|
|
|
|
testObject := TSimpleTypes.Create;
|
|
|
|
try
|
|
|
|
with TX2UtPersistTest.Create do
|
|
|
|
try
|
|
|
|
Write(testObject);
|
|
|
|
|
2013-12-30 10:31:32 +01:00
|
|
|
// CheckEquals('Integer:42'#13#10, Output.Lines.Text);
|
2007-06-13 07:47:25 +02:00
|
|
|
finally
|
|
|
|
Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
FreeAndNil(testObject);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TX2UtPersistTest }
|
|
|
|
constructor TX2UtPersistTest.Create();
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
FOutput := TPersistTestOutput.Create();
|
2007-06-13 07:47:25 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTest.CreateFiler(AIsReader: Boolean): IX2PersistFiler;
|
|
|
|
var
|
|
|
|
testFiler: TX2UtPersistTestFiler;
|
2007-06-13 07:47:25 +02:00
|
|
|
|
|
|
|
begin
|
2016-04-15 15:37:58 +02:00
|
|
|
testFiler := TX2UtPersistTestFiler.Create(AIsReader);
|
2007-08-10 14:48:00 +02:00
|
|
|
testFiler.Output := Self.Output;
|
2007-06-13 07:47:25 +02:00
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
Result := testFiler;
|
2007-06-13 07:47:25 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
{ TX2UtPersistTestFiler }
|
|
|
|
function TX2UtPersistTestFiler.BeginSection(const AName: String): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
|
|
|
Result := inherited BeginSection(AName);
|
|
|
|
if Result then
|
2007-08-10 14:48:00 +02:00
|
|
|
Output.Lines.Add(AName + ' {');
|
2007-06-13 07:47:25 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
procedure TX2UtPersistTestFiler.EndSection();
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Output.Lines.Add('}');
|
2007-06-13 07:47:25 +02:00
|
|
|
inherited EndSection();
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.ReadFloat(const AName: String; out AValue: Extended): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.ReadInt64(const AName: String; out AValue: Int64): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.ReadInteger(const AName: String; out AValue: Integer): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.ReadString(const AName: String; out AValue: String): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.WriteFloat(const AName: String; AValue: Extended): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Output.Lines.Add(Format('Float:%.2f', [AValue]));
|
2007-06-13 07:47:25 +02:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.WriteInt64(const AName: String; AValue: Int64): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Output.Lines.Add(Format('Int64:%d', [AValue]));
|
2007-06-13 07:47:25 +02:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.WriteInteger(const AName: String; AValue: Integer): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Output.Lines.Add(Format('Integer:%d', [AValue]));
|
2007-06-13 07:47:25 +02:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistTestFiler.WriteString(const AName, AValue: String): Boolean;
|
2007-06-13 07:47:25 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Output.Lines.Add(Format('String:%s', [AValue]));
|
2007-06-13 07:47:25 +02:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-04-15 15:37:58 +02:00
|
|
|
procedure TX2UtPersistTestFiler.GetKeys(const ADest: TStrings);
|
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2UtPersistTestFiler.GetSections(const ADest: TStrings);
|
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2UtPersistTestFiler.DeleteKey(const AName: string);
|
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2UtPersistTestFiler.DeleteSection(const AName: string);
|
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
{ TPersistTestOutput }
|
|
|
|
constructor TPersistTestOutput.Create();
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
FLines := TStringList.Create();
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
destructor TPersistTestOutput.Destroy();
|
|
|
|
begin
|
|
|
|
FreeAndNil(FLines);
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TPersistTestOutput.GetLines(): TStrings;
|
|
|
|
begin
|
|
|
|
Result := FLines;
|
|
|
|
end;
|
|
|
|
|
2007-06-13 07:47:25 +02:00
|
|
|
initialization
|
|
|
|
RegisterTest(TPersistTest.Suite);
|
|
|
|
|
|
|
|
end.
|
|
|
|
|