Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use recent language features on Themes.WPF Test Converters #3559

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Globalization;
using System.Windows.Media;
using MaterialDesignThemes.Wpf.Converters;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters;

public class BrushOpacityConverterTests
public sealed class BrushOpacityConverterTests
{
[Theory]
[InlineData("0.16", 0.16)]
Expand All @@ -14,7 +13,7 @@ public void AllCultureParseParameterCorrectly(object parameter, double expectedO
var converter = new BrushOpacityConverter();
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var inputBrush = new SolidColorBrush { Color = Colors.Red };
SolidColorBrush inputBrush = new () { Color = Colors.Red };
var brush = (SolidColorBrush?)converter.Convert(inputBrush, typeof(Brush), parameter, culture);
Assert.Equal(expectedOpacity, brush?.Opacity);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System.Globalization;
using MaterialDesignThemes.Wpf.Converters;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters;

public class DoubleToCornerRadiusConverterTests
public sealed class DoubleToCornerRadiusConverterTests
{
[Theory]
[InlineData(-0.16, 0.0)]
[InlineData(0.16, 0.16)]
[InlineData(5.0, 5.0)]
public void AllCultureParseParameterCorrectly(object parameter, double expectedCornerRadius)
{
var converter = new DoubleToCornerRadiusConverter();
DoubleToCornerRadiusConverter converter = new ();
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var cornerRadius = (CornerRadius?)converter.Convert(parameter, typeof(CornerRadius), parameter, culture);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Globalization;
using System.Windows.Media;
using MaterialDesignThemes.Wpf.Converters;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters;

Expand All @@ -10,24 +9,24 @@ public class FloatingHintTransformConverterTests
public static IEnumerable<object?[]> InvalidParameters =>
new[]
{
new object?[] {null, null, null, null},
new object?[] {1.0, null, null, null},
new object?[] {null, 1.0, null, null},
new object?[] {null, null, 1.0, null},
new object?[] {null, null, null, new Point()},
new object?[] {1.0, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue},
new object?[] {DependencyProperty.UnsetValue, 1.0, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue},
new object?[] {DependencyProperty.UnsetValue, DependencyProperty.UnsetValue, 1.0, DependencyProperty.UnsetValue},
[null, null, null, null],
[1.0, null, null, null],
[null, 1.0, null, null],
[null, null, 1.0, null],
[null, null, null, new Point()],
[1.0, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue],
[DependencyProperty.UnsetValue, 1.0, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue],
[DependencyProperty.UnsetValue, DependencyProperty.UnsetValue, 1.0, DependencyProperty.UnsetValue],
new object?[] {DependencyProperty.UnsetValue, DependencyProperty.UnsetValue, DependencyProperty.UnsetValue, new Point() },
};

[Theory]
[MemberData(nameof(InvalidParameters))]
public void WhenParametersAreNotSetItReturnsIdentity(object? scale, object? lower, object? upper, object? offset)
{
var converter = new FloatingHintTransformConverter();
FloatingHintTransformConverter converter = new ();

var result = converter.Convert(new[] { scale, lower, upper, offset },
object? result = converter.Convert([scale, lower, upper, offset],
typeof(Transform), null, CultureInfo.CurrentUICulture);

Assert.Equal(Transform.Identity, result);
Expand All @@ -38,9 +37,10 @@ public void WhenParametersAreNotSetItReturnsIdentity(object? scale, object? lowe
[InlineData(1.5, 2.0, 3.0, 2.0, -3.0)]
public void WhenParametersAreSpecifiedItReturnsTransforms(double scale, double lower, double upper, double x, double y)
{
var converter = new FloatingHintTransformConverter();
FloatingHintTransformConverter converter = new();

var result = (TransformGroup?)converter.Convert(new object?[] { scale, lower, upper, new Point(x, y), 0 }, typeof(Transform), null, CultureInfo.CurrentUICulture);
var result = (TransformGroup?)converter.Convert([scale, lower, upper, new Point(x, y), 0],
typeof(Transform), null, CultureInfo.CurrentUICulture);

Assert.NotNull(result);
var scaleTransform = (ScaleTransform)result!.Children[0];
Expand All @@ -58,9 +58,10 @@ public void WhenParametersAreSpecifiedItReturnsTransforms(double scale, double l
[InlineData(1.5, 2.0, 3.0, 2.0, 3.0)]
public void WhenParametersAreSpecifiedAndScaleTransformDisabledItReturnsTransforms(double scale, double lower, double upper, double x, double y)
{
var converter = new FloatingHintTransformConverter { ApplyScaleTransform = false };
FloatingHintTransformConverter converter = new () { ApplyScaleTransform = false };

var result = (TransformGroup?)converter.Convert(new object?[] { scale, lower, upper, new Point(x, y), 0 }, typeof(Transform), null, CultureInfo.CurrentUICulture);
var result = (TransformGroup?)converter.Convert([scale, lower, upper, new Point(x, y), 0],
typeof(Transform), null, CultureInfo.CurrentUICulture);

Assert.NotNull(result);
Assert.Single(result.Children);
Expand All @@ -72,9 +73,10 @@ public void WhenParametersAreSpecifiedAndScaleTransformDisabledItReturnsTransfor
[InlineData(1.5, 2.0, 3.0, 2.0, 3.0)]
public void WhenParametersAreSpecifiedAndTranslateTransformDisabledItReturnsTransforms(double scale, double lower, double upper, double x, double y)
{
var converter = new FloatingHintTransformConverter { ApplyTranslateTransform = false };
FloatingHintTransformConverter converter = new () { ApplyTranslateTransform = false };

var result = (TransformGroup?)converter.Convert(new object?[] { scale, lower, upper, new Point(x, y), 0 }, typeof(Transform), null, CultureInfo.CurrentUICulture);
var result = (TransformGroup?)converter.Convert([scale, lower, upper, new Point(x, y), 0],
typeof(Transform), null, CultureInfo.CurrentUICulture);

Assert.NotNull(result);
Assert.Single(result.Children);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Globalization;
using MaterialDesignThemes.Wpf.Converters;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters;

public class MathConverterTests
public sealed class MathConverterTests
{
[Theory]
[EnumData]
public void EnumValues_AreAllHandled(MathOperation operation)
{
var converter = new MathConverter
MathConverter converter = new ()
{
Operation = operation
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System.Globalization;
using MaterialDesignThemes.Wpf.Converters;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters;

public class MathMultipleConverterTests
public sealed class MathMultipleConverterTests
{
[Theory]
[EnumData]
public void EnumValues_AreAllHandled(MathOperation operation)
{
var converter = new MathMultipleConverter
MathMultipleConverter converter = new ()
{
Operation = operation
};

Assert.True(converter.Convert(new object?[] { 1.0, 1.0 }, null, null, CultureInfo.CurrentUICulture) is double);
Assert.True(converter.Convert([1.0, 1.0], null, null, CultureInfo.CurrentUICulture) is double);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Globalization;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters;

public class SliderToolTipConverterTests
public sealed class SliderToolTipConverterTests
{
[Theory]
[InlineData(1.4)]
Expand All @@ -12,25 +11,25 @@ public class SliderToolTipConverterTests
[InlineData(42)]
public void SliderConverterTest(object value)
{
Wpf.Converters.SliderToolTipConverter converter = new Wpf.Converters.SliderToolTipConverter();
Wpf.Converters.SliderToolTipConverter converter = new ();

//test a valid case
var result = converter.Convert(new object?[] { value, "Test String Format {0}" }, typeof(string), null, CultureInfo.CurrentCulture);
object? result = converter.Convert([value, "Test String Format {0}"], typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal($"Test String Format {value}", result);

//test too many placeholders in format string
result = converter.Convert(new object?[] { value, "{0} {1}" }, typeof(string), null, CultureInfo.CurrentCulture);
result = converter.Convert([value, "{0} {1}"], typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);

result = converter.Convert(new object?[] { value, "{0} {1} {2}" }, typeof(string), null, CultureInfo.CurrentCulture);
result = converter.Convert([value, "{0} {1} {2}"], typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);

//test empty format string
result = converter.Convert(new object?[] { value, "" }, typeof(string), null, CultureInfo.CurrentCulture);
result = converter.Convert([value, ""], typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);

//test null format string
result = converter.Convert(new object?[] { value, null }, typeof(string), null, CultureInfo.CurrentCulture);
result = converter.Convert([value, null], typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);
}
}
Loading