Skip to content

Commit

Permalink
feat: add resource converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism committed Sep 2, 2024
1 parent 739b9fc commit ee9820f
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Irihi.Avalonia.Shared.Public/Converters/ResourceConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Globalization;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data.Converters;

namespace Irihi.Avalonia.Shared.Converters;

public class ResourceConverter : ResourceDictionary, IValueConverter
{
public virtual object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is null) return AvaloniaProperty.UnsetValue;
return TryGetResource(value, null, out var resource) ? resource : AvaloniaProperty.UnsetValue;
}

public virtual object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Contracts\IOuterContentControl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Contracts\IPopupInnerContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Contracts\IPopupOuterContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\ResourceConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindingExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LogicalHelpers.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Globalization;
using Avalonia;
using Avalonia.Media;
using Irihi.Avalonia.Shared.Converters;
using Irihi.Avalonia.Shared.UnitTest.Mocks;

// Ensure this namespace matches your actual namespace
namespace Irihi.Avalonia.Shared.UnitTest.Converters;

public class ResourceConverterTests
{
private readonly ResourceConverter _converter = new();

[Fact]
public void Convert_WithNullValue_ReturnsAvaloniaPropertyUnsetValue()
{
// Arrange
object? value = null;
var targetType = typeof(object); // Or a specific type if applicable
object? parameter = null;
var culture = CultureInfo.InvariantCulture;

// Act
var result = _converter.Convert(value, targetType, parameter, culture);

// Assert
Assert.Equal(AvaloniaProperty.UnsetValue, result);
}

[Fact]
public void Convert_WithExistingResource_ReturnsResource()
{
// Arrange
// Assuming you would add a known resource to your converter for testing purposes
_converter["TestResource"] = "TestValue";
object value = "TestResource";
var targetType = typeof(string); // Assuming the target type you expect
object? parameter = null;
var culture = CultureInfo.InvariantCulture;

// Act
var result = _converter.Convert(value, targetType, parameter, culture);

// Assert
Assert.Equal("TestValue", result);
}

[Fact]
public void Convert_WithNonExistentResource_ReturnsAvaloniaPropertyUnsetValue()
{
// Arrange
object value = "NonExistentResource";
var targetType = typeof(string); // Or the expected type
object? parameter = null;
var culture = CultureInfo.InvariantCulture;

// Act
var result = _converter.Convert(value, targetType, parameter, culture);

// Assert
Assert.Equal(AvaloniaProperty.UnsetValue, result);
}

// Note: The ConvertBack method is not tested since it's not implemented.
// If it gets implemented in the future, similar test cases should be written for it.

[Fact]
public void ConvertBack_ThrowsNotImplementedException()
{
// Arrange
object? value = null;
var targetType = typeof(object); // Or a specific type if applicable
object? parameter = null;
var culture = CultureInfo.InvariantCulture;

// Act & Assert
Assert.Throws<NotImplementedException>(() => _converter.ConvertBack(value, targetType, parameter, culture));
}

[Fact]
public void ConvertFromDerivedClass_Success()
{
// Arrange
var derivedConverter = new MockResourceConverter();
object value = "Red";
var targetType = typeof(IBrush); // Assuming the target type you expect
object? parameter = null;
var culture = CultureInfo.InvariantCulture;

// Act
var result = derivedConverter.Convert(value, targetType, parameter, culture);

// Assert
Assert.Equal(Brushes.Red.Color, (result as ISolidColorBrush)?.Color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Common\IRIHI_CommandBaseGenericTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Common\IRIHI_CommandBaseTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Common\IRIHI_ObservableBaseTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\ResourceConverterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GlobalUsings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AffectsPseudoClassTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtensionTest.cs" />
Expand All @@ -21,7 +22,11 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ObservableExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\PropertyPseudoClassHelperTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RoutedEventExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Mocks\MockResourceConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Shapes\PureCircleTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Shapes\PureRingTest.cs" />
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)Mocks\MockResourceConverter.axaml" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<ResourceDictionary
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="https://github.com/avaloniaui"
x:Class="Irihi.Avalonia.Shared.UnitTest.Mocks.MockResourceConverter">
<SolidColorBrush x:Key="Red" Color="Red"/>
<SolidColorBrush x:Key="Green" Color="Green"/>
<SolidColorBrush x:Key="Blue" Color="Blue"/>
<SolidColorBrush x:Key="Yellow" Color="Yellow"/>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Irihi.Avalonia.Shared.Converters;

namespace Irihi.Avalonia.Shared.UnitTest.Mocks;

public class MockResourceConverter: ResourceConverter
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand All @@ -24,6 +25,10 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Irihi.Avalonia.Shared\Irihi.Avalonia.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<AvaloniaResource Include="..\Irihi.Avalonia.Shared.UnitTest.Public\Mocks\MockResourceConverter.axaml" />
</ItemGroup>

<Import Project="..\Irihi.Avalonia.Shared.UnitTest.Public\Irihi.Avalonia.Shared.UnitTest.Public.projitems" Label="Shared" />

Expand Down

0 comments on commit ee9820f

Please sign in to comment.