Skip to content

Commit 3f879e1

Browse files
authored
Merge pull request #250 from MADE-Apps/features/new-converters
Alteration to mark Windows specifics as Obsolete
2 parents aee8e4a + 7d6e18f commit 3f879e1

File tree

6 files changed

+292
-46
lines changed

6 files changed

+292
-46
lines changed

src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace MADE.Data.Converters
66
{
77
using System;
8-
using MADE.Data.Converters.Exceptions;
98
using MADE.Data.Converters.Strings;
109
using Windows.UI.Xaml;
1110
using Windows.UI.Xaml.Data;
1211

1312
/// <summary>
1413
/// Defines a Windows components for a XAML value converter from <see cref="bool"/> to <see cref="string"/>.
1514
/// </summary>
16-
public class BooleanToStringValueConverter : DependencyObject, IValueConverter, IValueConverter<bool, string>
15+
[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.")]
16+
public partial class BooleanToStringValueConverter : DependencyObject, IValueConverter, IValueConverter<bool, string>
1717
{
1818
/// <summary>
1919
/// Defines the dependency property for <see cref="TrueValue"/>.
@@ -87,50 +87,6 @@ public object ConvertBack(object value, Type targetType, object parameter, strin
8787

8888
return this.ConvertBack(b, parameter);
8989
}
90-
91-
/// <summary>
92-
/// Converts the <paramref name="value">value</paramref> to the <see cref="string"/> type.
93-
/// </summary>
94-
/// <param name="value">
95-
/// The value to convert.
96-
/// </param>
97-
/// <param name="parameter">
98-
/// The optional parameter used to help with conversion.
99-
/// </param>
100-
/// <returns>
101-
/// The converted <see cref="string"/> object.
102-
/// </returns>
103-
public string Convert(bool value, object parameter = default)
104-
{
105-
return value ? this.TrueValue : this.FalseValue;
106-
}
107-
108-
/// <summary>
109-
/// Converts the <paramref name="value">value</paramref> back to the <see cref="bool"/> type.
110-
/// </summary>
111-
/// <param name="value">
112-
/// The value to convert.
113-
/// </param>
114-
/// <param name="parameter">
115-
/// The optional parameter used to help with conversion.
116-
/// </param>
117-
/// <returns>
118-
/// The converted <see cref="bool"/> object.
119-
/// </returns>
120-
public bool ConvertBack(string value, object parameter = default)
121-
{
122-
if (value == this.TrueValue)
123-
{
124-
return true;
125-
}
126-
127-
if (value == this.FalseValue)
128-
{
129-
return false;
130-
}
131-
132-
throw new InvalidDataConversionException(nameof(BooleanToStringValueConverter), value, $"The value to convert back is not of the expected {nameof(this.TrueValue)} or {nameof(this.FalseValue)}");
133-
}
13490
}
13591
}
13692
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace MADE.Data.Converters
2+
{
3+
using MADE.Data.Converters.Exceptions;
4+
using MADE.Data.Converters.Extensions;
5+
6+
/// <summary>
7+
/// Defines a value converter from <see cref="bool"/> to <see cref="string"/> with a pre-determined <see cref="TrueValue"/> and <see cref="FalseValue"/>.
8+
/// </summary>
9+
public partial class BooleanToStringValueConverter : IValueConverter<bool, string>
10+
{
11+
#if !WINDOWS_UWP
12+
/// <summary>
13+
/// Gets or sets the positive/true value.
14+
/// </summary>
15+
public string TrueValue { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the negative/false value.
19+
/// </summary>
20+
public string FalseValue { get; set; }
21+
#endif
22+
23+
/// <summary>
24+
/// Converts the <paramref name="value">value</paramref> to the <see cref="string"/> type.
25+
/// </summary>
26+
/// <param name="value">
27+
/// The value to convert.
28+
/// </param>
29+
/// <param name="parameter">
30+
/// The optional parameter used to help with conversion.
31+
/// </param>
32+
/// <returns>
33+
/// The converted <see cref="string"/> object.
34+
/// </returns>
35+
public string Convert(bool value, object parameter = default)
36+
{
37+
return value.ToFormattedString(this.TrueValue, this.FalseValue);
38+
}
39+
40+
/// <summary>
41+
/// Converts the <paramref name="value">value</paramref> back to the <see cref="bool"/> type.
42+
/// </summary>
43+
/// <param name="value">
44+
/// The value to convert.
45+
/// </param>
46+
/// <param name="parameter">
47+
/// The optional parameter used to help with conversion.
48+
/// </param>
49+
/// <returns>
50+
/// The converted <see cref="bool"/> object.
51+
/// </returns>
52+
public bool ConvertBack(string value, object parameter = default)
53+
{
54+
if (value == this.TrueValue)
55+
{
56+
return true;
57+
}
58+
59+
if (value == this.FalseValue)
60+
{
61+
return false;
62+
}
63+
64+
throw new InvalidDataConversionException(nameof(BooleanToStringValueConverter), value, $"The value to convert back is not of the expected {nameof(this.TrueValue)} or {nameof(this.FalseValue)}");
65+
}
66+
}
67+
}

src/MADE.Data.Converters/DateTimeToStringValueConverter.Windows.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace MADE.Data.Converters
1010
/// <summary>
1111
/// Defines a Windows components for a XAML value converter from <see cref="DateTime"/> to <see cref="string"/> with an optional format string.
1212
/// </summary>
13+
[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.")]
1314
public partial class DateTimeToStringValueConverter : IValueConverter
1415
{
1516
/// <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)