|
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; |
2 | 9 |
|
3 | 10 | namespace CodeJam |
4 | 11 | { |
| 12 | + /// <summary> |
| 13 | + /// <see cref="ArgumentAssertion{T}"/> extension methods. |
| 14 | + /// </summary> |
| 15 | + [PublicAPI] |
5 | 16 | public static class ArgumentAssertionExtensions |
6 | 17 | { |
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 |
8 | 39 | { |
9 | 40 | Code.NotNull(arg.Argument, arg.ArgumentName); |
10 | 41 | return arg; |
11 | 42 | } |
12 | 43 |
|
| 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] |
13 | 49 | public static ArgumentAssertion<IEnumerable<T>> ItemNotNull<T>(this ArgumentAssertion<IEnumerable<T>> arg) where T : class |
14 | 50 | { |
15 | 51 | Code.ItemNotNull(arg.Argument, arg.ArgumentName); |
16 | 52 | return arg; |
17 | 53 | } |
18 | 54 |
|
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) |
20 | 60 | { |
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); |
22 | 104 | return arg; |
23 | 105 | } |
24 | 106 | } |
|
0 commit comments