Skip to content

Commit

Permalink
Concept/Prototype for testing utilities (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakenVeina authored Jul 21, 2024
1 parent f6a187f commit 8fd1124
Show file tree
Hide file tree
Showing 10 changed files with 977 additions and 497 deletions.
248 changes: 117 additions & 131 deletions src/DynamicData.Tests/Cache/ExpireAfterFixture.ForSource.cs

Large diffs are not rendered by default.

323 changes: 174 additions & 149 deletions src/DynamicData.Tests/Cache/ExpireAfterFixture.ForStream.cs

Large diffs are not rendered by default.

250 changes: 118 additions & 132 deletions src/DynamicData.Tests/List/ExpireAfterFixture.cs

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions src/DynamicData.Tests/Utilities/CacheItemRecordingObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Reactive.Concurrency;

namespace DynamicData.Tests.Utilities;

public sealed class CacheItemRecordingObserver<TObject, TKey>
: RecordingObserverBase<IChangeSet<TObject, TKey>>
where TObject : notnull
where TKey : notnull
{
private readonly List<IChangeSet<TObject, TKey>> _recordedChangeSets;
private readonly Dictionary<TKey, TObject> _recordedItemsByKey;
private readonly List<TObject> _recordedItemsSorted;

public CacheItemRecordingObserver(IScheduler scheduler)
: base(scheduler)
{
_recordedChangeSets = new();
_recordedItemsByKey = new();
_recordedItemsSorted = new();
}

public IReadOnlyList<IChangeSet<TObject, TKey>> RecordedChangeSets
=> _recordedChangeSets;

public IReadOnlyDictionary<TKey, TObject> RecordedItemsByKey
=> _recordedItemsByKey;

public IReadOnlyList<TObject> RecordedItemsSorted
=> _recordedItemsSorted;

protected override void OnNext(IChangeSet<TObject, TKey> value)
{
if (!HasFinalized)
{
_recordedChangeSets.Add(value);

foreach (var change in value)
{
switch (change.Reason)
{
case ChangeReason.Add:
_recordedItemsByKey.Add(change.Key, change.Current);
if (change.CurrentIndex is not -1)
_recordedItemsSorted.Insert(change.CurrentIndex, change.Current);
break;

case ChangeReason.Moved:
_recordedItemsSorted.RemoveAt(change.PreviousIndex);
_recordedItemsSorted.Insert(change.CurrentIndex, change.Current);
break;

case ChangeReason.Remove:
_recordedItemsByKey.Remove(change.Key);
if (change.CurrentIndex is not -1)
_recordedItemsSorted.RemoveAt(change.CurrentIndex);
break;

case ChangeReason.Update:
_recordedItemsByKey[change.Key] = change.Current;
if (change.CurrentIndex is not -1)
{
_recordedItemsSorted.RemoveAt(change.PreviousIndex);
_recordedItemsSorted.Insert(change.CurrentIndex, change.Current);
}
break;
}
}
}
}
}
74 changes: 74 additions & 0 deletions src/DynamicData.Tests/Utilities/ListItemRecordingObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Reactive.Concurrency;

namespace DynamicData.Tests.Utilities;

public sealed class ListItemRecordingObserver<T>
: RecordingObserverBase<IChangeSet<T>>
where T : notnull
{
private readonly List<IChangeSet<T>> _recordedChangeSets;
private readonly List<T> _recordedItems;

public ListItemRecordingObserver(IScheduler scheduler)
: base(scheduler)
{
_recordedChangeSets = new();
_recordedItems = new();
}

public IReadOnlyList<IChangeSet<T>> RecordedChangeSets
=> _recordedChangeSets;

public IReadOnlyList<T> RecordedItems
=> _recordedItems;

protected override void OnNext(IChangeSet<T> value)
{
if (!HasFinalized)
{
_recordedChangeSets.Add(value);

foreach (var change in value)
{
switch (change.Reason)
{
case ListChangeReason.Add:
if (change.Item.CurrentIndex is -1)
_recordedItems.Add(change.Item.Current);
else
_recordedItems.Insert(change.Item.CurrentIndex, change.Item.Current);
break;

case ListChangeReason.AddRange:
if (change.Range.Index is -1)
_recordedItems.AddRange(change.Range);
else
_recordedItems.InsertRange(change.Range.Index, change.Range);
break;

case ListChangeReason.Clear:
_recordedItems.Clear();
break;

case ListChangeReason.Moved:
_recordedItems.RemoveAt(change.Item.PreviousIndex);
_recordedItems.Insert(change.Item.CurrentIndex, change.Item.Current);
break;

case ListChangeReason.Remove:
_recordedItems.RemoveAt(change.Item.CurrentIndex);
break;

case ListChangeReason.RemoveRange:
_recordedItems.RemoveRange(change.Range.Index, change.Range.Count);
break;

case ListChangeReason.Replace:
_recordedItems[change.Item.CurrentIndex] = change.Item.Current;
break;
}
}
}
}
}
Loading

0 comments on commit 8fd1124

Please sign in to comment.