Skip to content

Commit

Permalink
feat: move to shared project. add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism committed Apr 11, 2024
1 parent c7a7019 commit 62bb16b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class LogicalHelpers
{
public static int CalculateDistanceFromLogicalParent<T>(this ILogical logical, int @default = -1) where T: ILogical
{
int distance = @default;
int distance = 0;
ILogical? parent = logical;
while (parent is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Contracts\IPopupOuterContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindingExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LogicalHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ObservableExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RoutedEventExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Reactive\ReadonlyDisposableCollection.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Avalonia.Controls;
using Irihi.Avalonia.Shared.Helpers;

namespace Irihi.Avalonia.Shared.UnitTest.Helpers;

public class LogicalHelpersTest
{
[Fact]
public void CalculateDistanceFromLogicalParent_Null()
{
Assert.Equal(-1, (null as Button)!.CalculateDistanceFromLogicalParent<StackPanel>());
}

[Fact]
public void CalculateDistanceFromLogicalParent_Default()
{
var parent = new StackPanel();
var child = new Button();
parent.Children.Add(child);
Assert.Equal(1, child.CalculateDistanceFromLogicalParent<StackPanel>(-1));
Assert.Equal(-1, child.CalculateDistanceFromLogicalParent<Grid>(-1));
}

[Fact]
public void CalculateDistanceFromLogicalParent()
{
var parent = new StackPanel();
var child = new Grid();
var grandChild = new Button();
parent.Children.Add(child);
child.Children.Add(grandChild);
Assert.Equal(2, grandChild.CalculateDistanceFromLogicalParent<StackPanel>());
Assert.Equal(1, grandChild.CalculateDistanceFromLogicalParent<Grid>());
Assert.Equal(-1, grandChild.CalculateDistanceFromLogicalParent<Canvas>());
}

[Fact]
public void CalculateDistanceFromLogicalParent_Self()
{
var parent = new StackPanel();
var child = new Grid();
var grandChild = new Button();
parent.Children.Add(child);
child.Children.Add(grandChild);
Assert.Equal(2, grandChild.CalculateDistanceFromLogicalParent<StackPanel>());
Assert.Equal(1, grandChild.CalculateDistanceFromLogicalParent<Grid>());
Assert.Equal(0, grandChild.CalculateDistanceFromLogicalParent<Button>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AffectsPseudoClassTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindingExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LogicalHelpersTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ObservableExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\PropertyPseudoClassHelperTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RoutedEventExtensionTest.cs" />
Expand Down
18 changes: 0 additions & 18 deletions test/Irihi.Avalonia.Shared.UnitTest/Helpers/LogicalHelpersTest.cs

This file was deleted.

0 comments on commit 62bb16b

Please sign in to comment.