Skip to content

Updating tests to use TUnit and MTP #3838

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions Directory.packages.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project>
<ItemGroup>
<PackageVersion Include="BluwolfIcons" Version="1.0.1" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageVersion Include="Dragablz" Version="0.0.3.234" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageVersion Include="Humanizer" Version="2.14.1" />
<PackageVersion Include="MahApps.Metro" Version="2.4.10" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
Expand All @@ -24,11 +24,10 @@
<PackageVersion Include="ShowMeTheXAML.MSBuild" Version="2.0.0" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Private.Uri" Version="4.3.2" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="TUnit" Version="0.19.86" />
<PackageVersion Include="VirtualizingWrapPanel" Version="1.5.8" />
<PackageVersion Include="XAMLTest" Version="1.2.2" />
<PackageVersion Include="xunit" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageVersion Include="Xunit.StaFact" Version="1.1.11" />
</ItemGroup>
</Project>
18 changes: 9 additions & 9 deletions src/MaterialDesignColors.Wpf/SwatchesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace MaterialDesignColors;
/// </summary>
public class SwatchesProvider
{
private static readonly object _syncLock = new();
/// <summary>
/// Generates an instance reading swatches from the provided assembly, allowing
/// colours outside of the standard material palette to be loaded provided the are stored in the expected XAML format.
Expand Down Expand Up @@ -39,12 +40,7 @@ public SwatchesProvider(Assembly assembly)
Read(assemblyName, x.SingleOrDefault(y => y.match.Groups["type"].Value == "primary")?.key),
Read(assemblyName, x.SingleOrDefault(y => y.match.Groups["type"].Value == "secondary")?.key)
))
.ToList() ??
#if NETCOREAPP3_1_OR_GREATER
(IEnumerable<Swatch>)Array.Empty<Swatch>();
#else
(IEnumerable<Swatch>)new Swatch[0];
#endif
.ToList() ?? [];
}

/// <summary>
Expand Down Expand Up @@ -98,8 +94,12 @@ static Hue GetHue(ResourceDictionary dictionary, DictionaryEntry entry)
if (assemblyName is null || path is null)
return null;

return (ResourceDictionary)Application.LoadComponent(new Uri(
$"/{assemblyName};component/{path.Replace(".baml", ".xaml")}",
UriKind.RelativeOrAbsolute));
lock (_syncLock)
{
//NB: Application.LoadComponent is not thread safe
return (ResourceDictionary)Application.LoadComponent(new Uri(
$"/{assemblyName};component/{path.Replace(".baml", ".xaml")}",
UriKind.RelativeOrAbsolute));
}
}
}
30 changes: 15 additions & 15 deletions src/MaterialDesignThemes.Wpf/RatingBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ internal class TextBlockForegroundConverter : IMultiValueConverter

public static TextBlockForegroundConverter Instance { get; } = new();

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values?.Length == 5
&& values[0] is SolidColorBrush brush
Expand Down Expand Up @@ -416,23 +416,23 @@ GradientStopCollection CreateGradientStopCollection(Color originalColor, Color s
if (invertDirection)
{
return new()
{
new GradientStop {Color = semiTransparent, Offset = offset},
new GradientStop {Color = originalColor, Offset = offset},
};
}
return new()
{
new GradientStop {Color = semiTransparent, Offset = offset},
new GradientStop {Color = originalColor, Offset = offset},
new GradientStop {Color = semiTransparent, Offset = offset}
};
}
return new()
{
new GradientStop {Color = originalColor, Offset = offset},
new GradientStop {Color = semiTransparent, Offset = offset}
};
}

// This should never happen (returning actual brush to avoid the compilers squiggly line warning)
return Brushes.Transparent;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}

internal class PreviewIndicatorTransformXConverter : IMultiValueConverter
Expand All @@ -441,9 +441,9 @@ internal class PreviewIndicatorTransformXConverter : IMultiValueConverter

internal static double Margin => 2.0;

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values.Length >= 7
if (values?.Length >= 7
&& values[0] is double ratingBarButtonActualWidth
&& values[1] is double previewValueActualWidth
&& values[2] is Orientation ratingBarOrientation
Expand Down Expand Up @@ -482,7 +482,7 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
return 1.0;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}

internal class PreviewIndicatorTransformYConverter : IMultiValueConverter
Expand All @@ -491,9 +491,9 @@ internal class PreviewIndicatorTransformYConverter : IMultiValueConverter

