Skip to content

Commit

Permalink
IObservableList items as IReadOnlyList (#912)
Browse files Browse the repository at this point in the history
* IObservableList items as IReadOnlyList

* Fix test

* use indexer

* Fix api

---------

Co-authored-by: Roland Pheasant <[email protected]>
  • Loading branch information
kronic and RolandPheasant authored Jun 29, 2024
1 parent 903afc8 commit af2a29e
Show file tree
Hide file tree
Showing 41 changed files with 74 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ namespace DynamicData
{
int Count { get; }
System.IObservable<int> CountChanged { get; }
System.Collections.Generic.IEnumerable<T> Items { get; }
System.Collections.Generic.IReadOnlyList<T> Items { get; }
System.IObservable<DynamicData.IChangeSet<T>> Connect(System.Func<T, bool>? predicate = null);
System.IObservable<DynamicData.IChangeSet<T>> Preview(System.Func<T, bool>? predicate = null);
}
Expand Down Expand Up @@ -2642,7 +2642,7 @@ namespace DynamicData
public SourceList(System.IObservable<DynamicData.IChangeSet<T>>? source = null) { }
public int Count { get; }
public System.IObservable<int> CountChanged { get; }
public System.Collections.Generic.IEnumerable<T> Items { get; }
public System.Collections.Generic.IReadOnlyList<T> Items { get; }
public System.IObservable<DynamicData.IChangeSet<T>> Connect(System.Func<T, bool>? predicate = null) { }
public void Dispose() { }
public void Edit(System.Action<DynamicData.IExtendedList<T>> updateAction) { }
Expand Down
4 changes: 2 additions & 2 deletions src/DynamicData.Tests/Binding/AvaloniaDictionaryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Add()

_results.Messages.Count.Should().Be(1);
_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(person);
_results.Data.Items[0].Should().Be(person);
}

[Fact]
Expand All @@ -48,7 +48,7 @@ public void Replace()
_collection["Someone"] = person2;

_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(person2);
_results.Data.Items[0].Should().Be(person2);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Add()

_results.Messages.Count.Should().Be(1);
_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

public void Dispose() => _results.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void AddToSourceAddsToDestination()
_source.AddOrUpdate(person);

_list.Count.Should().Be(1, "Should be 1 item in the collection");
_list.Items.First().Should().Be(person, "Should be same person");
_list.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down Expand Up @@ -97,6 +97,6 @@ public void UpdateToSourceUpdatesTheDestination()
_source.AddOrUpdate(personUpdated);

_list.Count.Should().Be(1, "Should be 1 item in the collection");
_list.Items.First().Should().Be(personUpdated, "Should be updated person");
_list.Items[0].Should().Be(personUpdated, "Should be updated person");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void AddToSourceAddsToDestination()
_source.AddOrUpdate(person);

_list.Count.Should().Be(1, "Should be 1 item in the collection");
_list.Items.First().Should().Be(person, "Should be same person");
_list.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void AddToSourceAddsToDestination()
_source.Add(person);

_list.Count.Should().Be(1, "Should be 1 item in the collection");
_list.Items.First().Should().Be(person, "Should be same person");
_list.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down Expand Up @@ -191,6 +191,6 @@ public void UpdateToSourceUpdatesTheDestination()
_source.Replace(person, personUpdated);

_list.Count.Should().Be(1, "Should be 1 item in the collection");
_list.Items.First().Should().Be(personUpdated, "Should be updated person");
_list.Items[0].Should().Be(personUpdated, "Should be updated person");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Add()

_results.Messages.Count.Should().Be(1);
_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

public void Dispose() => _results.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Add()

_results.Messages.Count.Should().Be(1);
_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

public void Dispose() => _results.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Add()

_results.Messages.Count.Should().Be(1);
_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

public void Dispose() => _results.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void ResultContainsChildrenAddedWithInsert()
{
// Arrange
var randomOwner = _randomizer.ListItem(_animalOwners.Items.ToList());
var insertIndex = _randomizer.Number(randomOwner.Animals.Items.Count());
var insertIndex = _randomizer.Number(randomOwner.Animals.Items.Count);
var insertThis = _animalFaker.Generate();
var initialCount = _animalOwners.Items.Sum(owner => owner.Animals.Count);

Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData.Tests/Cache/TransformManyAsyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private static Func<AnimalOwner, Guid, Task<IObservableCache<Animal, int>>> Sele
CreateSelector(static owner => owner.ObservableCache, delayFactory);

private static Func<AnimalOwner, Guid, Task<IEnumerable<Animal>>> SelectEnumerable(Func<Task>? delayFactory = null) =>
CreateSelector(static owner => owner.Animals.Items, delayFactory);
CreateSelector(static owner => owner.Animals.Items.AsEnumerable(), delayFactory);

private static Func<AnimalOwner, Guid, Task<T>> CreateSelector<T>(Func<AnimalOwner, T> selector, Func<Task>? delayFactory = null) =>
(delayFactory != null)
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData.Tests/EnumerableIListFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void EnumerableIListTests()
rng.NextBytes(data);

var inputData = new byte[39];
var lastItem = data[data.Length - 1];
var lastItem = data[^1];
var firstItem = data[0];
Array.Copy(data, 1, inputData, 0, 38);
var listOfRandomFloats = new List<byte>(inputData);
Expand Down
14 changes: 7 additions & 7 deletions src/DynamicData.Tests/List/AutoRefreshFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ void CheckContent()
//change the value and move to a grouping which does not yet exist
items[1].Age = -1;
results.Data.Count.Should().Be(11);
results.Data.Items.Last().GroupKey.Should().Be(-1);
results.Data.Items.Last().List.Count.Should().Be(1);
results.Data.Items.First().List.Count.Should().Be(9);
results.Data.Items[^1].GroupKey.Should().Be(-1);
results.Data.Items[^1].List.Count.Should().Be(1);
results.Data.Items[0].List.Count.Should().Be(9);
CheckContent();

//put the value back where it was and check the group was removed
Expand Down Expand Up @@ -230,9 +230,9 @@ void CheckContent()
//change the value and move to a grouping which does not yet exist
items[1].Age = -1;
results.Data.Count.Should().Be(11);
results.Data.Items.Last().Key.Should().Be(-1);
results.Data.Items.Last().Count.Should().Be(1);
results.Data.Items.First().Count.Should().Be(9);
results.Data.Items[^1].Key.Should().Be(-1);
results.Data.Items[^1].Count.Should().Be(1);
results.Data.Items[0].Count.Should().Be(9);
CheckContent();

//put the value back where it was and check the group was removed
Expand Down Expand Up @@ -354,7 +354,7 @@ public void RefreshTransformAsList()
var obj = new Example { Value = 0 };
list.Add(obj);
obj.Value = 1;
valueList.Items.First().Should().Be(1);
valueList.Items[0].Should().Be(1);
}

private class Example : AbstractNotifyPropertyChanged
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData.Tests/List/DisposeManyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void ItemsAreDisposedAfterRemovalOrReplacement()
_itemsSource.AddRange(items[7..10]);
_changeSetsSource.OnNext(new ChangeSet<DisposableObject>() // Refresh
{
new(ListChangeReason.Refresh, current: _itemsSource.Items.First(), index: 0)
new(ListChangeReason.Refresh, current: _itemsSource.Items[0], index: 0)
});

_results.Exception.Should().BeNull();
Expand Down
6 changes: 3 additions & 3 deletions src/DynamicData.Tests/List/DistinctValuesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void DuplicatedResultsResultInNoAdditionalMessage()

_results.Messages.Count.Should().Be(1, "Should be 1 update message");
_results.Data.Count.Should().Be(1, "Should be 1 items in the cache");
_results.Data.Items.First().Should().Be(20, "Should 20");
_results.Data.Items[0].Should().Be(20, "Should 20");
}

[Fact]
Expand All @@ -68,7 +68,7 @@ public void FiresAddWhenaNewItemIsAdded()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
_results.Data.Items.First().Should().Be(20, "Should 20");
_results.Data.Items[0].Should().Be(20, "Should 20");
}

[Fact]
Expand All @@ -86,7 +86,7 @@ public void FiresBatchResultOnce()
_results.Data.Count.Should().Be(3, "Should be 3 items in the cache");

_results.Data.Items.Should().BeEquivalentTo(new[] { 20, 21, 22 });
_results.Data.Items.First().Should().Be(20, "Should 20");
_results.Data.Items[0].Should().Be(20, "Should 20");
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions src/DynamicData.Tests/List/DynamicOrFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void RefreshPassesThrough()
results.Data.Count.Should().Be(2);
results.Messages.Count.Should().Be(3);
results.Messages[2].Refreshes.Should().Be(1);
results.Messages[2].First().Item.Current.Should().Be(source1.Items.First());
results.Messages[2].First().Item.Current.Should().Be(source1.Items[0]);
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ public void IncludedWhenItemIsInOneSource()
_source1.Add(1);

_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

[Fact]
Expand All @@ -137,7 +137,7 @@ public void IncludedWhenItemIsInTwoSources()
_source1.Add(1);
_source2.Add(1);
_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData.Tests/List/DynamicXOrFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void IncludedWhenItemIsInOneSource()
_source1.Add(1);

