Skip to content

Commit 93a20f5

Browse files
More coverage tweaks.
1 parent e0f63dd commit 93a20f5

6 files changed

Lines changed: 76 additions & 16 deletions

File tree

source/CollectionWrapper.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace Open.Collections;
66

77
[ExcludeFromCodeCoverage]
8-
public class CollectionWrapper<T, TCollection> : ReadOnlyCollectionWrapper<T, TCollection>, ICollection<T>
8+
public class CollectionWrapper<T, TCollection>
9+
: ReadOnlyCollectionWrapper<T, TCollection>, ICollection<T>, IAddMultiple<T>
910
where TCollection : class, ICollection<T>
1011
{
1112
public CollectionWrapper(TCollection source, bool owner = false)
@@ -31,10 +32,7 @@ protected virtual void AddInternal(T item)
3132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3233
public virtual void Add(T item) => AddInternal(item);
3334

34-
/// <summary>Adds more than one item.</summary>
35-
/// <param name="item1">First item to add.</param>
36-
/// <param name="item2">Additional item to add.</param>
37-
/// <param name="items">Extended param items to add.</param>
35+
/// <inheritdoc cref="IAddMultiple{T}.AddThese(T, T, T[])"/>
3836
public virtual void AddThese(T item1, T item2, params T[] items)
3937
{
4038
AddInternal(item1);

source/IAddMultiple.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace Open.Collections;
4+
public interface IAddMultiple<T>
5+
{
6+
/// <summary>Adds more than one item.</summary>
7+
/// <param name="item1">First item to add.</param>
8+
/// <param name="item2">Additional item to add.</param>
9+
/// <param name="items">Extended param items to add.</param>
10+
void AddThese(T item1, T item2, params T[] items);
11+
12+
/// <summary>
13+
/// Adds all the items in <paramref name="items"/> to this collection.
14+
/// </summary>
15+
/// <param name="items">The items to add.</param>
16+
void AddRange(IEnumerable<T> items);
17+
}

source/Synchronized/TrackedCollectionWrapper.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Open.Collections.Synchronized;
1111

1212
public class TrackedCollectionWrapper<T, TCollection>
13-
: ModificationSynchronizedBase, ICollection<T>
13+
: ModificationSynchronizedBase, ICollection<T>, IAddMultiple<T>
1414
where TCollection : class, ICollection<T>
1515
{
1616
protected TCollection InternalSource;
@@ -57,6 +57,18 @@ public void Add(T item)
5757
return true;
5858
});
5959

60+
/// <inheritdoc cref="IAddMultiple{T}.AddThese(T, T, T[])" />
61+
public void AddThese(T item1, T item2, params T[] items)
62+
=> Sync!.Modifying(() => AssertIsAlive(), () =>
63+
{
64+
AddInternal(item1);
65+
AddInternal(item2);
66+
foreach (T? i in items)
67+
AddInternal(i);
68+
return true;
69+
});
70+
71+
/// <inheritdoc />
6072
public void AddRange(IEnumerable<T> items)
6173
{
6274
if (items is null) return;

source/Synchronized/TrackedList.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using Open.Threading;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics.CodeAnalysis;
45

56
namespace Open.Collections.Synchronized;
67

78
public class TrackedList<T> : TrackedCollectionWrapper<T, IList<T>>, IList<T>
89
{
10+
[ExcludeFromCodeCoverage]
911
public TrackedList(ModificationSynchronizer? sync = null) : base(new List<T>(), sync)
1012
{
1113
}
1214

15+
[ExcludeFromCodeCoverage]
1316
public TrackedList(out ModificationSynchronizer sync) : base(new List<T>(), out sync)
1417
{
1518
}
@@ -41,16 +44,6 @@ bool changing
4144
return changing;
4245
}
4346

44-
public void Add(T item, T item2, params T[] items)
45-
=> Sync!.Modifying(() => AssertIsAlive(), () =>
46-
{
47-
AddInternal(item);
48-
AddInternal(item2);
49-
foreach (var i in items)
50-
AddInternal(i);
51-
return true;
52-
});
53-
5447
/// <inheritdoc />
5548
public int IndexOf(T item)
5649
=> Sync!.Reading(

testing/Open.Collections.Tests/BasicCollectionTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@ public void Add()
2828
Collection.Count.Should().Be(count + 1);
2929
}
3030

31+
[Fact]
32+
public void AddThese()
33+
{
34+
if (Collection is not IAddMultiple<int> c) return;
35+
if (Collection.IsReadOnly)
36+
{
37+
Assert.Throws<Exception>(() => c.AddThese(1, 2, 3, 4));
38+
return;
39+
}
40+
41+
int count = Collection.Count;
42+
c.AddThese(1, 2, 3, 4);
43+
Collection.Count.Should().Be(count + 4);
44+
}
45+
46+
[Fact]
47+
public void AddRange()
48+
{
49+
if (Collection is not IAddMultiple<int> c) return;
50+
var e = Enumerable.Range(1, 4);
51+
if (Collection.IsReadOnly)
52+
{
53+
Assert.Throws<Exception>(() => c.AddRange(e));
54+
return;
55+
}
56+
57+
int count = Collection.Count;
58+
c.AddRange(e);
59+
Collection.Count.Should().Be(count + 4);
60+
}
61+
3162
[Fact]
3263
public void Clear()
3364
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Open.Collections.Synchronized;
2+
3+
namespace Open.Collections.Tests.Collections;
4+
public class TrackedListTests : BasicListTests<TrackedList<int>>
5+
{
6+
public TrackedListTests() : base(new())
7+
{
8+
}
9+
}

0 commit comments

Comments
 (0)