Changeset 342 for trunk/ItemList.pas


Ignore:
Timestamp:
Dec 22, 2024, 10:52:26 AM (10 hours ago)
Author:
chronos
Message:
  • Added: More tests.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ItemList.pas

    r336 r342  
    4141  private
    4242    procedure AssignValue(Source: TItem; Field: TItemField);
     43    function CompareValue(Item: TItem; Field: TItemField): Boolean;
    4344    procedure LoadValueFromNode(Node: TDOMNode; Field: TItemField); virtual;
    4445    procedure SaveValueToNode(Node: TDOMNode; Field: TItemField); virtual;
     
    6465    procedure SetValueReference(Index: Integer; Value: TItem);
    6566    procedure Assign(Source: TItem); virtual;
     67    function Compare(Item: TItem): Boolean; virtual;
     68    function ToString: string; override;
    6669    procedure LoadFromNode(Node: TDOMNode); virtual;
    6770    procedure SaveToNode(Node: TDOMNode); virtual;
     
    8891    function FindByName(Name: string): TItem;
    8992    function GetNewId: Integer;
     93    function ToString: string; override;
    9094    procedure Assign(Source: TItemList); virtual;
     95    function Compare(ItemList: TItemList): Boolean; virtual;
    9196    function AddItem(Name: string = ''): TItem; virtual;
    9297    function CreateItem(Name: string = ''): TItem; virtual;
     
    150155end;
    151156
     157function TItemList.Compare(ItemList: TItemList): Boolean;
     158var
     159  I: Integer;
     160begin
     161  Result := Count = ItemList.Count;
     162  if not Result then Exit;
     163  for I := 0 to Count - 1 do begin
     164    Result := Result and TItem(Items[I]).Compare(ItemList.Items[I]);
     165    if not Result then Break;
     166  end;
     167end;
     168
    152169function TItemList.AddItem(Name: string): TItem;
    153170begin
     
    269286end;
    270287
     288function TItemList.ToString: string;
     289var
     290  I: Integer;
     291begin
     292  Result := '';
     293  for I := 0 to Count - 1 do
     294  with TItem(Items[I]) do begin
     295    Result := Result + ToString + LineEnding;
     296  end;
     297end;
     298
    271299{ TItemFields }
    272300
     
    312340  if Field.DataType = dtReference then begin
    313341    SetValueReference(Field.Index, Source.GetValueReference(Field.Index));
     342  end else
     343    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     344end;
     345
     346function TItem.CompareValue(Item: TItem; Field: TItemField): Boolean;
     347begin
     348  if Field.DataType = dtString then begin
     349    Result := GetValueString(Field.Index) = Item.GetValueString(Field.Index);
     350  end else
     351  if Field.DataType = dtColor then begin
     352    Result := GetValueColor(Field.Index) = Item.GetValueColor(Field.Index);
     353  end else
     354  if Field.DataType = dtInteger then begin
     355    Result := GetValueInteger(Field.Index) = Item.GetValueInteger(Field.Index);
     356  end else
     357  if Field.DataType = dtBoolean then begin
     358    Result := GetValueBoolean(Field.Index) = Item.GetValueBoolean(Field.Index);
     359  end else
     360  if Field.DataType = dtEnumeration then begin
     361    Result := GetValueEnumeration(Field.Index) = Item.GetValueEnumeration(Field.Index);
     362  end else
     363  if Field.DataType = dtReference then begin
     364    Result := GetValueReference(Field.Index) = Item.GetValueReference(Field.Index);
    314365  end else
    315366    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     
    503554end;
    504555
     556function TItem.Compare(Item: TItem): Boolean;
     557var
     558  I: Integer;
     559  Fields: TItemFields;
     560begin
     561  Result := True;
     562  Result := Result and (Id = Item.Id);
     563  if Item is ClassType then begin
     564    Fields := GetFields;
     565    try
     566      for I := 0 to Fields.Count - 1 do begin
     567        Result := Result and CompareValue(Item, Fields[I]);
     568        if not Result then Break;
     569      end;
     570    finally
     571      Fields.Free;
     572    end;
     573  end;
     574end;
     575
     576function TItem.ToString: string;
     577var
     578  Fields: TItemFields;
     579  I: Integer;
     580begin
     581  Result := 'Id: ' + IntToStr(Id) + LineEnding;
     582  Fields := GetFields;
     583  try
     584    for I := 0 to Fields.Count - 1 do begin
     585      Result := Result + Fields[I].SysName + ': ' + GetValueAsText(Fields[I].Index) + LineEnding;
     586    end;
     587  finally
     588    Fields.Free;
     589  end;
     590end;
     591
    505592procedure TItem.LoadFromNode(Node: TDOMNode);
    506593var
Note: See TracChangeset for help on using the changeset viewer.