diff --git a/csharp/Platform.Ranges.Tests/EnsureExtensionsTests.cs b/csharp/Platform.Ranges.Tests/EnsureExtensionsTests.cs index f8e6de0..0ef5f33 100644 --- a/csharp/Platform.Ranges.Tests/EnsureExtensionsTests.cs +++ b/csharp/Platform.Ranges.Tests/EnsureExtensionsTests.cs @@ -1,6 +1,7 @@ using System; using Xunit; using Platform.Exceptions; +using Platform.Ranges.Resources; namespace Platform.Ranges.Tests { @@ -11,5 +12,27 @@ public static class EnsureExtensionsTests [Fact] public static void ArgumentInRangeExceptionTest() => Assert.Throws(() => Ensure.Always.ArgumentInRange(5, (6, 7))); + + [Fact] + public static void MaximumArgumentIsGreaterOrEqualToMinimumMessageTest() + { + var exception = Assert.Throws(() => Ensure.Always.MaximumArgumentIsGreaterOrEqualToMinimum(5, 3)); + Assert.Contains(ExceptionMessages.MaximumShouldBeGreaterOrEqualToMinimum, exception.Message); + } + + [Fact] + public static void ArgumentInRangeMessageTest() + { + var exception = Assert.Throws(() => Ensure.Always.ArgumentInRange(10, (1, 5), "testArgument")); + var expectedMessage = string.Format(ExceptionMessages.ArgumentOutOfRange, 10, new Range(1, 5)); + Assert.Contains(expectedMessage, exception.Message); + } + + [Fact] + public static void ExceptionMessagesResourcesTest() + { + Assert.Equal("Maximum should be greater or equal to minimum.", ExceptionMessages.MaximumShouldBeGreaterOrEqualToMinimum); + Assert.Equal("Argument value [{0}] is out of range {1}.", ExceptionMessages.ArgumentOutOfRange); + } } } diff --git a/csharp/Platform.Ranges/EnsureExtensions.cs b/csharp/Platform.Ranges/EnsureExtensions.cs index 1747d55..ff13a05 100644 --- a/csharp/Platform.Ranges/EnsureExtensions.cs +++ b/csharp/Platform.Ranges/EnsureExtensions.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using Platform.Exceptions; using Platform.Exceptions.ExtensionRoots; +using Platform.Ranges.Resources; #pragma warning disable IDE0060 // Remove unused parameter @@ -15,7 +16,7 @@ namespace Platform.Ranges /// public static class EnsureExtensions { - private const string DefaultMaximumShouldBeGreaterOrEqualToMinimumMessage = "Maximum should be greater or equal to minimum."; + private static string DefaultMaximumShouldBeGreaterOrEqualToMinimumMessage => ExceptionMessages.MaximumShouldBeGreaterOrEqualToMinimum; #region Always @@ -126,7 +127,7 @@ public static void ArgumentInRange(this EnsureAlwaysExtensionRoot roo [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ArgumentInRange(this EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range range, string argumentName) { - string messageBuilder() => $"Argument value [{argumentValue}] is out of range {range}."; + string messageBuilder() => string.Format(ExceptionMessages.ArgumentOutOfRange, argumentValue, range); ArgumentInRange(root, argumentValue, range, argumentName, messageBuilder); } diff --git a/csharp/Platform.Ranges/Platform.Ranges.csproj b/csharp/Platform.Ranges/Platform.Ranges.csproj index 6d7584f..253c58b 100644 --- a/csharp/Platform.Ranges/Platform.Ranges.csproj +++ b/csharp/Platform.Ranges/Platform.Ranges.csproj @@ -39,6 +39,13 @@ + + + ResXFileCodeGenerator + ExceptionMessages.Designer.cs + + + diff --git a/csharp/Platform.Ranges/Resources/ExceptionMessages.Designer.cs b/csharp/Platform.Ranges/Resources/ExceptionMessages.Designer.cs new file mode 100644 index 0000000..19c5af8 --- /dev/null +++ b/csharp/Platform.Ranges/Resources/ExceptionMessages.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace Platform.Ranges.Resources { + using System; + + + /// <summary> + /// A strongly-typed resource class, for looking up localized strings, etc. + /// </summary> + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class ExceptionMessages { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + public ExceptionMessages() { + } + + /// <summary> + /// Returns the cached ResourceManager instance used by this class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Platform.Ranges.Resources.ExceptionMessages", typeof(ExceptionMessages).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// <summary> + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// <summary> + /// Looks up a localized string similar to Argument value [{0}] is out of range {1}.. + /// </summary> + public static string ArgumentOutOfRange { + get { + return ResourceManager.GetString("ArgumentOutOfRange", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Maximum should be greater or equal to minimum.. + /// </summary> + public static string MaximumShouldBeGreaterOrEqualToMinimum { + get { + return ResourceManager.GetString("MaximumShouldBeGreaterOrEqualToMinimum", resourceCulture); + } + } + } +} \ No newline at end of file diff --git a/csharp/Platform.Ranges/Resources/ExceptionMessages.resx b/csharp/Platform.Ranges/Resources/ExceptionMessages.resx new file mode 100644 index 0000000..60430c9 --- /dev/null +++ b/csharp/Platform.Ranges/Resources/ExceptionMessages.resx @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Maximum should be greater or equal to minimum. + Error message when maximum value is less than minimum value in a range + + + Argument value [{0}] is out of range {1}. + Error message when an argument value is outside the allowed range. {0} is the value, {1} is the range + +