-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/Irihi.Avalonia.Shared.Public/Converters/ResourceConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
test/Irihi.Avalonia.Shared.UnitTest.Public/Converters/ResourceConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/Irihi.Avalonia.Shared.UnitTest.Public/Mocks/MockResourceConverter.axaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
8 changes: 8 additions & 0 deletions
8
test/Irihi.Avalonia.Shared.UnitTest.Public/Mocks/MockResourceConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters