Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Build/Props/CodeJam.Default.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AssemblyOriginatorKeyFile>..\Build\CodeJam.snk</AssemblyOriginatorKeyFile>

<Version>2.2.0.0</Version>
<PackageVersion>2.2.0-beta1</PackageVersion>
<PackageVersion>2.2.0-beta2</PackageVersion>
<PackageOutputPath>..\_Results</PackageOutputPath>

<Company>RSDN</Company>
Expand Down
8 changes: 6 additions & 2 deletions CodeJam.Blocks/Readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
CodeJam.Blocks 2.1.1 Release Notes
CodeJam.Blocks 2.2.0-beta1 Release Notes
----------------------------------

What's new in 2.2.0-beta1
-------------------------
* Additional code annotation for ReSharper

What's new in 2.1.1
-----------------------
-------------------
* Fix package dependencies
* Fix version

Expand Down
29 changes: 29 additions & 0 deletions CodeJam.Main/Assertions/ArgumentAssertion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace CodeJam
{
/// <summary>
/// Builder type for argument assertions.
/// </summary>
public struct ArgumentAssertion<T>
{
/// <summary>
/// Initialize instance.
/// </summary>
/// <param name="arg">Argument value.</param>
/// <param name="argName">Argument name.</param>
public ArgumentAssertion(T arg, string argName)
{
Argument = arg;
ArgumentName = argName;
}

/// <summary>
/// Argument value.
/// </summary>
public T Argument { get; }

/// <summary>
/// Argument name.
/// </summary>
public string ArgumentName { get; }
}
}
146 changes: 146 additions & 0 deletions CodeJam.Main/Assertions/ArgumentAssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

using JetBrains.Annotations;

using static CodeJam.PlatformDependent;

