Skip to content

Commit 7d6e18f

Browse files
committed
Added boolean to string format extension methods
1 parent 39975a8 commit 7d6e18f

File tree

4 files changed

+224
-1
lines changed

4 files changed

+224
-1
lines changed

src/MADE.Data.Converters/BooleanToStringValueConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MADE.Data.Converters
22
{
33
using MADE.Data.Converters.Exceptions;
4+
using MADE.Data.Converters.Extensions;
45

56
/// <summary>
67
/// Defines a value converter from <see cref="bool"/> to <see cref="string"/> with a pre-determined <see cref="TrueValue"/> and <see cref="FalseValue"/>.
@@ -33,7 +34,7 @@ public partial class BooleanToStringValueConverter : IValueConverter<bool, strin
3334
/// </returns>
3435
public string Convert(bool value, object parameter = default)
3536
{
36-
return value ? this.TrueValue : this.FalseValue;
37+
return value.ToFormattedString(this.TrueValue, this.FalseValue);
3738
}
3839

3940
/// <summary>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// MADE Apps licenses this file to you under the MIT license.
2+
// See the LICENSE file in the project root for more information.
3+
4+
namespace MADE.Data.Converters.Extensions
5+
{
6+
/// <summary>
7+
/// Defines a collection of extensions for <see cref="bool"/> values.
8+
/// </summary>
9+
public static class BooleanExtensions
10+
{
11+
/// <summary>
12+
/// Converts a <see cref="bool"/> value to a <see cref="string"/> value with optional true/false values.
13+
/// </summary>
14+
/// <param name="value">The <see cref="bool"/> value to format.</param>
15+
/// <param name="trueValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>true</c>.</param>
16+
/// <param name="falseValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>false</c>.</param>
17+
/// <returns>A formatted string</returns>
18+
public static string ToFormattedString(this bool value, string trueValue = "True", string falseValue = "False")
19+
{
20+
return value ? trueValue : falseValue;
21+
}
22+
23+
/// <summary>
24+
/// Converts a nullable <see cref="bool"/> value to a <see cref="string"/> value with optional true/false/null values.
25+
/// </summary>
26+
/// <param name="value">The <see cref="bool"/> value to format.</param>
27+
/// <param name="trueValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>true</c>. Default, True.</param>
28+
/// <param name="falseValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>false</c>. Default, False.</param>
29+
/// <param name="nullValue">The <see cref="string"/> format for when the <paramref name="value"/> is <c>null</c>. Default, Not set.</param>
30+
/// <returns>A formatted string</returns>
31+
public static string ToFormattedString(
32+
this bool? value,
33+
string trueValue = "True",
34+
string falseValue = "False",
35+
string nullValue = "Not set")
36+
{
37+
return value.HasValue ? value.Value ? trueValue : falseValue : nullValue;
38+
}
39+
}
40+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
namespace MADE.Data.Converters.Tests.Tests
2+
{
3+
using System.Diagnostics.CodeAnalysis;
4+
using MADE.Data.Converters.Extensions;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
8+
[ExcludeFromCodeCoverage]
9+
[TestFixture]
10+
public class BooleanExtensionsTests
11+
{
12+
public class WhenConvertingBooleanToFormattedString
13+
{
14+
[Test]
15+
public void ShouldReturnTrueValueIfTrue()
16+
{
17+
// Arrange
18+
const bool boolean = true;
19+
const string expected = "Yes";
20+
21+
// Act
22+
string formatted = boolean.ToFormattedString(expected, "No");
23+
24+
// Assert
25+
formatted.ShouldBe(expected);
26+
}
27+
28+
[Test]
29+
public void ShouldReturnFalseValueIfFalse()
30+
{
31+
// Arrange
32+
const bool boolean = false;
33+
const string expected = "No";
34+
35+
// Act
36+
string formatted = boolean.ToFormattedString("Yes", expected);
37+
38+
// Assert
39+
formatted.ShouldBe(expected);
40+
}
41+
}
42+
43+
public class WhenConvertingNullableBooleanToFormattedString
44+
{
45+
[Test]
46+
public void ShouldReturnTrueValueIfTrue()
47+
{
48+
// Arrange
49+
bool? boolean = true;
50+
const string expected = "Yes";
51+
52+
// Act
53+
string formatted = boolean.ToFormattedString(expected, "No", "N/A");
54+
55+
// Assert
56+
formatted.ShouldBe(expected);
57+
}
58+
59+
[Test]
60+
public void ShouldReturnFalseValueIfFalse()
61+
{
62+
// Arrange
63+
bool? boolean = false;
64+
const string expected = "No";
65+
66+
// Act
67+
string formatted = boolean.ToFormattedString("Yes", expected, "N/A");
68+
69+
// Assert
70+
formatted.ShouldBe(expected);
71+
}
72+
73+
[Test]
74+
public void ShouldReturnNullValueIfNull()
75+
{
76+
// Arrange
77+
bool? boolean = null;
78+
const string expected = "N/A";
79+
80+
// Act
81+
string formatted = boolean.ToFormattedString("Yes", "No", expected);
82+
83+
// Assert
84+
formatted.ShouldBe(expected);
85+
}
86+
}
87+
}
88+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
namespace MADE.Data.Converters.Tests.Tests
2+
{
3+
using System.Diagnostics.CodeAnalysis;
4+
using MADE.Data.Converters.Exceptions;
5+
using NUnit.Framework;
6+
7+
using Shouldly;
8+
9+
[ExcludeFromCodeCoverage]
10+
[TestFixture]
11+
public class BooleanToStringValueConverterTests
12+
{
13+
public class WhenConverting
14+
{
15+
[Test]
16+
public void ShouldConvertToTrueValueWhenTrue()
17+
{
18+
// Arrange
19+
const bool boolean = true;
20+
const string expected = "Yes";
21+
22+
var converter = new BooleanToStringValueConverter {TrueValue = expected, FalseValue = "No"};
23+
24+
// Act
25+
string converted = converter.Convert(boolean);
26+
27+
// Assert
28+
converted.ShouldBe(expected);
29+
}
30+
31+
[Test]
32+
public void ShouldConvertToFalseValueWhenFalse()
33+
{
34+
const bool boolean = false;
35+
const string expected = "No";
36+
37+
var converter = new BooleanToStringValueConverter {TrueValue = "Yes", FalseValue = expected};
38+
39+
// Act
40+
string converted = converter.Convert(boolean);
41+
42+
// Assert
43+
converted.ShouldBe(expected);
44+
}
45+
}
46+
47+
public class WhenConvertingBack
48+
{
49+
[Test]
50+
public void ShouldConvertToTrueWhenTrueValue()
51+
{
52+
// Arrange
53+
const string booleanString = "Yes";
54+
const bool expected = true;
55+
56+
var converter = new BooleanToStringValueConverter {TrueValue = booleanString, FalseValue = "No"};
57+
58+
// Act
59+
bool converted = converter.ConvertBack(booleanString);
60+
61+
// Assert
62+
converted.ShouldBe(expected);
63+
}
64+
65+
[Test]
66+
public void ShouldConvertToFalseWhenFalseValue()
67+
{
68+
// Arrange
69+
const string booleanString = "No";
70+
const bool expected = false;
71+
72+
var converter = new BooleanToStringValueConverter {TrueValue = "Yes", FalseValue = booleanString};
73+
74+
// Act
75+
bool converted = converter.ConvertBack(booleanString);
76+
77+
// Assert
78+
converted.ShouldBe(expected);
79+
}
80+
81+
[Test]
82+
public void ShouldThrowInvalidDataConversionExceptionIfNotTrueOrFalseValue()
83+
{
84+
// Arrange
85+
const string booleanString = "Not valid";
86+
87+
var converter = new BooleanToStringValueConverter {TrueValue = "Yes", FalseValue = "No"};
88+
89+
// Act & Assert
90+
Should.Throw<InvalidDataConversionException>(() => converter.ConvertBack(booleanString));
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)