diff --git a/src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs b/src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs index eb926d81..303783d4 100644 --- a/src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs +++ b/src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs @@ -5,7 +5,6 @@ namespace MADE.Data.Converters { using System; - using MADE.Data.Converters.Exceptions; using MADE.Data.Converters.Strings; using Windows.UI.Xaml; using Windows.UI.Xaml.Data; @@ -13,7 +12,8 @@ namespace MADE.Data.Converters /// /// Defines a Windows components for a XAML value converter from to . /// - public class BooleanToStringValueConverter : DependencyObject, IValueConverter, IValueConverter + [Obsolete("BooleanToStringValueConverter for Windows will be removed in a future major release. Use the replicated BooleanToStringValueConverter type from the MADE.UI.Data.Converters library instead.")] + public partial class BooleanToStringValueConverter : DependencyObject, IValueConverter, IValueConverter { /// /// Defines the dependency property for . @@ -87,50 +87,6 @@ public object ConvertBack(object value, Type targetType, object parameter, strin return this.ConvertBack(b, parameter); } - - /// - /// Converts the value to the type. - /// - /// - /// The value to convert. - /// - /// - /// The optional parameter used to help with conversion. - /// - /// - /// The converted object. - /// - public string Convert(bool value, object parameter = default) - { - return value ? this.TrueValue : this.FalseValue; - } - - /// - /// Converts the value back to the type. - /// - /// - /// The value to convert. - /// - /// - /// The optional parameter used to help with conversion. - /// - /// - /// The converted object. - /// - public bool ConvertBack(string value, object parameter = default) - { - if (value == this.TrueValue) - { - return true; - } - - if (value == this.FalseValue) - { - return false; - } - - throw new InvalidDataConversionException(nameof(BooleanToStringValueConverter), value, $"The value to convert back is not of the expected {nameof(this.TrueValue)} or {nameof(this.FalseValue)}"); - } } } #endif \ No newline at end of file diff --git a/src/MADE.Data.Converters/BooleanToStringValueConverter.cs b/src/MADE.Data.Converters/BooleanToStringValueConverter.cs new file mode 100644 index 00000000..147e140a --- /dev/null +++ b/src/MADE.Data.Converters/BooleanToStringValueConverter.cs @@ -0,0 +1,67 @@ +namespace MADE.Data.Converters +{ + using MADE.Data.Converters.Exceptions; + using MADE.Data.Converters.Extensions; + + /// + /// Defines a value converter from to with a pre-determined and . + /// + public partial class BooleanToStringValueConverter : IValueConverter + { +#if !WINDOWS_UWP + /// + /// Gets or sets the positive/true value. + /// + public string TrueValue { get; set; } + + /// + /// Gets or sets the negative/false value. + /// + public string FalseValue { get; set; } +#endif + + /// + /// Converts the value to the type. + /// + /// + /// The value to convert. + /// + /// + /// The optional parameter used to help with conversion. + /// + /// + /// The converted object. + /// + public string Convert(bool value, object parameter = default) + { + return value.ToFormattedString(this.TrueValue, this.FalseValue); + } + + /// + /// Converts the value back to the type. + /// + /// + /// The value to convert. + /// + /// + /// The optional parameter used to help with conversion. + /// + /// + /// The converted object. + /// + public bool ConvertBack(string value, object parameter = default) + { + if (value == this.TrueValue) + { + return true; + } + + if (value == this.FalseValue) + { + return false; + } + + throw new InvalidDataConversionException(nameof(BooleanToStringValueConverter), value, $"The value to convert back is not of the expected {nameof(this.TrueValue)} or {nameof(this.FalseValue)}"); + } + } +} diff --git a/src/MADE.Data.Converters/DateTimeToStringValueConverter.Windows.cs b/src/MADE.Data.Converters/DateTimeToStringValueConverter.Windows.cs index 27af2690..2f3ac5f4 100644 --- a/src/MADE.Data.Converters/DateTimeToStringValueConverter.Windows.cs +++ b/src/MADE.Data.Converters/DateTimeToStringValueConverter.Windows.cs @@ -10,6 +10,7 @@ namespace MADE.Data.Converters /// /// Defines a Windows components for a XAML value converter from to with an optional format string. /// + [Obsolete("DateTimeToStringValueConverter for Windows will be removed in a future major release. Use the replicated DateTimeToStringValueConverter type from the MADE.UI.Data.Converters library instead.")] public partial class DateTimeToStringValueConverter : IValueConverter { /// diff --git a/src/MADE.Data.Converters/Extensions/BooleanExtensions.cs b/src/MADE.Data.Converters/Extensions/BooleanExtensions.cs new file mode 100644 index 00000000..e9c04f89 --- /dev/null +++ b/src/MADE.Data.Converters/Extensions/BooleanExtensions.cs @@ -0,0 +1,40 @@ +// MADE Apps licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace MADE.Data.Converters.Extensions +{ + /// + /// Defines a collection of extensions for values. + /// + public static class BooleanExtensions + { + /// + /// Converts a value to a value with optional true/false values. + /// + /// The value to format. + /// The format for when the is true. + /// The format for when the is false. + /// A formatted string + public static string ToFormattedString(this bool value, string trueValue = "True", string falseValue = "False") + { + return value ? trueValue : falseValue; + } + + /// + /// Converts a nullable value to a value with optional true/false/null values. + /// + /// The value to format. + /// The format for when the is true. Default, True. + /// The format for when the is false. Default, False. + /// The format for when the is null. Default, Not set. + /// A formatted string + public static string ToFormattedString( + this bool? value, + string trueValue = "True", + string falseValue = "False", + string nullValue = "Not set") + { + return value.HasValue ? value.Value ? trueValue : falseValue : nullValue; + } + } +} \ No newline at end of file diff --git a/tests/MADE.Data.Converters.Tests/Tests/BooleanExtensionsTests.cs b/tests/MADE.Data.Converters.Tests/Tests/BooleanExtensionsTests.cs new file mode 100644 index 00000000..c2d26776 --- /dev/null +++ b/tests/MADE.Data.Converters.Tests/Tests/BooleanExtensionsTests.cs @@ -0,0 +1,88 @@ +namespace MADE.Data.Converters.Tests.Tests +{ + using System.Diagnostics.CodeAnalysis; + using MADE.Data.Converters.Extensions; + using NUnit.Framework; + using Shouldly; + + [ExcludeFromCodeCoverage] + [TestFixture] + public class BooleanExtensionsTests + { + public class WhenConvertingBooleanToFormattedString + { + [Test] + public void ShouldReturnTrueValueIfTrue() + { + // Arrange + const bool boolean = true; + const string expected = "Yes"; + + // Act + string formatted = boolean.ToFormattedString(expected, "No"); + + // Assert + formatted.ShouldBe(expected); + } + + [Test] + public void ShouldReturnFalseValueIfFalse() + { + // Arrange + const bool boolean = false; + const string expected = "No"; + + // Act + string formatted = boolean.ToFormattedString("Yes", expected); + + // Assert + formatted.ShouldBe(expected); + } + } + + public class WhenConvertingNullableBooleanToFormattedString + { + [Test] + public void ShouldReturnTrueValueIfTrue() + { + // Arrange + bool? boolean = true; + const string expected = "Yes"; + + // Act + string formatted = boolean.ToFormattedString(expected, "No", "N/A"); + + // Assert + formatted.ShouldBe(expected); + } + + [Test] + public void ShouldReturnFalseValueIfFalse() + { + // Arrange + bool? boolean = false; + const string expected = "No"; + + // Act + string formatted = boolean.ToFormattedString("Yes", expected, "N/A"); + + // Assert + formatted.ShouldBe(expected); + } + + [Test] + public void ShouldReturnNullValueIfNull() + { + // Arrange + bool? boolean = null; + const string expected = "N/A"; + + // Act + string formatted = boolean.ToFormattedString("Yes", "No", expected); + + // Assert + formatted.ShouldBe(expected); + } + } + } +} \ No newline at end of file diff --git a/tests/MADE.Data.Converters.Tests/Tests/BooleanToStringValueConverterTests.cs b/tests/MADE.Data.Converters.Tests/Tests/BooleanToStringValueConverterTests.cs new file mode 100644 index 00000000..4d298df8 --- /dev/null +++ b/tests/MADE.Data.Converters.Tests/Tests/BooleanToStringValueConverterTests.cs @@ -0,0 +1,94 @@ +namespace MADE.Data.Converters.Tests.Tests +{ + using System.Diagnostics.CodeAnalysis; + using MADE.Data.Converters.Exceptions; + using NUnit.Framework; + + using Shouldly; + + [ExcludeFromCodeCoverage] + [TestFixture] + public class BooleanToStringValueConverterTests + { + public class WhenConverting + { + [Test] + public void ShouldConvertToTrueValueWhenTrue() + { + // Arrange + const bool boolean = true; + const string expected = "Yes"; + + var converter = new BooleanToStringValueConverter {TrueValue = expected, FalseValue = "No"}; + + // Act + string converted = converter.Convert(boolean); + + // Assert + converted.ShouldBe(expected); + } + + [Test] + public void ShouldConvertToFalseValueWhenFalse() + { + const bool boolean = false; + const string expected = "No"; + + var converter = new BooleanToStringValueConverter {TrueValue = "Yes", FalseValue = expected}; + + // Act + string converted = converter.Convert(boolean); + + // Assert + converted.ShouldBe(expected); + } + } + + public class WhenConvertingBack + { + [Test] + public void ShouldConvertToTrueWhenTrueValue() + { + // Arrange + const string booleanString = "Yes"; + const bool expected = true; + + var converter = new BooleanToStringValueConverter {TrueValue = booleanString, FalseValue = "No"}; + + // Act + bool converted = converter.ConvertBack(booleanString); + + // Assert + converted.ShouldBe(expected); + } + + [Test] + public void ShouldConvertToFalseWhenFalseValue() + { + // Arrange + const string booleanString = "No"; + const bool expected = false; + + var converter = new BooleanToStringValueConverter {TrueValue = "Yes", FalseValue = booleanString}; + + // Act + bool converted = converter.ConvertBack(booleanString); + + // Assert + converted.ShouldBe(expected); + } + + [Test] + public void ShouldThrowInvalidDataConversionExceptionIfNotTrueOrFalseValue() + { + // Arrange + const string booleanString = "Not valid"; + + var converter = new BooleanToStringValueConverter {TrueValue = "Yes", FalseValue = "No"}; + + // Act & Assert + Should.Throw(() => converter.ConvertBack(booleanString)); + } + } + } +} \ No newline at end of file