-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from MADE-Apps/features/new-converters
Alteration to mark Windows specifics as Obsolete
- Loading branch information
Showing
6 changed files
with
292 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
88
tests/MADE.Data.Converters.Tests/Tests/BooleanExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
tests/MADE.Data.Converters.Tests/Tests/BooleanToStringValueConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |