Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 61ce70a

Browse files
authored
Merge pull request #397 from github/fixes/tracking-collection-add-remove-should-throw
Tracking collection should throw some more
2 parents e39e298 + 146aad8 commit 61ce70a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/GitHub.Exports.Reactive/Collections/TrackingCollection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ public IDisposable Subscribe(Action<T> onNext, Action onCompleted)
397397

398398
public void AddItem(T item)
399399
{
400+
if (source == null)
401+
throw new InvalidOperationException("No source observable has been set. Call Listen or pass an observable to the constructor");
400402
if (disposed)
401403
throw new ObjectDisposedException("TrackingCollection");
402404

@@ -411,6 +413,8 @@ public void AddItem(T item)
411413

412414
public void RemoveItem(T item)
413415
{
416+
if (source == null)
417+
throw new InvalidOperationException("No source observable has been set. Call Listen or pass an observable to the constructor");
414418
if (disposed)
415419
throw new ObjectDisposedException("TrackingCollection");
416420

src/TrackingCollectionTests/TrackingCollectionTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,4 +1943,18 @@ public void ListeningTwiceWorks()
19431943

19441944
CollectionAssert.AreEqual(list2, col);
19451945
}
1946+
1947+
[Test]
1948+
public void AddingWithNoObservableSetThrows()
1949+
{
1950+
ITrackingCollection<Thing> col = new TrackingCollection<Thing>();
1951+
Assert.Throws<InvalidOperationException>(() => col.AddItem(new Thing()));
1952+
}
1953+
1954+
[Test]
1955+
public void RemovingWithNoObservableSetThrows()
1956+
{
1957+
ITrackingCollection<Thing> col = new TrackingCollection<Thing>();
1958+
Assert.Throws<InvalidOperationException>(() => col.RemoveItem(new Thing()));
1959+
}
19461960
}

0 commit comments

Comments
 (0)