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 Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2025.12.12.1</Version>
<Version>2025.12.12.2</Version>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
Expand Down
59 changes: 58 additions & 1 deletion src/libraries/TedToolkit.Assertions/AssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using TedToolkit.Assertions.Assertions;
using TedToolkit.Assertions.Constraints;

Expand Down Expand Up @@ -86,4 +87,60 @@ public static ObjectAssertion<T> Could<T>(this T value,
return new ObjectAssertion<T>(value, valueName, AssertionType.Could,
new CallerInfo(memberName, filePath, lineNumber));
}

/// <summary>
/// The must assertion with null check
/// </summary>
/// <param name="value"></param>
/// <param name="valueName"></param>
/// <param name="memberName"></param>
/// <param name="filePath"></param>
/// <param name="lineNumber"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static AndConstraint<T> MustNotNull<T>(
[NotNull] this T value,
[CallerArgumentExpression(nameof(value))]
string valueName = "",
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0)
where T : class

{
ThrowIfInvalid(value);
return new(new ObjectAssertion<T>(
value.ThrowIfNull(valueName),
valueName,
AssertionType.Must,
new CallerInfo(memberName, filePath, lineNumber)));
}


/// <summary>
/// The must assertion with null check
/// </summary>
/// <param name="value"></param>
/// <param name="valueName"></param>
/// <param name="memberName"></param>
/// <param name="filePath"></param>
/// <param name="lineNumber"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static AndConstraint<T> MustNotNull<T>(
[NotNull] this T? value,
[CallerArgumentExpression(nameof(value))]
string valueName = "",
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0)
where T : struct
{
ThrowIfInvalid(value);
return new(new ObjectAssertion<T>(
value.ThrowIfNull(valueName),
valueName,
AssertionType.Must,
new CallerInfo(memberName, filePath, lineNumber)));
}
}
41 changes: 41 additions & 0 deletions src/libraries/TedToolkit.Assertions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace TedToolkit.Assertions;

/// <summary>
/// Some exceptions extensions.
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// Throw ArgumentNullException if obj is null.
/// </summary>
/// <param name="obj">The object</param>
/// <param name="objName">the name of obj</param>
/// <exception cref="ArgumentNullException">exceptions</exception>
[return: NotNull]
public static T ThrowIfNull<T>(
[NotNull] this T obj,
[CallerArgumentExpression(nameof(obj))]
string objName = "")
where T : class
{
return obj ?? throw new ArgumentNullException(objName);
}

/// <summary>
/// Throw ArgumentNullException if obj is null.
/// </summary>
/// <param name="obj">The object</param>
/// <param name="objName">the name of obj</param>
/// <exception cref="ArgumentNullException">exceptions</exception>
public static T ThrowIfNull<T>(
[NotNull] this T? obj,
[CallerArgumentExpression(nameof(obj))]
string objName = "")
where T : struct
{
return obj ?? throw new ArgumentNullException(objName);
}
}