141 lines
4.4 KiB
ObjectPascal
141 lines
4.4 KiB
ObjectPascal
unit TestDataRecord.RawAccess;
|
|
|
|
interface
|
|
|
|
uses
|
|
DUnitX.TestFramework,
|
|
Myc.DataRecord;
|
|
|
|
type
|
|
[TestFixture]
|
|
TTestDataRecordRawAccess = class
|
|
public
|
|
[Test]
|
|
procedure TestRawAccess_Integer;
|
|
[Test]
|
|
procedure TestRawAccess_String;
|
|
[Test]
|
|
procedure TestRawAccess_Interface;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.Classes;
|
|
|
|
type
|
|
// A simple interface for testing managed type behavior.
|
|
IMyTestInterface = interface
|
|
function GetValue: integer;
|
|
end;
|
|
|
|
// Implementation of the test interface.
|
|
TMyTestInterfaceImpl = class(TInterfacedObject, IMyTestInterface)
|
|
private
|
|
FValue: Integer;
|
|
public
|
|
constructor Create(const AValue: Integer);
|
|
function GetValue: integer;
|
|
end;
|
|
|
|
constructor TMyTestInterfaceImpl.Create(const AValue: Integer);
|
|
begin
|
|
inherited Create;
|
|
FValue := AValue;
|
|
end;
|
|
|
|
function TMyTestInterfaceImpl.GetValue: integer;
|
|
begin
|
|
Result := FValue;
|
|
end;
|
|
|
|
procedure TTestDataRecordRawAccess.TestRawAccess_Integer;
|
|
var
|
|
rec: TDataRecord;
|
|
field: TDataRecord.IField;
|
|
readValue, newValue: Integer;
|
|
begin
|
|
// Arrange: Create a record with an integer field.
|
|
var builder := TDataRecord.CreateBuilder;
|
|
builder.AddField<Integer>('IntField', 123);
|
|
rec := builder.CreateRec;
|
|
field := rec.GetField('IntField');
|
|
Assert.IsNotNull(field, 'Field should exist');
|
|
|
|
// Act & Assert (GetRaw): Read the initial value via raw pointer.
|
|
readValue := 0;
|
|
field.GetRaw(@readValue);
|
|
Assert.AreEqual(123, readValue, 'GetRaw should retrieve the correct integer value');
|
|
|
|
// Act (SetRaw): Set a new value via raw pointer.
|
|
newValue := 456;
|
|
field.SetRaw(@newValue);
|
|
|
|
// Assert: Verify the new value using the typed accessor.
|
|
var typedField := rec.GetField<Integer>('IntField');
|
|
Assert.AreEqual(456, typedField.Value, 'SetRaw should update the integer value correctly');
|
|
end;
|
|
|
|
procedure TTestDataRecordRawAccess.TestRawAccess_Interface;
|
|
var
|
|
rec: TDataRecord;
|
|
field: TDataRecord.IField;
|
|
initialObj, readObj, newObj: IMyTestInterface;
|
|
begin
|
|
// Arrange: Create a record with an interface field.
|
|
initialObj := TMyTestInterfaceImpl.Create(10);
|
|
var builder := TDataRecord.CreateBuilder;
|
|
builder.AddField<IMyTestInterface>('IField', initialObj);
|
|
rec := builder.CreateRec;
|
|
field := rec.GetField('IField');
|
|
Assert.IsNotNull(field, 'Field should exist');
|
|
|
|
// Act & Assert (GetRaw): Read the initial interface via raw pointer.
|
|
readObj := nil;
|
|
field.GetRaw(@readObj);
|
|
Assert.IsNotNull(readObj, 'GetRaw should retrieve a non-nil interface');
|
|
Assert.AreSame(initialObj, readObj, 'GetRaw should retrieve the same interface instance');
|
|
Assert.AreEqual(10, readObj.GetValue, 'Value of retrieved interface should be correct');
|
|
|
|
// Act (SetRaw): Set a new interface via raw pointer.
|
|
newObj := TMyTestInterfaceImpl.Create(20);
|
|
field.SetRaw(@newObj);
|
|
|
|
// Assert: Verify the new interface is set correctly.
|
|
var typedField := rec.GetField<IMyTestInterface>('IField');
|
|
Assert.AreSame(newObj, typedField.Value, 'SetRaw should update to the new interface instance');
|
|
Assert.AreEqual(20, typedField.Value.GetValue, 'Value of new interface should be correct');
|
|
end;
|
|
|
|
procedure TTestDataRecordRawAccess.TestRawAccess_String;
|
|
var
|
|
rec: TDataRecord;
|
|
field: TDataRecord.IField;
|
|
readString, newString: string;
|
|
begin
|
|
// Arrange: Create a record with a string field.
|
|
var builder := TDataRecord.CreateBuilder;
|
|
builder.AddField<string>('StrField', 'Hello');
|
|
rec := builder.CreateRec;
|
|
field := rec.GetField('StrField');
|
|
Assert.IsNotNull(field, 'Field should exist');
|
|
|
|
// Act & Assert (GetRaw): Read the initial string via raw pointer.
|
|
readString := '';
|
|
field.GetRaw(@readString);
|
|
Assert.AreEqual('Hello', readString, 'GetRaw should retrieve the correct string value');
|
|
|
|
// Act (SetRaw): Set a new string via raw pointer.
|
|
newString := 'World';
|
|
field.SetRaw(@newString);
|
|
|
|
// Assert: Verify the new string using the typed accessor.
|
|
var typedField := rec.GetField<string>('StrField');
|
|
Assert.AreEqual('World', typedField.Value, 'SetRaw should update the string value correctly');
|
|
end;
|
|
|
|
initialization
|
|
TDUnitX.RegisterTestFixture(TTestDataRecordRawAccess);
|
|
|
|
end.
|