internal static double Margin => 2.0;

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values.Length >= 7
if (values?.Length >= 7
&& values[0] is double ratingBarButtonActualHeight
&& values[1] is double previewValueActualHeight
&& values[2] is Orientation ratingBarOrientation
Expand Down Expand Up @@ -531,6 +531,6 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
return 1.0;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by mdresgen.
// This code was generated by MaterialDesignToolkit.ResourceGeneration.
// </auto-generated>
//------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion src/MaterialDesignThemes.Wpf/Theme.g.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by mdresgen.
// This code was generated by MaterialDesignToolkit.ResourceGeneration.
// </auto-generated>
//------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion src/MaterialDesignThemes.Wpf/ThemeExtensions.g.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by mdresgen.
// This code was generated by MaterialDesignToolkit.ResourceGeneration.
// </auto-generated>
//------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion src/MaterialDesignToolkit.ResourceGeneration/Brushes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ void WriteBrush(Brush brush, string name)
{{indent}}{{indent}}{
{{indent}}{{indent}}{{indent}}IVisualElement<TextBlock> textBlock = await panel.GetElement<TextBlock>("[Text=\"{{name}}\"]");
{{indent}}{{indent}}{{indent}}Color? textBlockBackground = await textBlock.GetBackgroundColor();
{{indent}}{{indent}}{{indent}}Assert.Equal(await GetResourceColor("{{brush.Name}}"), textBlockBackground);
{{indent}}{{indent}}{{indent}}await Assert.That(textBlockBackground).IsEqualTo(await GetResourceColor("{{brush.Name}}"));
{{indent}}{{indent}}}
""");
}
Expand Down
9 changes: 4 additions & 5 deletions tests/MaterialDesignColors.Wpf.Tests/ColorAssistTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System.Windows.Media;
using MaterialDesignColors.ColorManipulation;
using Xunit;

namespace MaterialDesignColors.Wpf.Tests;

public class ColorAssistTests
{
[Fact]
public void EnsureContrastRatio_AdjustsColor()
[Test]
public async Task EnsureContrastRatio_AdjustsColor()
{
var background = Color.FromRgb(0xFA, 0xFA, 0xFA);
var foreground = Color.FromRgb(0xFF, 0xC1, 0x07);

var adjusted = foreground.EnsureContrastRatio(background, 3.0f);

double contrastRatio = adjusted.ContrastRatio(background);
Assert.True(contrastRatio >= 2.9);
Assert.True(contrastRatio <= 3.1);
await Assert.That(contrastRatio).IsGreaterThanOrEqualTo(2.9);
await Assert.That(contrastRatio).IsLessThanOrEqualTo(3.1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<TargetFrameworks>net472;net6.0-windows;net8.0-windows</TargetFrameworks>
<AssemblyTitle>MaterialDesignColors.Wpf.Tests</AssemblyTitle>
<Product>MaterialDesignColors.Wpf.Tests</Product>
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net472'">
<Reference Include="PresentationCore" />
Expand All @@ -25,9 +28,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Shouldly" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="System.Private.Uri" />
<PackageReference Include="System.Text.RegularExpressions" />
<PackageReference Include="TUnit" />
</ItemGroup>
</Project>
41 changes: 19 additions & 22 deletions tests/MaterialDesignColors.Wpf.Tests/ResourceProviderFixture.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,62 @@
using Shouldly;
using Xunit;

namespace MaterialDesignColors.Wpf.Fixture;
namespace MaterialDesignColors.Wpf.Tests;

public class ResourceProviderFixture
{
[Fact]
public void ExcludesBlack()
[Test]
public async Task ExcludesBlack()
{
SwatchesProvider swatchesProvider = new ();

bool containsBlack = swatchesProvider.Swatches.Any(
swatch => string.Compare(swatch.Name, "Black", StringComparison.InvariantCultureIgnoreCase) == 0);

containsBlack.ShouldBe(false);
await Assert.That(containsBlack).IsFalse();
}

[Fact]
public void IncludesGrey()
[Test]
public async Task IncludesGrey()
{
SwatchesProvider swatchesProvider = new ();

bool containsBlack = swatchesProvider.Swatches.Any(
swatch => string.Compare(swatch.Name, "Grey", StringComparison.InvariantCultureIgnoreCase) == 0);

containsBlack.ShouldBe(true);
await Assert.That(containsBlack).IsTrue();
}

[Fact]
public void BrownHasNoSecondary()
[Test]
public async Task BrownHasNoSecondary()
{
SwatchesProvider swatchesProvider = new ();

var brownSwatch = swatchesProvider.Swatches.Single(
swatch => swatch.Name == "brown");

brownSwatch.SecondaryHues.ShouldNotBeNull();
brownSwatch.SecondaryHues.Count.ShouldBe(0);
await Assert.That(brownSwatch.SecondaryHues).IsNotNull();
await Assert.That(brownSwatch.SecondaryHues.Count).IsEqualTo(0);
}

[Fact]
public void BrownHasPrimaries()
[Test]
public async Task BrownHasPrimaries()
{
SwatchesProvider swatchesProvider = new ();

var brownSwatch = swatchesProvider.Swatches.Single(
swatch => swatch.Name == "brown");

brownSwatch.PrimaryHues.ShouldNotBeNull();
brownSwatch.PrimaryHues.Count.ShouldBe(10);
await Assert.That(brownSwatch.PrimaryHues).IsNotNull();
await Assert.That(brownSwatch.PrimaryHues.Count).IsEqualTo(10);
}

[Fact]
public void IndigoHasSecondaries()
[Test]
public async Task IndigoHasSecondaries()
{
SwatchesProvider swatchesProvider = new ();

var brownSwatch = swatchesProvider.Swatches.Single(
swatch => swatch.Name == "indigo");

brownSwatch.SecondaryHues.ShouldNotBeNull();
brownSwatch.SecondaryHues.Count.ShouldBe(4);
await Assert.That(brownSwatch.SecondaryHues).IsNotNull();
await Assert.That(brownSwatch.SecondaryHues.Count).IsEqualTo(4);
}
}
Loading
Loading