Skip to content

Commit

Permalink
Merge pull request #250 from MADE-Apps/features/new-converters
Browse files Browse the repository at this point in the history
Alteration to mark Windows specifics as Obsolete
  • Loading branch information
tom-made authored May 24, 2022
2 parents aee8e4a + 7d6e18f commit 3f879e1
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 46 deletions.
48 changes: 2 additions & 46 deletions src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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;

/// <summary>
/// Defines a Windows components for a XAML value converter from <see cref="bool"/> to <see cref="string"/>.
/// </summary>
public class BooleanToStringValueConverter : DependencyObject, IValueConverter, IValueConverter<bool, string>
[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<bool, string>
{
/// <summary>
/// Defines the dependency property for <see cref="TrueValue"/>.
Expand Down Expand Up @@ -87,50 +87,6 @@ public object ConvertBack(object value, Type targetType, object parameter, strin

return this.ConvertBack(b, parameter);
}

/// <summary>
/// Converts the <paramref name="value">value</paramref> to the <see cref="string"/> type.
/// </summary>
/// <param name="value">
/// The value to convert.
/// </param>
/// <param name="parameter">
/// The optional parameter used to help with conversion.
/// </param>
/// <returns>
/// The converted <see cref="string"/> object.
/// </returns>
public string Convert(bool value, object parameter = default)
{
return value ? this.TrueValue : this.FalseValue;
}

/// <summary>
/// Converts the <paramref name="value">value</paramref> back to the <see cref="bool"/> type.
/// </summary>
/// <param name="value">
/// The value to convert.
/// </param>
/// <param name="parameter">
/// The optional parameter used to help with conversion.
/// </param>
/// <returns>
/// The converted <see cref="bool"/> object.
/// </returns>
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
67 changes: 67 additions & 0 deletions src/MADE.Data.Converters/BooleanToStringValueConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace MADE.Data.Converters
{
using MADE.Data.Converters.Exceptions;
using MADE.Data.Converters.Extensions;

/// <summary>
/// Defines a value converter from <see cref="bool"/> to <see cref="string"/> with a pre-determined <see cref="TrueValue"/> and <see cref="FalseValue"/>.
/// </summary>
public partial class BooleanToStringValueConverter : IValueConverter<bool, string>
{
#if !WINDOWS_UWP
/// <summary>
/// Gets or sets the positive/true value.
/// </summary>
public string TrueValue { get; set; }

/// <summary>
/// Gets or sets the negative/false value.
/// </summary>
public string FalseValue { get; set; }
#endif

/// <summary>
/// Converts the <paramref name="value">value</paramref> to the <see cref="string"/> type.
/// </summary>
/// <param name="value">
/// The value to convert.
/// </param>
/// <param name="parameter">
/// The optional parameter used to help with conversion.
/// </param>
/// <returns>
/// The converted <see cref="string"/> object.
/// </returns>
public string Convert(bool value, object parameter = default)
{
return value.ToFormattedString(this.TrueValue, this.FalseValue);
}

/// <summary>
/// Converts the <paramref name="value">value</paramref> back to the <see cref="bool"/> type.
/// </summary>
/// <param name="value">
/// The value to convert.
/// </param>
/// <param name="parameter">
/// The optional parameter used to help with conversion.
/// </param>
/// <returns>
/// The converted <see cref="bool"/> object.
/// </returns>
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)}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace MADE.Data.Converters
/// <summary>
/// Defines a Windows components for a XAML value converter from <see cref="DateTime"/> to <see cref="string"/> with an optional format string.
/// </summary>
[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
{
/// <summary>
Expand Down
40 changes: 40 additions & 0 deletions src/MADE.Data.Converters/Extensions/BooleanExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Defines a collection of extensions for <see cref="bool"/> values.
/// </summary>
public static class BooleanExtensions
{
/// <summary>
/// Converts a <see cref="bool"/> value to a <see cref="string"/> value with optional true/false values.
/// </summary>
/// <param name="value">The <see cref="bool"/> value to format.</param>
/// <param name="trueValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>true</c>.</param>
/// <param name="falseValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>false</c>.</param>
/// <returns>A formatted string</returns>
public static string ToFormattedString(this bool value, string trueValue = "True", string falseValue = "False")
{
return value ? trueValue : falseValue;
}

/// <summary>
/// Converts a nullable <see cref="bool"/> value to a <see cref="string"/> value with optional true/false/null values.
/// </summary>
/// <param name="value">The <see cref="bool"/> value to format.</param>
/// <param name="trueValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>true</c>. Default, True.</param>
/// <param name="falseValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>false</c>. Default, False.</param>
/// <param name="nullValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>null</c>. Default, Not set.</param>
/// <returns>A formatted string</returns>
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;
}
}
}
88 changes: 88 additions & 0 deletions tests/MADE.Data.Converters.Tests/Tests/BooleanExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidDataConversionException>(() => converter.ConvertBack(booleanString));
}
}
}
}

0 comments on commit 3f879e1

Please sign in to comment.