_results.Data.Count.Should().Be(1);
_results.Data.Items.First().Should().Be(1);
_results.Data.Items[0].Should().Be(1);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void AddMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
_results.Data.Items.First().Should().Be(person, "Should be same person");
_results.Data.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down Expand Up @@ -64,7 +64,7 @@ public void AddNotMatchedAndUpdateMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Messages[0].First().Range.First().Should().Be(matched, "Should be same person");
_results.Data.Items.First().Should().Be(matched, "Should be same person");
_results.Data.Items[0].Should().Be(matched, "Should be same person");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void AddMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
_results.Data.Items.First().Should().Be(person, "Should be same person");
_results.Data.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down Expand Up @@ -64,7 +64,7 @@ public void AddNotMatchedAndUpdateMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Messages[0].First().Range.First().Should().Be(matched, "Should be same person");
_results.Data.Items.First().Should().Be(matched, "Should be same person");
_results.Data.Items[0].Should().Be(matched, "Should be same person");
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions src/DynamicData.Tests/List/FilterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void AddMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
_results.Data.Items.First().Should().Be(person, "Should be same person");
_results.Data.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down Expand Up @@ -58,7 +58,7 @@ public void AddNotMatchedAndUpdateMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Messages[0].First().Range.First().Should().Be(matched, "Should be same person");
_results.Data.Items.First().Should().Be(matched, "Should be same person");
_results.Data.Items[0].Should().Be(matched, "Should be same person");
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions src/DynamicData.Tests/List/FilterWithObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void AddMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");
_results.Data.Items.First().Should().Be(person, "Should be same person");
_results.Data.Items[0].Should().Be(person, "Should be same person");
}

[Fact]
Expand Down Expand Up @@ -66,7 +66,7 @@ public void AddNotMatchedAndUpdateMatched()

_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Messages[0].First().Range.First().Should().Be(matched, "Should be same person");
_results.Data.Items.First().Should().Be(matched, "Should be same person");
_results.Data.Items[0].Should().Be(matched, "Should be same person");
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions src/DynamicData.Tests/List/GroupImmutableFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void FiresOnlyOnceForABatchOfUniqueValues()

_results.Messages.Count.Should().Be(1);
_results.Messages.First().Adds.Should().Be(1);
_results.Data.Items.First().Count.Should().Be(4);
_results.Data.Items[0].Count.Should().Be(4);
}

[Fact]
Expand Down Expand Up @@ -162,7 +162,7 @@ public void UpdateAnItemWillChangedThegroup()
_results.Messages.First().Adds.Should().Be(1);
_results.Messages.Skip(1).First().Adds.Should().Be(1);
_results.Messages.Skip(1).First().Removes.Should().Be(1);
var group = _results.Data.Items.First();
var group = _results.Data.Items[0];
group.Count.Should().Be(1);

group.Key.Should().Be(21);
Expand All @@ -178,7 +178,7 @@ public void UpdatesArePermissible()
_results.Messages.First().Adds.Should().Be(1);
_results.Messages.Skip(1).First().Replaced.Should().Be(1);

var group = _results.Data.Items.First();
var group = _results.Data.Items[0];
group.Count.Should().Be(2);
}
}
4 changes: 2 additions & 2 deletions src/DynamicData.Tests/List/GroupOnFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Add()
_results.Messages.Count.Should().Be(1, "Should be 1 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");

var firstGroup = _results.Data.Items.First().List.Items.ToArray();
var firstGroup = _results.Data.Items[0].List.Items.ToArray();
firstGroup[0].Should().Be(person, "Should be same person");
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public void UpdateWillChangeTheGroup()
_results.Messages.Count.Should().Be(2, "Should be 2 updates");
_results.Data.Count.Should().Be(1, "Should be 1 item in the cache");

var firstGroup = _results.Data.Items.First().List.Items.ToArray();
var firstGroup = _results.Data.Items[0].List.Items.ToArray();
firstGroup[0].Should().Be(amended, "Should be same person");
}
}
4 changes: 2 additions & 2 deletions src/DynamicData.Tests/List/GroupOnPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CanGroupOnAdds()

_results.Data.Count.Should().Be(1);

var firstGroup = _results.Data.Items.First();
var firstGroup = _results.Data.Items[0];

firstGroup.Count.Should().Be(1);
firstGroup.Key.Should().Be(10);
Expand Down Expand Up @@ -93,7 +93,7 @@ public void Regroup()
person.Age = 20;

_results.Data.Count.Should().Be(1);
var firstGroup = _results.Data.Items.First();
var firstGroup = _results.Data.Items[0];

firstGroup.Count.Should().Be(1);
firstGroup.Key.Should().Be(20);
Expand Down
Loading

0 comments on commit af2a29e

Please sign in to comment.