Skip to content

Commit 4c6475b

Browse files
committed
Feat: updated extensions
1 parent 7e9eafb commit 4c6475b

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using System.Collections;
5+
46
// ReSharper disable once CheckNamespace
57
namespace System;
68
// ---------------------------------------------------------------------------------------------------------------------
79
// Code
810
// ---------------------------------------------------------------------------------------------------------------------
911
public static class CollectionExtensions {
10-
public static bool IsEmpty(this string[] arr) => arr.Length == 0;
11-
12-
public static bool IsEmpty(this IEnumerable<string> arr) => !arr.Any();
12+
public static bool IsEmpty<T>(this IEnumerable<T> source) => source switch {
13+
ICollection<T> collection => collection.Count == 0,
14+
ICollection collection => collection.Count == 0,
15+
_ => !source.Any()
16+
};
17+
18+
public static bool IsCollectionEmpty<T>(this ICollection<T> collection) => collection.Count == 0;
1319
}

tests/Tests.CodeOfChaos.Extensions/CollectionExtensionTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,60 @@ public async Task IsEmpty_Enumerable_ShouldWork(IEnumerable<string> input, bool
3333
// Assert
3434
await Assert.That(output).IsEqualTo(expected);
3535
}
36+
37+
[Test]
38+
[Arguments(new string[] {}, true)]
39+
[Arguments(new[] { "a" }, false)]
40+
[Arguments(new[] { "a", "b" }, false)]
41+
public async Task IsCollectionEmpty_Array_ShouldWork(string[] input, bool expected) {
42+
// Arrange
43+
44+
// Act
45+
bool output = input.IsCollectionEmpty();
46+
47+
// Assert
48+
await Assert.That(output).IsEqualTo(expected);
49+
}
50+
51+
[Test]
52+
[Arguments(new string[] {}, true)]
53+
[Arguments(new[] { "a" }, false)]
54+
[Arguments(new[] { "a", "b" }, false)]
55+
public async Task IsCollectionEmpty_Enumerable_ShouldWork(ICollection<string> input, bool expected) {
56+
// Arrange
57+
58+
// Act
59+
bool output = input.IsCollectionEmpty();
60+
61+
// Assert
62+
await Assert.That(output).IsEqualTo(expected);
63+
}
64+
65+
[Test]
66+
[Arguments(new string[] {}, true)]
67+
[Arguments(new[] {"a"}, false)]
68+
public async Task IsCollectionEmpty_ShouldWork_List(IEnumerable<string> input, bool expected) {
69+
// Arrange
70+
List<string> collection = input.ToList();
71+
72+
// Act
73+
bool output = collection.IsCollectionEmpty();
74+
75+
// Assert
76+
await Assert.That(output).IsEqualTo(expected);
77+
}
78+
79+
[Test]
80+
[Arguments(new string[] {}, true)]
81+
[Arguments(new[] {"a"}, false)]
82+
public async Task IsCollectionEmpty_ShouldWork_Dictionary(IEnumerable<string> input, bool expected) {
83+
// Arrange
84+
Dictionary<string, string> collection = input.ToDictionary(s => s, s =>s );
85+
86+
// Act
87+
bool output = collection.IsCollectionEmpty();
88+
89+
// Assert
90+
await Assert.That(output).IsEqualTo(expected);
91+
}
3692
}

tests/Tests.CodeOfChaos.Extensions/StringExtensionTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
#if DEBUG // fixes an issue with tests throwing an error during debug
5+
using TUnit.Core.Exceptions;
6+
#endif
7+
48
namespace Tests.CodeOfChaos.Extensions;
59
// ---------------------------------------------------------------------------------------------------------------------
610
// Code
@@ -101,12 +105,11 @@ public async Task ToGuid_ShouldReturnGuid_WhenInputIsValid(string input, string
101105
[Arguments("InvalidGuidFormat")]
102106
[Arguments("1234")]
103107
public async Task ToGuid_ShouldThrowException_WhenInputIsInvalid(string input) {
104-
// Arrange
105-
106-
// Act
107-
108-
// Assert
108+
#if DEBUG // fixes an issue with tests throwing an error during debug
109+
Assert.Throws<TUnitException>(() => input.ToGuid());
110+
#else
109111
Assert.Throws<FormatException>(() => input.ToGuid());
112+
#endif
110113
}
111114

112115
[Test]

0 commit comments

Comments
 (0)