Skip to content

Commit

Permalink
Removes extension methods for ICollection, IList and IDictionary to a…
Browse files Browse the repository at this point in the history
…void ambiguous usages
  • Loading branch information
arpadbarta committed Aug 27, 2023
1 parent 6f86d7c commit 83f905c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 241 deletions.
4 changes: 2 additions & 2 deletions samples/ConsoleApp/CollectionExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public class CollectionExamples
{
public void CollectionAccessExtensions()
{
IReadOnlyList<int> collection = new[] {1, 2, 3};
var collection = new[] {1, 2, 3};

_ = collection.HasIndex(4);
_ = collection.AtIndexOrDefault(4);
_ = collection.AtIndexOrFallback(4, -1);
_ = collection.IsEmpty();

IReadOnlyDictionary<int, int> directory = new Dictionary<int, int>();
var directory = new Dictionary<int, int>();

_ = directory.AtKeyOrFallback(4, -1);
}
Expand Down
88 changes: 0 additions & 88 deletions source/PlainBytes.System.Extensions/Collections/IndexLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,6 @@ namespace PlainBytes.System.Extensions.Collections
/// </summary>
public static class IndexLookup
{
/// <summary>
/// Checks if the index is valid for the collection
/// </summary>
/// <typeparam name="T">The generic type of the collection</typeparam>
/// <param name="source">The collection that is being checked on</param>
/// <param name="index">The index that is being evaluated</param>
/// <returns>True if the index is valid</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasIndex<T>(this ICollection<T> source, int index)
{
return index > -1 && index < source.Count;
}

/// <summary>
/// Tries to get the value from the given index.
/// </summary>
/// <typeparam name="T">The generic type of the collection</typeparam>
/// <param name="source">The collection that is being accessed</param>
/// <param name="index">The index that is being evaluated</param>
/// <returns>The value if the index is valid, otherwise the default of the collection type</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T AtIndexOrDefault<T>(this IList<T> source, int index)
{
if (source.HasIndex(index))
{
return source[index];
}

Debug.WriteLine($"Index is out of bounds should have been in range of 0 and {source.Count}");

return default;
}

/// <summary>
/// Evaluates if the collection has any elements.
/// </summary>
/// <typeparam name="T">The generic type of the collection</typeparam>
/// <param name="source">The collection that is being evaluated.</param>
/// <returns>True if it has any elements, otherwise false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEmpty<T>(this ICollection<T> source)
{
return source.Count == 0;
}

/// <summary>
/// Tries to get the value from the given index.
/// </summary>
/// <typeparam name="T">The generic type of the collection</typeparam>
/// <param name="source">The collection that is being accessed</param>
/// <param name="index">The index that is being evaluated</param>
/// <param name="fallback">The fallback value</param>
/// <returns>The value if the index is valid, otherwise the provided fallback</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T AtIndexOrFallback<T>(this IList<T> source, int index, T fallback)
{
if (source.HasIndex(index))
{
return source[index];
}

Debug.WriteLine($"Index is out of bounds should have been in range of 0 and {source.Count}");

return fallback;
}

/// <summary>
/// Checks if the index is valid for the collection
/// </summary>
Expand Down Expand Up @@ -162,28 +96,6 @@ public static TValue AtKeyOrFallback<TKey, TValue>(this IReadOnlyDictionary<TKey

return fallback;
}

/// <summary>
/// Tries to get the value from the given key.
/// </summary>
/// <typeparam name="TKey">The generic type of the key</typeparam>
/// <typeparam name="TValue">The generic type of the value</typeparam>
/// <param name="source">The dictionary that is being accessed</param>
/// <param name="key">The key that is being evaluated</param>
/// <param name="fallback">The fallback value</param>
/// <returns>The value if the key exists, otherwise the provided fallback</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TValue AtKeyOrFallback<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue fallback)
{
if (source.TryGetValue(key, out var value))
{
return value;
}

Debug.WriteLine($"Index is out of bounds should have been in range of 0 and {source.Count}");

return fallback;
}

/// <summary>
/// Evaluates if the collection has any elements.
Expand Down
165 changes: 14 additions & 151 deletions tests/PlainBytes.System.Extensions.Tests/Collections/IndexLookupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class IndexLookupTests
public void HasIndex_GivenNullCollection_ThrowsNullReferenceException()
{
// Arrange
IList<int> collection = null;
int[] collection = null;

// Act
Assert.Throws<NullReferenceException>(() => collection.HasIndex(1));
Expand All @@ -22,7 +22,7 @@ public void HasIndex_GivenNullCollection_ThrowsNullReferenceException()
public void HasIndex_GivenNegativeIndex_ReturnFalse()
{
// Arrange
IList<int> collection = new[] { 1, 2, 1, 3 };
var collection = new[] { 1, 2, 1, 3 };

// Act
var result = collection.HasIndex(-5);
Expand All @@ -35,10 +35,10 @@ public void HasIndex_GivenNegativeIndex_ReturnFalse()
public void HasIndex_GivenOutOfBoundValue_ReturnFalse()
{
// Arrange
IList<int> collection = new[] { 1, 2, 1, 3 };
var collection = new[] { 1, 2, 1, 3 };

// Act
var result = collection.HasIndex(collection.Count);
var result = collection.HasIndex(collection.Length);

// Assert
Assert.False(result);
Expand All @@ -48,7 +48,7 @@ public void HasIndex_GivenOutOfBoundValue_ReturnFalse()
public void AtIndexOrDefault_GivenNullCollection_ThrowsNullReferenceException()
{
// Arrange
IList<int> collection = null;
int[] collection = null;

// Assert
Assert.Throws<NullReferenceException>(() => collection.AtIndexOrDefault(2));
Expand All @@ -58,10 +58,10 @@ public void AtIndexOrDefault_GivenNullCollection_ThrowsNullReferenceException()
public void AtIndexOrDefaultValueType_GivenInvalidIndex_ReturnDefault()
{
// Arrange
IList<int> collection = new[] { 1, 2, 1, 3 };
var collection = new[] { 1, 2, 1, 3 };

// Act
var result = collection.AtIndexOrDefault(collection.Count);
var result = collection.AtIndexOrDefault(collection.Length);

// Assert
Assert.Equal(0, result);
Expand All @@ -71,10 +71,10 @@ public void AtIndexOrDefaultValueType_GivenInvalidIndex_ReturnDefault()
public void AtIndexOrDefaultReferenceType_GivenInvalidIndex_ReturnDefault()
{
// Arrange
IList<object> collection = new[] { new object(), new object(), new object(), new object() };
var collection = new[] { new object(), new object(), new object(), new object() };

// Act
var result = collection.AtIndexOrDefault(collection.Count);
var result = collection.AtIndexOrDefault(collection.Length);

// Assert
Assert.Null(result);
Expand All @@ -84,7 +84,7 @@ public void AtIndexOrDefaultReferenceType_GivenInvalidIndex_ReturnDefault()
public void AtIndexOrFallback_GivenNullCollection_ThrowsNullReferenceException()
{
// Arrange
IList<int> collection = null;
int[] collection = null;

// Assert
Assert.Throws<NullReferenceException>(() => collection.AtIndexOrFallback(2, 0));
Expand All @@ -94,128 +94,11 @@ public void AtIndexOrFallback_GivenNullCollection_ThrowsNullReferenceException()
public void AtIndexOrFallback_GivenInvalidIndex_ReturnFallback()
{
// Arrange
IList<int> collection = new[] { 1, 2, 1, 3 };
var collection = new[] { 1, 2, 1, 3 };
const int expectedValue = int.MaxValue;

// Act
var result = collection.AtIndexOrFallback(collection.Count, expectedValue);

// Assert
Assert.Equal(expectedValue, result);
}

[Fact]
public void HasIndex_ReadOnly_GivenNullCollection_ThrowsNullReferenceException()
{
// Arrange
IReadOnlyList<int> collection = null;

// Act
Assert.Throws<NullReferenceException>(() =>collection.HasIndex(1));
}

[Fact]
public void HasIndex_ReadOnly_GivenNegativeIndex_ReturnFalse()
{
// Arrange
IReadOnlyList<int> collection = new[] { 1, 2, 1, 3 };

// Act
var result = collection.HasIndex(-5);

// Assert
Assert.False(result);
}

[Fact]
public void HasIndex_ReadOnly_GivenOutOfBoundValue_ReturnFalse()
{
// Arrange
IReadOnlyList<int> collection = new[] { 1, 2, 1, 3 };

// Act
var result = collection.HasIndex(collection.Count + 1);

// Assert
Assert.False(result);
}

[Fact]
public void AtIndexOrDefault_ReadOnly_GivenNullCollection_ThrowsNullReferenceException()
{
// Arrange
IReadOnlyList<int> collection = null;

// Assert
Assert.Throws<NullReferenceException>(() => collection.AtIndexOrDefault(2));
}

[Fact]
public void AtIndexOrDefaultValueType_ReadOnly_GivenInvalidIndex_ReturnDefault()
{
// Arrange
IReadOnlyList<int> collection = new[] { 1, 2, 1, 3 };

// Act
var result = collection.AtIndexOrDefault(collection.Count + 1);

// Assert
Assert.Equal(0, result);
}

[Fact]
public void AtIndexOrDefaultReferenceType_ReadOnly_GivenInvalidIndex_ReturnDefault()
{
// Arrange
IReadOnlyList<object> collection = new[] { new object(), new object(), new object(), new object() };

// Act
var result = collection.AtIndexOrDefault(collection.Count + 1);

// Assert
Assert.Null(result);
}

[Fact]
public void AtIndexOrFallback_ReadOnly_GivenNullCollection_ThrowsNullReferenceException()
{
// Arrange
IReadOnlyList<int> collection = null;

// Assert
Assert.Throws<NullReferenceException>(() => collection.AtIndexOrFallback(2, 0));
}

[Fact]
public void AtIndexOrFallback_ReadOnly_GivenInvalidIndex_ReturnFallback()
{
// Arrange
IReadOnlyList<int> collection = new[] { 1, 2, 1, 3 };
const int expectedValue = int.MaxValue;

// Act
var result = collection.AtIndexOrFallback(collection.Count, expectedValue);

// Assert
Assert.Equal(expectedValue, result);
}

[Fact]
public void AtKeyOrFallback_ReadOnly_GivenValidKey_ReturnValue()
{
// Arrange
const int expectedValue = 2;
const int fallback = -1;

var dictionary = (IReadOnlyDictionary<int,int>)new Dictionary<int, int>
{
{1,1},
{2,expectedValue},
{3,3}
};

// Act
var result = dictionary.AtKeyOrFallback(2, fallback);
var result = collection.AtIndexOrFallback(collection.Length, expectedValue);

// Assert
Assert.Equal(expectedValue, result);
Expand All @@ -228,7 +111,7 @@ public void AtKeyOrFallback_GivenValidKey_ReturnValue()
const int expectedValue = 2;
const int fallback = -1;

var dictionary = (IDictionary<int,int>)new Dictionary<int, int>
var dictionary = new Dictionary<int, int>
{
{1,1},
{2,expectedValue},
Expand All @@ -242,33 +125,13 @@ public void AtKeyOrFallback_GivenValidKey_ReturnValue()
Assert.Equal(expectedValue, result);
}

[Fact]
public void AtKeyOrFallback_ReadOnly_GivenValidKey_ReturnFallback()
{
// Arrange
const int expectedValue = -1;

var dictionary = (IReadOnlyDictionary<int,int>)new Dictionary<int, int>
{
{1,1},
{2,2},
{3,3}
};

// Act
var result = dictionary.AtKeyOrFallback(4, expectedValue);

// Assert
Assert.Equal(expectedValue, result);
}

[Fact]
public void AtKeyOrFallback_GivenValidKey_ReturnFallback()
{
// Arrange
const int expectedValue = -1;

var dictionary = (IDictionary<int,int>)new Dictionary<int, int>
var dictionary = new Dictionary<int, int>
{
{1,1},
{2,2},
Expand Down

0 comments on commit 83f905c

Please sign in to comment.