Skip to content

Commit 7c52a48

Browse files
committed
Working on fluent assertions
1 parent 8c239a1 commit 7c52a48

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

CodeJam.Main/Assertions/ArgumentAssertionExtensions.cs

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,106 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Runtime.CompilerServices;
5+
6+
using JetBrains.Annotations;
7+
8+
using static CodeJam.PlatformDependent;
29

310
namespace CodeJam
411
{
12+
/// <summary>
13+
/// <see cref="ArgumentAssertion{T}"/> extension methods.
14+
/// </summary>
15+
[PublicAPI]
516
public static class ArgumentAssertionExtensions
617
{
7-
public static ArgumentAssertion<T> NotNull<T>(this ArgumentAssertion<T> arg) where T : class
18+
/// <summary>
19+
/// Ensures that <paramref name="arg"/> != <c>null</c>
20+
/// </summary>
21+
/// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
22+
/// <param name="arg">The argument.</param>
23+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
24+
[AssertionMethod]
25+
public static ArgumentAssertion<T> NotNull<T>([NoEnumeration] this ArgumentAssertion<T> arg) where T : class
26+
{
27+
Code.NotNull(arg.Argument, arg.ArgumentName);
28+
return arg;
29+
}
30+
31+
/// <summary>
32+
/// Ensures that <paramref name="arg"/> != <c>null</c>
33+
/// </summary>
34+
/// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
35+
/// <param name="arg">The argument.</param>
36+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
37+
[AssertionMethod]
38+
public static ArgumentAssertion<T?> NotNull<T>([NoEnumeration] this ArgumentAssertion<T?> arg) where T : struct
839
{
940
Code.NotNull(arg.Argument, arg.ArgumentName);
1041
return arg;
1142
}
1243

44+
/// <summary>Ensures that all items in <paramref name="arg"/> != <c>null</c></summary>
45+
/// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
46+
/// <param name="arg">The argument.</param>
47+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
48+
[AssertionMethod]
1349
public static ArgumentAssertion<IEnumerable<T>> ItemNotNull<T>(this ArgumentAssertion<IEnumerable<T>> arg) where T : class
1450
{
1551
Code.ItemNotNull(arg.Argument, arg.ArgumentName);
1652
return arg;
1753
}
1854

19-
public static ArgumentAssertion<T?> NotNull<T>(this ArgumentAssertion<T?> arg) where T : struct
55+
/// <summary>Ensures that <paramref name="arg"/> is not null nor empty</summary>
56+
/// <param name="arg">The argument.</param>
57+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
58+
[AssertionMethod]
59+
public static ArgumentAssertion<string> NotNullNorEmpty(this ArgumentAssertion<string> arg)
2060
{
21-
Code.NotNull(arg.Argument, arg.ArgumentName);
61+
Code.NotNullNorEmpty(arg.Argument, arg.ArgumentName);
62+
return arg;
63+
}
64+
65+
/// <summary>Ensures that <paramref name="arg"/> is not null nor white space</summary>
66+
/// <param name="arg">The argument.</param>
67+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
68+
[AssertionMethod]
69+
public static ArgumentAssertion<string> NotNullNorWhiteSpace(this ArgumentAssertion<string> arg)
70+
{
71+
Code.NotNullNorWhiteSpace(arg.Argument, arg.ArgumentName);
72+
return arg;
73+
}
74+
75+
/// <summary>Assertion for the argument value</summary>
76+
/// <param name="arg">The argument.</param>
77+
/// <param name="condition">The condition to check</param>
78+
/// <param name="message">The message.</param>
79+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
80+
[AssertionMethod]
81+
public static ArgumentAssertion<T> Assert<T>(
82+
this ArgumentAssertion<T> arg,
83+
[AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition,
84+
[NotNull] string message)
85+
{
86+
Code.AssertArgument(condition, arg.ArgumentName, message);
87+
return arg;
88+
}
89+
90+
/// <summary>Assertion for the argument value</summary>
91+
/// <param name="arg">Argument.</param>
92+
/// <param name="condition">The condition to check</param>
93+
/// <param name="messageFormat">The message format.</param>
94+
/// <param name="args">Format string arguments.</param>
95+
[DebuggerHidden, MethodImpl(AggressiveInlining)]
96+
[AssertionMethod, StringFormatMethod("messageFormat")]
97+
public static ArgumentAssertion<T> Assert<T>(
98+
this ArgumentAssertion<T> arg,
99+
[AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition,
100+
[NotNull] string messageFormat,
101+
[CanBeNull] params object[] args)
102+
{
103+
Code.AssertArgument(condition, arg.ArgumentName, messageFormat, args);
22104
return arg;
23105
}
24106
}

CodeJam.Main/Assertions/Code.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ public static void AssertArgument(
134134
throw CodeExceptions.Argument(argName, messageFormat, args);
135135
}
136136

137+
/// <summary>
138+
/// Creates <see cref="ArgumentAssertion{T}"/> for fluent assertions.
139+
/// </summary>
140+
/// <typeparam name="T">Type of argument</typeparam>
141+
/// <param name="arg">Argument value.</param>
142+
/// <param name="argName">Argument name.</param>
143+
/// <returns><see cref="ArgumentAssertion{T}"/> instance.</returns>
137144
public static ArgumentAssertion<T> Arg<T>(T arg, string argName) => new ArgumentAssertion<T>(arg, argName);
138145
#endregion
139146

0 commit comments

Comments
 (0)