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
66 changes: 66 additions & 0 deletions src/Microsoft.VisualStudio.Validation/Verify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -73,6 +75,70 @@ public static void Operation([DoesNotReturnIf(false)] bool condition, [Interpola
}
}

/// <inheritdoc cref="Operation(bool, ResourceManager, string, object?)"/>
/// <param name="condition"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='condition']"/></param>
/// <param name="resourceManager"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='resourceManager']"/></param>
/// <param name="resourceName"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='unformattedMessageResourceName']"/></param>
[DebuggerStepThrough]
public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string resourceName)
{
Requires.NotNull(resourceManager, nameof(resourceManager));
if (!condition)
{
throw new InvalidOperationException(resourceManager.GetString(resourceName, CultureInfo.CurrentCulture));
}
}

/// <inheritdoc cref="Operation(bool, ResourceManager, string, object?, object?)"/>
[DebuggerStepThrough]
public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string unformattedMessageResourceName, object? arg1)
{
Requires.NotNull(resourceManager, nameof(resourceManager));
if (!condition)
{
throw new InvalidOperationException(PrivateErrorHelpers.Format(resourceManager.GetString(unformattedMessageResourceName, CultureInfo.CurrentCulture)!, arg1));
}
}

/// <inheritdoc cref="Operation(bool, ResourceManager, string, object?[])"/>
/// <param name="condition"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='condition']"/></param>
/// <param name="resourceManager"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='resourceManager']"/></param>
/// <param name="unformattedMessageResourceName"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='unformattedMessageResourceName']"/></param>
/// <param name="arg1">The first formatting argument.</param>
/// <param name="arg2">The second formatting argument.</param>
[DebuggerStepThrough]
public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string unformattedMessageResourceName, object? arg1, object? arg2)
{
Requires.NotNull(resourceManager, nameof(resourceManager));
if (!condition)
{
throw new InvalidOperationException(PrivateErrorHelpers.Format(resourceManager.GetString(unformattedMessageResourceName, CultureInfo.CurrentCulture)!, arg1, arg2));
}
}

/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if a condition is false.
/// </summary>
/// <param name="condition">The condition to check.</param>
/// <param name="resourceManager">The resource manager from which to retrieve the exception message. For example: <c>Strings.ResourceManager</c>.</param>
/// <param name="unformattedMessageResourceName">The name of the string resource to obtain for the exception message. For example: <c>nameof(Strings.SomeError)</c>.</param>
/// <param name="args">The formatting arguments.</param>
/// <remarks>
/// This overload allows only loading a localized string in the error condition as an optimization in perf critical sections over the simpler
/// to use <see cref="Operation(bool, string?)"/> overload.
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="resourceManager"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> is <see langword="false"/>.</exception>
[DebuggerStepThrough]
public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string unformattedMessageResourceName, params object?[] args)
{
Requires.NotNull(resourceManager, nameof(resourceManager));
if (!condition)
{
throw new InvalidOperationException(PrivateErrorHelpers.Format(resourceManager.GetString(unformattedMessageResourceName, CultureInfo.CurrentCulture)!, args));
}
}

/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if a condition is false.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions test/Microsoft.VisualStudio.Validation.Tests/VerifyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.InteropServices;
using Microsoft;
using Microsoft.VisualStudio.Validation.Tests;
using Xunit;

public class VerifyTests
Expand Down Expand Up @@ -39,6 +40,23 @@ string FormattingMethod()
Assert.StartsWith("Some generated string method.", ex.Message);
}

[Fact]
public void Operation_ResourceManager()
{
AssertThrows(TestStrings.GetResourceString(TestStrings.SomeError), c => Verify.Operation(c, TestStrings.ResourceManager, TestStrings.SomeError));
AssertThrows(TestStrings.FormatSomeError1Arg("A"), c => Verify.Operation(c, TestStrings.ResourceManager, TestStrings.SomeError1Arg, "A"));
AssertThrows(TestStrings.FormatSomeError2Args("A", "B"), c => Verify.Operation(c, TestStrings.ResourceManager, TestStrings.SomeError2Args, "A", "B"));
AssertThrows(TestStrings.FormatSomeError3Args("A", "B", "C"), c => Verify.Operation(c, TestStrings.ResourceManager, TestStrings.SomeError3Args, "A", "B", "C"));

static void AssertThrows(string? expectedMessage, Action<bool> action)
{
action(true);

InvalidOperationException actual = Assert.Throws<InvalidOperationException>(() => action(false));
Assert.Equal(expectedMessage, actual.Message);
}
}

[Fact]
public void OperationWithHelp()
{
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "17.8",
"version": "17.12-beta",
"publicReleaseRefSpec": [
"^refs/heads/main$",
"^refs/heads/v\\d+(?:\\.\\d+)?$"
Expand Down