diff --git a/src/Microsoft.VisualStudio.Validation/Verify.cs b/src/Microsoft.VisualStudio.Validation/Verify.cs index 1d0584af..65a67d55 100644 --- a/src/Microsoft.VisualStudio.Validation/Verify.cs +++ b/src/Microsoft.VisualStudio.Validation/Verify.cs @@ -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; @@ -73,6 +75,70 @@ public static void Operation([DoesNotReturnIf(false)] bool condition, [Interpola } } + /// + /// + /// + /// + [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)); + } + } + + /// + [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)); + } + } + + /// + /// + /// + /// + /// The first formatting argument. + /// The second formatting argument. + [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)); + } + } + + /// + /// Throws an if a condition is false. + /// + /// The condition to check. + /// The resource manager from which to retrieve the exception message. For example: Strings.ResourceManager. + /// The name of the string resource to obtain for the exception message. For example: nameof(Strings.SomeError). + /// The formatting arguments. + /// + /// This overload allows only loading a localized string in the error condition as an optimization in perf critical sections over the simpler + /// to use overload. + /// + /// Thrown if is . + /// Thrown if is . + [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)); + } + } + /// /// Throws an if a condition is false. /// diff --git a/test/Microsoft.VisualStudio.Validation.Tests/VerifyTests.cs b/test/Microsoft.VisualStudio.Validation.Tests/VerifyTests.cs index 80980f8b..30c6823c 100644 --- a/test/Microsoft.VisualStudio.Validation.Tests/VerifyTests.cs +++ b/test/Microsoft.VisualStudio.Validation.Tests/VerifyTests.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using Microsoft; +using Microsoft.VisualStudio.Validation.Tests; using Xunit; public class VerifyTests @@ -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 action) + { + action(true); + + InvalidOperationException actual = Assert.Throws(() => action(false)); + Assert.Equal(expectedMessage, actual.Message); + } + } + [Fact] public void OperationWithHelp() { diff --git a/version.json b/version.json index 3fa718bf..975d440e 100644 --- a/version.json +++ b/version.json @@ -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+)?$"