Code formatting
This commit is contained in:
+70
-69
@@ -9,19 +9,19 @@ uses
|
||||
|
||||
type
|
||||
// Dummy interface and class for testing
|
||||
IMyTestInterface = interface( IInterface )
|
||||
IMyTestInterface = interface(IInterface)
|
||||
['{7A8C1A01-1C6B-4A7A-B9D8-28E6A8D02A0F}']
|
||||
// Unique GUID
|
||||
procedure Foo;
|
||||
function GetValue: Integer;
|
||||
end;
|
||||
|
||||
TMyTestReceiver = class( TInterfacedObject, IMyTestInterface )
|
||||
TMyTestReceiver = class(TInterfacedObject, IMyTestInterface)
|
||||
private
|
||||
FValue: Integer;
|
||||
FCallCount: Integer;
|
||||
public
|
||||
constructor Create( AValue: Integer );
|
||||
constructor Create(AValue: Integer);
|
||||
procedure Foo;
|
||||
function GetValue: Integer;
|
||||
property CallCount: Integer read FCallCount write FCallCount;
|
||||
@@ -53,7 +53,7 @@ implementation
|
||||
|
||||
{ TMyTestReceiver }
|
||||
|
||||
constructor TMyTestReceiver.Create( AValue: Integer );
|
||||
constructor TMyTestReceiver.Create(AValue: Integer);
|
||||
begin
|
||||
inherited Create;
|
||||
FValue := AValue;
|
||||
@@ -62,7 +62,7 @@ end;
|
||||
|
||||
procedure TMyTestReceiver.Foo;
|
||||
begin
|
||||
Inc( FCallCount );
|
||||
Inc(FCallCount);
|
||||
end;
|
||||
|
||||
function TMyTestReceiver.GetValue: Integer;
|
||||
@@ -93,7 +93,7 @@ begin
|
||||
// This call is expected to succeed without raising EAssertionFailed or other exceptions
|
||||
// if TMycNotifyList.Destroy is implemented correctly.
|
||||
FNotifier.Destroy;
|
||||
Assert.IsTrue( True, 'FNotifier.Destroy completed without raising an exception.' );
|
||||
Assert.IsTrue(True, 'FNotifier.Destroy completed without raising an exception.');
|
||||
end;
|
||||
|
||||
// Test for: Correct execution of Notify on an empty, locked, and finalized list.
|
||||
@@ -105,22 +105,23 @@ var
|
||||
dummyPredicate: TPredicate<IMyTestInterface>;
|
||||
begin
|
||||
predicateCallCount := 0;
|
||||
dummyPredicate := function( Item: IMyTestInterface ): Boolean
|
||||
begin
|
||||
Inc( predicateCallCount );
|
||||
Result := True;
|
||||
end;
|
||||
dummyPredicate :=
|
||||
function(Item: IMyTestInterface): Boolean
|
||||
begin
|
||||
Inc(predicateCallCount);
|
||||
Result := True;
|
||||
end;
|
||||
|
||||
FNotifier.Lock;
|
||||
Assert.IsTrue( FNotifier.IsLocked, 'Notifier should be locked.' );
|
||||
Assert.IsTrue(FNotifier.IsLocked, 'Notifier should be locked.');
|
||||
|
||||
// FNotifier.Finalize; // Internally, FFirst becomes 2 (locked & finalized state bits) if it was 0 after Lock
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
// FNotifier.Finalize; // Internally, FFirst becomes 2 (locked & finalized state bits) if it was 0 after Lock
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
|
||||
// In a corrected Notifier, this call should not cause an AV and not call the predicate.
|
||||
FNotifier.Notify( dummyPredicate );
|
||||
Assert.AreEqual( 0, predicateCallCount, 'Notify predicate should not have been called for an empty, finalized list.' );
|
||||
Assert.IsTrue( True, 'FNotifier.Notify on finalized locked empty list completed without AV.' );
|
||||
FNotifier.Notify(dummyPredicate);
|
||||
Assert.AreEqual(0, predicateCallCount, 'Notify predicate should not have been called for an empty, finalized list.');
|
||||
Assert.IsTrue(True, 'FNotifier.Notify on finalized locked empty list completed without AV.');
|
||||
|
||||
if FNotifier.IsLocked then // Release lock for subsequent operations or cleanup
|
||||
begin
|
||||
@@ -134,14 +135,14 @@ end;
|
||||
procedure TMycTestNotifierTests.Test03_UnadviseAllOnFinalizedLockedEmptyListExecutesWithoutErrors;
|
||||
begin
|
||||
FNotifier.Lock;
|
||||
Assert.IsTrue( FNotifier.IsLocked, 'Notifier should be locked.' );
|
||||
Assert.IsTrue(FNotifier.IsLocked, 'Notifier should be locked.');
|
||||
|
||||
// FNotifier.Finalize; // FFirst becomes 2 internally
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
// FNotifier.Finalize; // FFirst becomes 2 internally
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
|
||||
// In a corrected Notifier, this call should not cause an AV.
|
||||
FNotifier.UnadviseAll;
|
||||
Assert.IsTrue( True, 'FNotifier.UnadviseAll on finalized locked empty list completed without AV.' );
|
||||
Assert.IsTrue(True, 'FNotifier.UnadviseAll on finalized locked empty list completed without AV.');
|
||||
|
||||
if FNotifier.IsLocked then // Release lock
|
||||
begin
|
||||
@@ -156,37 +157,37 @@ var
|
||||
tag1, tag2: TMycNotifyList<IMyTestInterface>.TTag;
|
||||
receiver1, receiver2: IMyTestInterface;
|
||||
begin
|
||||
receiver1 := TMyTestReceiver.Create( 1 );
|
||||
receiver2 := TMyTestReceiver.Create( 2 );
|
||||
receiver1 := TMyTestReceiver.Create(1);
|
||||
receiver2 := TMyTestReceiver.Create(2);
|
||||
|
||||
FNotifier.Lock;
|
||||
try
|
||||
tag1 := FNotifier.Advise( receiver1 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag1, 'Tag1 should not be nil.' );
|
||||
FNotifier.Unadvise( tag1 );
|
||||
tag1 := FNotifier.Advise(receiver1);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag1, 'Tag1 should not be nil.');
|
||||
FNotifier.Unadvise(tag1);
|
||||
|
||||
tag1 := FNotifier.Advise( receiver1 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag1, 'Tag1 should not be nil.' );
|
||||
tag1 := FNotifier.Advise(receiver1);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag1, 'Tag1 should not be nil.');
|
||||
|
||||
tag2 := FNotifier.Advise( receiver2 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag2, 'Tag2 should not be nil.' );
|
||||
Assert.AreNotEqual( tag1, tag2, 'Tag1 and Tag2 should be different.' );
|
||||
tag2 := FNotifier.Advise(receiver2);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag2, 'Tag2 should not be nil.');
|
||||
Assert.AreNotEqual(tag1, tag2, 'Tag1 and Tag2 should be different.');
|
||||
|
||||
FNotifier.Unadvise( tag1 );
|
||||
FNotifier.Unadvise( tag2 );
|
||||
FNotifier.Unadvise(tag1);
|
||||
FNotifier.Unadvise(tag2);
|
||||
|
||||
// Re-add and unadvise in different order
|
||||
tag1 := FNotifier.Advise( receiver1 );
|
||||
tag2 := FNotifier.Advise( receiver2 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag1 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag2 );
|
||||
FNotifier.Unadvise( tag2 );
|
||||
FNotifier.Unadvise( tag1 );
|
||||
tag1 := FNotifier.Advise(receiver1);
|
||||
tag2 := FNotifier.Advise(receiver2);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag1);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag2);
|
||||
FNotifier.Unadvise(tag2);
|
||||
FNotifier.Unadvise(tag1);
|
||||
|
||||
finally
|
||||
FNotifier.Release;
|
||||
end;
|
||||
Assert.IsFalse( FNotifier.IsLocked, 'Notifier should be unlocked.' );
|
||||
Assert.IsFalse(FNotifier.IsLocked, 'Notifier should be unlocked.');
|
||||
end;
|
||||
|
||||
// Test for: Notify calls advised items and can remove items based on the predicate.
|
||||
@@ -196,30 +197,30 @@ var
|
||||
rcv1, rcv2, rcv3: TMyTestReceiver;
|
||||
itemsProcessedCount: Integer;
|
||||
begin
|
||||
rcv1 := TMyTestReceiver.Create( 10 ); // Keep
|
||||
rcv2 := TMyTestReceiver.Create( 20 ); // Remove
|
||||
rcv3 := TMyTestReceiver.Create( 30 ); // Keep
|
||||
rcv1 := TMyTestReceiver.Create(10); // Keep
|
||||
rcv2 := TMyTestReceiver.Create(20); // Remove
|
||||
rcv3 := TMyTestReceiver.Create(30); // Keep
|
||||
|
||||
FNotifier.Lock;
|
||||
try
|
||||
FNotifier.Advise( rcv1 );
|
||||
FNotifier.Advise( rcv2 );
|
||||
FNotifier.Advise( rcv3 );
|
||||
FNotifier.Advise(rcv1);
|
||||
FNotifier.Advise(rcv2);
|
||||
FNotifier.Advise(rcv3);
|
||||
|
||||
itemsProcessedCount := 0;
|
||||
FNotifier.Notify(
|
||||
function( Item: IMyTestInterface ): Boolean
|
||||
begin
|
||||
Inc( itemsProcessedCount );
|
||||
TMyTestReceiver( Item ).Foo;
|
||||
Result := Item.GetValue <> 20; // Remove item with value 20
|
||||
end
|
||||
);
|
||||
function(Item: IMyTestInterface): Boolean
|
||||
begin
|
||||
Inc(itemsProcessedCount);
|
||||
TMyTestReceiver(Item).Foo;
|
||||
Result := Item.GetValue <> 20; // Remove item with value 20
|
||||
end
|
||||
);
|
||||
|
||||
Assert.AreEqual( 3, itemsProcessedCount, 'Notify predicate called for all 3 items.' );
|
||||
Assert.AreEqual( 1, rcv1.CallCount, 'Receiver1 called once.' );
|
||||
Assert.AreEqual( 1, rcv2.CallCount, 'Receiver2 called once (before removal).' );
|
||||
Assert.AreEqual( 1, rcv3.CallCount, 'Receiver3 called once.' );
|
||||
Assert.AreEqual(3, itemsProcessedCount, 'Notify predicate called for all 3 items.');
|
||||
Assert.AreEqual(1, rcv1.CallCount, 'Receiver1 called once.');
|
||||
Assert.AreEqual(1, rcv2.CallCount, 'Receiver2 called once (before removal).');
|
||||
Assert.AreEqual(1, rcv3.CallCount, 'Receiver3 called once.');
|
||||
|
||||
// Reset counts and check again
|
||||
rcv1.CallCount := 0;
|
||||
@@ -228,17 +229,17 @@ begin
|
||||
itemsProcessedCount := 0;
|
||||
|
||||
FNotifier.Notify(
|
||||
function( Item: IMyTestInterface ): Boolean
|
||||
begin
|
||||
Inc( itemsProcessedCount );
|
||||
TMyTestReceiver( Item ).Foo;
|
||||
Result := True; // Keep remaining
|
||||
end
|
||||
);
|
||||
Assert.AreEqual( 2, itemsProcessedCount, 'Notify predicate called for 2 remaining items.' );
|
||||
Assert.AreEqual( 1, rcv1.CallCount, 'Receiver1 called again.' );
|
||||
Assert.AreEqual( 0, rcv2.CallCount, 'Receiver2 (removed) not called again.' );
|
||||
Assert.AreEqual( 1, rcv3.CallCount, 'Receiver3 called again.' );
|
||||
function(Item: IMyTestInterface): Boolean
|
||||
begin
|
||||
Inc(itemsProcessedCount);
|
||||
TMyTestReceiver(Item).Foo;
|
||||
Result := True; // Keep remaining
|
||||
end
|
||||
);
|
||||
Assert.AreEqual(2, itemsProcessedCount, 'Notify predicate called for 2 remaining items.');
|
||||
Assert.AreEqual(1, rcv1.CallCount, 'Receiver1 called again.');
|
||||
Assert.AreEqual(0, rcv2.CallCount, 'Receiver2 (removed) not called again.');
|
||||
Assert.AreEqual(1, rcv3.CallCount, 'Receiver3 called again.');
|
||||
|
||||
finally
|
||||
FNotifier.Release;
|
||||
@@ -257,6 +258,6 @@ end;
|
||||
|
||||
initialization
|
||||
|
||||
TDUnitX.RegisterTestFixture( TMycTestNotifierTests );
|
||||
TDUnitX.RegisterTestFixture(TMycTestNotifierTests);
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user