namespace CodeJam
{
/// <summary>
/// <see cref="ArgumentAssertion{T}"/> extension methods.
/// </summary>
[PublicAPI]
public static class ArgumentAssertionExtensions
{
/// <summary>
/// Ensures that <paramref name="arg"/> != <c>null</c>
/// </summary>
/// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<T> NotNull<T>([NoEnumeration] this ArgumentAssertion<T> arg) where T : class
{
Code.NotNull(arg.Argument, arg.ArgumentName);
return arg;
}

/// <summary>
/// Ensures that <paramref name="arg"/> != <c>null</c>
/// </summary>
/// <typeparam name="T">Type of the value. Auto-inferred in most cases</typeparam>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<T?> NotNull<T>([NoEnumeration] this ArgumentAssertion<T?> arg) where T : struct
{
Code.NotNull(arg.Argument, arg.ArgumentName);
return arg;
}

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

/// <summary>
/// Ensures that supplied enumerable is not empty.
/// </summary>
/// <typeparam name="T">Type of item.</typeparam>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<IEnumerable<T>> NotEmpty<T>(this ArgumentAssertion<IEnumerable<T>> arg)
{
Code.NotEmpty(arg.Argument, arg.ArgumentName);
return arg;
}

/// <summary>
/// Ensures that supplied collection is not empty.
/// </summary>
/// <typeparam name="T">Type of item.</typeparam>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<ICollection<T>> NotEmpty<T>(this ArgumentAssertion<ICollection<T>> arg)
{
Code.NotEmpty(arg.Argument, arg.ArgumentName);
return arg;
}

/// <summary>
/// Ensures that supplied array is not empty.
/// </summary>
/// <typeparam name="T">Type of item.</typeparam>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<T[]> NotEmpty<T>(this ArgumentAssertion<T[]> arg)
{
Code.NotEmpty(arg.Argument, arg.ArgumentName);
return arg;
}

/// <summary>Ensures that <paramref name="arg"/> is not null nor empty</summary>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<string> NotNullNorEmpty(this ArgumentAssertion<string> arg)
{
Code.NotNullNorEmpty(arg.Argument, arg.ArgumentName);
return arg;
}

/// <summary>Ensures that <paramref name="arg"/> is not null nor white space</summary>
/// <param name="arg">The argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<string> NotNullNorWhiteSpace(this ArgumentAssertion<string> arg)
{
Code.NotNullNorWhiteSpace(arg.Argument, arg.ArgumentName);
return arg;
}

/// <summary>Assertion for the argument value</summary>
/// <param name="arg">The argument.</param>
/// <param name="condition">The condition to check</param>
/// <param name="message">The message.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<T> Assert<T>(
this ArgumentAssertion<T> arg,
[AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition,
[NotNull] string message)
{
Code.AssertArgument(condition, arg.ArgumentName, message);
return arg;
}

/// <summary>Assertion for the argument value</summary>
/// <param name="arg">Argument.</param>
/// <param name="condition">The condition to check</param>
/// <param name="messageFormat">The message format.</param>
/// <param name="args">Format string arguments.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod, StringFormatMethod("messageFormat")]
public static ArgumentAssertion<T> Assert<T>(
this ArgumentAssertion<T> arg,
[AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition,
[NotNull] string messageFormat,
[CanBeNull] params object[] args)
{
Code.AssertArgument(condition, arg.ArgumentName, messageFormat, args);
return arg;
}
}
}
66 changes: 61 additions & 5 deletions CodeJam.Main/Assertions/Code.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
 using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

using CodeJam.Arithmetic;
using CodeJam.Strings;
using CodeJam.Arithmetic;
using CodeJam.Strings;

using JetBrains.Annotations;

Expand Down Expand Up @@ -74,14 +74,57 @@ public static void NotNull<T>(
throw CodeExceptions.ArgumentNull(argName);
}

/// <summary>
/// Ensures that supplied enumerable is not empty.
/// </summary>
/// <typeparam name="T">Type of item.</typeparam>
/// <param name="arg">Enumerable.</param>
/// <param name="argName">Name of the argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static void NotEmpty<T>(IEnumerable<T> arg, [NotNull, InvokerParameterName] string argName)
{
using (var en = arg.GetEnumerator())
if (!en.MoveNext())
throw CodeExceptions.ArgumentEmpty(argName);
}

/// <summary>
/// Ensures that supplied collection is not empty.
/// </summary>
/// <typeparam name="T">Type of item.</typeparam>
/// <param name="arg">Collection.</param>
/// <param name="argName">Name of the argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static void NotEmpty<T>(ICollection<T> arg, [NotNull, InvokerParameterName] string argName)
{
if (arg.Count == 0)
throw CodeExceptions.ArgumentEmpty(argName);
}

/// <summary>
/// Ensures that supplied array is not empty.
/// </summary>
/// <typeparam name="T">Type of item.</typeparam>
/// <param name="arg">Array.</param>
/// <param name="argName">Name of the argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static void NotEmpty<T>(T[] arg, [NotNull, InvokerParameterName] string argName)
{
if (arg.Length == 0)
throw CodeExceptions.ArgumentEmpty(argName);
}

/// <summary>Ensures that <paramref name="arg"/> is not null nor empty</summary>
/// <param name="arg">The argument.</param>
/// <param name="argName">Name of the argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
[ContractAnnotation("arg:null => stop")]
public static void NotNullNorEmpty(
[CanBeNull] string arg,
[CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string arg,
[NotNull, InvokerParameterName] string argName)
{
if (string.IsNullOrEmpty(arg))
Expand All @@ -93,8 +136,9 @@ public static void NotNullNorEmpty(
/// <param name="argName">Name of the argument.</param>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
[ContractAnnotation("arg:null => stop")]
public static void NotNullNorWhiteSpace(
[CanBeNull] string arg,
[CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string arg,
[NotNull, InvokerParameterName] string argName)
{
if (arg.IsNullOrWhiteSpace())
Expand Down Expand Up @@ -132,6 +176,18 @@ public static void AssertArgument(
if (!condition)
throw CodeExceptions.Argument(argName, messageFormat, args);
}

/// <summary>
/// Creates <see cref="ArgumentAssertion{T}"/> for fluent assertions.
/// </summary>
/// <typeparam name="T">Type of argument</typeparam>
/// <param name="arg">Argument value.</param>
/// <param name="argName">Argument name.</param>
/// <returns><see cref="ArgumentAssertion{T}"/> instance.</returns>
[DebuggerHidden, MethodImpl(AggressiveInlining)]
[AssertionMethod]
public static ArgumentAssertion<T> Arg<T>(T arg, [InvokerParameterName] string argName) =>
new ArgumentAssertion<T>(arg, argName);
#endregion

#region Argument validation - in range
Expand Down
16 changes: 16 additions & 0 deletions CodeJam.Main/Assertions/CodeExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ public static ArgumentException ArgumentItemNull([NotNull, InvokerParameterName]
.LogToCodeTraceSourceBeforeThrow();
}

/// <summary>
/// Creates <see cref="ArgumentException"/>.
/// </summary>
/// <param name="argumentName">Name of the argument.</param>
/// <returns>Initialized instance of <see cref="ArgumentException"/></returns>
[DebuggerHidden, NotNull, MethodImpl(AggressiveInlining)]
[MustUseReturnValue]
public static ArgumentException ArgumentEmpty([NotNull, InvokerParameterName] string argumentName)
{
BreakIfAttached();
return new ArgumentException(
argumentName,
$"Collection {argumentName} must not be empty.")
.LogToCodeTraceSourceBeforeThrow();
}

/// <summary>Creates <see cref="ArgumentException"/> for empty string.</summary>
/// <param name="argumentName">Name of the argument.</param>
/// <returns>Initialized instance of <see cref="ArgumentException"/>.</returns>
Expand Down
5 changes: 5 additions & 0 deletions CodeJam.Main/CodeJam.Main.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Update="Assertions\DebugCode.generated.cs">
<AutoGen>True</AutoGen>
<DependentUpon>DebugCode.tt</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Update="Assertions\DebugCode.tt">
<Generator>TextTemplatingFileGenerator</Generator>
Expand Down Expand Up @@ -351,4 +352,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions CodeJam.Main/Readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CodeJam 2.1.1 Release Notes
---------------------------
CodeJam 2.2.0-beta2 Release Notes
---------------------------------

What's new in 2.2.0-beta2
-------------------------
* Limited .NET framework 2.0 support
* ExceptionExtensions.ToDiagnosticString[Async] improvements
* DictionaryExtensions.GetOrAddAsync/AddOrUpdateAsync
* LazyDictionary ctors and Create with initial data supplied
Expand Down