Skip to content

Commit 2741014

Browse files
committed
NotEmpty assertion
1 parent 7c52a48 commit 2741014

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

CodeJam.Main/Assertions/ArgumentAssertionExtensions.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,45 @@ public static ArgumentAssertion<IEnumerable<T>> ItemNotNull<T>(this ArgumentAsse
5252
return arg;
5353
}
5454

55+
/// <summary>
56+
/// Ensures that supplied enumerable is not empty.
57+
/// </summary>
58+
/// <typeparam name="T">Type of item.</typeparam>
59+
/// <param name="arg">The argument.</param>
60+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
61+
[AssertionMethod]
62+
public static ArgumentAssertion<IEnumerable<T>> NotEmpty<T>(this ArgumentAssertion<IEnumerable<T>> arg)
63+
{
64+
Code.NotEmpty(arg.Argument, arg.ArgumentName);
65+
return arg;
66+
}
67+
68+
/// <summary>
69+
/// Ensures that supplied collection is not empty.
70+
/// </summary>
71+
/// <typeparam name="T">Type of item.</typeparam>
72+
/// <param name="arg">The argument.</param>
73+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
74+
[AssertionMethod]
75+
public static ArgumentAssertion<ICollection<T>> NotEmpty<T>(this ArgumentAssertion<ICollection<T>> arg)
76+
{
77+
Code.NotEmpty(arg.Argument, arg.ArgumentName);
78+
return arg;
79+
}
80+
81+
/// <summary>
82+
/// Ensures that supplied array is not empty.
83+
/// </summary>
84+
/// <typeparam name="T">Type of item.</typeparam>
85+
/// <param name="arg">The argument.</param>
86+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
87+
[AssertionMethod]
88+
public static ArgumentAssertion<T[]> NotEmpty<T>(this ArgumentAssertion<T[]> arg)
89+
{
90+
Code.NotEmpty(arg.Argument, arg.ArgumentName);
91+
return arg;
92+
}
93+
5594
/// <summary>Ensures that <paramref name="arg"/> is not null nor empty</summary>
5695
/// <param name="arg">The argument.</param>
5796
[DebuggerHidden, MethodImpl(AggressiveInlining)]

CodeJam.Main/Assertions/Code.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,49 @@ public static void NotNull<T>(
7474
throw CodeExceptions.ArgumentNull(argName);
7575
}
7676

77+
/// <summary>
78+
/// Ensures that supplied enumerable is not empty.
79+
/// </summary>
80+
/// <typeparam name="T">Type of item.</typeparam>
81+
/// <param name="arg">Enumerable.</param>
82+
/// <param name="argName">Name of the argument.</param>
83+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
84+
[AssertionMethod]
85+
public static void NotEmpty<T>(IEnumerable<T> arg, [NotNull, InvokerParameterName] string argName)
86+
{
87+
using (var en = arg.GetEnumerator())
88+
if (!en.MoveNext())
89+
throw CodeExceptions.ArgumentEmpty(argName);
90+
}
91+
92+
/// <summary>
93+
/// Ensures that supplied collection is not empty.
94+
/// </summary>
95+
/// <typeparam name="T">Type of item.</typeparam>
96+
/// <param name="arg">Collection.</param>
97+
/// <param name="argName">Name of the argument.</param>
98+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
99+
[AssertionMethod]
100+
public static void NotEmpty<T>(ICollection<T> arg, [NotNull, InvokerParameterName] string argName)
101+
{
102+
if (arg.Count == 0)
103+
throw CodeExceptions.ArgumentEmpty(argName);
104+
}
105+
106+
/// <summary>
107+
/// Ensures that supplied array is not empty.
108+
/// </summary>
109+
/// <typeparam name="T">Type of item.</typeparam>
110+
/// <param name="arg">Array.</param>
111+
/// <param name="argName">Name of the argument.</param>
112+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
113+
[AssertionMethod]
114+
public static void NotEmpty<T>(T[] arg, [NotNull, InvokerParameterName] string argName)
115+
{
116+
if (arg.Length == 0)
117+
throw CodeExceptions.ArgumentEmpty(argName);
118+
}
119+
77120
/// <summary>Ensures that <paramref name="arg"/> is not null nor empty</summary>
78121
/// <param name="arg">The argument.</param>
79122
/// <param name="argName">Name of the argument.</param>
@@ -141,7 +184,10 @@ public static void AssertArgument(
141184
/// <param name="arg">Argument value.</param>
142185
/// <param name="argName">Argument name.</param>
143186
/// <returns><see cref="ArgumentAssertion{T}"/> instance.</returns>
144-
public static ArgumentAssertion<T> Arg<T>(T arg, string argName) => new ArgumentAssertion<T>(arg, argName);
187+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
188+
[AssertionMethod]
189+
public static ArgumentAssertion<T> Arg<T>(T arg, [InvokerParameterName] string argName) =>
190+
new ArgumentAssertion<T>(arg, argName);
145191
#endregion
146192

147193
#region Argument validation - in range

CodeJam.Main/Assertions/CodeExceptions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ public static ArgumentException ArgumentItemNull([NotNull, InvokerParameterName]
136136
.LogToCodeTraceSourceBeforeThrow();
137137
}
138138

139+
/// <summary>
140+
/// Creates <see cref="ArgumentException"/>.
141+
/// </summary>
142+
/// <param name="argumentName">Name of the argument.</param>
143+
/// <returns>Initialized instance of <see cref="ArgumentException"/></returns>
144+
[DebuggerHidden, NotNull, MethodImpl(AggressiveInlining)]
145+
[MustUseReturnValue]
146+
public static ArgumentException ArgumentEmpty([NotNull, InvokerParameterName] string argumentName)
147+
{
148+
BreakIfAttached();
149+
return new ArgumentException(
150+
argumentName,
151+
$"Collection {argumentName} must not be empty.")
152+
.LogToCodeTraceSourceBeforeThrow();
153+
}
154+
139155
/// <summary>Creates <see cref="ArgumentException"/> for empty string.</summary>
140156
/// <param name="argumentName">Name of the argument.</param>
141157
/// <returns>Initialized instance of <see cref="ArgumentException"/>.</returns>

0 commit comments

Comments
 (0)