-
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.
* feat: add command and observable base and tests. * feat: remove warnings. * feat: implement INPChanging.
- Loading branch information
Showing
16 changed files
with
429 additions
and
26 deletions.
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
src/Irihi.Avalonia.Shared.Public/Common/IRIHI_CommandBase.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,42 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Windows.Input; | ||
|
||
namespace Irihi.Avalonia.Shared.Common; | ||
|
||
/// <summary> | ||
/// This is a ICommand implementation for internal use, to avoid irrelevant dependencies. | ||
/// This should not be used by your application. | ||
/// </summary> | ||
public class IRIHI_CommandBase: ICommand | ||
{ | ||
private readonly Action _execute; | ||
private readonly Func<bool>? _canExecute; | ||
public event EventHandler? CanExecuteChanged; | ||
|
||
public IRIHI_CommandBase(Action execute) | ||
{ | ||
_execute = execute; | ||
} | ||
|
||
public IRIHI_CommandBase(Action execute, Func<bool> canExecute) | ||
{ | ||
_execute = execute; | ||
_canExecute = canExecute; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool CanExecute(object? parameter) | ||
{ | ||
return _canExecute is null || _canExecute.Invoke(); | ||
} | ||
|
||
public void Execute(object? parameter) | ||
{ | ||
_execute.Invoke(); | ||
} | ||
|
||
public void NotifyCanExecuteChanged() | ||
{ | ||
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Irihi.Avalonia.Shared.Public/Common/IRIHI_CommandBase`T.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,81 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Windows.Input; | ||
|
||
namespace Irihi.Avalonia.Shared.Common; | ||
|
||
/// <summary> | ||
/// This is a ICommand implementation for internal use, to avoid irrelevant dependencies. | ||
/// This should not be used by your application. | ||
/// </summary> | ||
public class IRIHI_CommandBase<T>: ICommand | ||
{ | ||
private readonly Action<T?> _execute; | ||
private readonly Predicate<T?>? _canExecute; | ||
public event EventHandler? CanExecuteChanged; | ||
|
||
public IRIHI_CommandBase(Action<T?> execute) | ||
{ | ||
_execute = execute; | ||
} | ||
|
||
public IRIHI_CommandBase(Action<T?> execute, Predicate<T?> canExecute) | ||
{ | ||
_execute = execute; | ||
_canExecute = canExecute; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool CanExecute(T? parameter) | ||
{ | ||
return _canExecute is null || _canExecute.Invoke(parameter); | ||
} | ||
|
||
public bool CanExecute(object? parameter) | ||
{ | ||
if (!TryGetCommandArgument(parameter, out var result)) | ||
{ | ||
throw new ArgumentException(); | ||
} | ||
return CanExecute(result); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Execute(T? parameter) | ||
{ | ||
_execute(parameter); | ||
} | ||
|
||
public void Execute(object? parameter) | ||
{ | ||
if (!TryGetCommandArgument(parameter, out var result)) | ||
{ | ||
throw new ArgumentException(); | ||
} | ||
Execute(result); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static bool TryGetCommandArgument(object? parameter, out T? result) | ||
{ | ||
if (parameter is null && default(T) is null) | ||
{ | ||
result = default; | ||
return true; | ||
} | ||
if (parameter is T obj) | ||
{ | ||
result = obj; | ||
return true; | ||
} | ||
result = default; | ||
return false; | ||
} | ||
|
||
public void NotifyCanExecuteChanged() | ||
{ | ||
EventHandler? canExecuteChanged = this.CanExecuteChanged; | ||
if (canExecuteChanged is null) | ||
return; | ||
canExecuteChanged(this, EventArgs.Empty); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Irihi.Avalonia.Shared.Public/Common/IRIHI_ObservableBase.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,45 @@ | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Irihi.Avalonia.Shared.Common; | ||
|
||
/// <summary> | ||
/// This is a INotifyPropertyChanged and INotifyPropertyChanging implementation for internal use, to avoid irrelevant | ||
/// dependencies. | ||
/// This should not be used by your application. | ||
/// </summary> | ||
public class IRIHI_ObservableBase : INotifyPropertyChanged, INotifyPropertyChanging | ||
{ | ||
public event PropertyChangedEventHandler? PropertyChanged; | ||
public event PropertyChangingEventHandler? PropertyChanging; | ||
|
||
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) | ||
{ | ||
PropertyChanged?.Invoke(this, e); | ||
} | ||
|
||
protected virtual void OnPropertyChanging(PropertyChangingEventArgs e) | ||
{ | ||
PropertyChanging?.Invoke(this, e); | ||
} | ||
|
||
protected void OnPropertyChanging([CallerMemberName] string? propertyName = null) | ||
{ | ||
OnPropertyChanging(new PropertyChangingEventArgs(propertyName)); | ||
} | ||
|
||
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) | ||
{ | ||
OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); | ||
} | ||
|
||
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string? propertyName = null) | ||
{ | ||
if (EqualityComparer<T>.Default.Equals(field, newValue)) | ||
return false; | ||
OnPropertyChanging(propertyName); | ||
field = newValue; | ||
OnPropertyChanged(propertyName); | ||
return true; | ||
} | ||
} |
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
4 changes: 1 addition & 3 deletions
4
src/Irihi.Avalonia.Shared.Public/Helpers/ObservableExtension.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
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
113 changes: 113 additions & 0 deletions
113
test/Irihi.Avalonia.Shared.UnitTest.Public/Common/IRIHI_CommandBaseGenericTests.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,113 @@ | ||
namespace Irihi.Avalonia.Shared.Common.Tests; | ||
|
||
public class IRIHI_CommandBaseGenericTests | ||
{ | ||
[Fact] | ||
public void ExecuteInvokesActionWithParameter() | ||
{ | ||
int executedParam = 0; | ||
var command = new IRIHI_CommandBase<int>(param => executedParam = param); | ||
|
||
command.Execute(5); | ||
|
||
Assert.Equal(5, executedParam); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteReturnsTrueByDefault() | ||
{ | ||
var command = new IRIHI_CommandBase<object>(_ => { }); | ||
|
||
bool canExecute = command.CanExecute(null); | ||
|
||
Assert.True(canExecute); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteReturnsFalseWhenPredicateSpecifiedAndReturnsFalse() | ||
{ | ||
var command = new IRIHI_CommandBase<int>(_ => { }, _ => false); | ||
|
||
bool canExecute = command.CanExecute(0); | ||
|
||
Assert.False(canExecute); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteThrowsArgumentExceptionForInvalidParameterType() | ||
{ | ||
var command = new IRIHI_CommandBase<int>(_ => { }); | ||
|
||
Assert.Throws<ArgumentException>(() => command.CanExecute("invalid")); | ||
} | ||
|
||
[Fact] | ||
public void ExecuteThrowsArgumentExceptionForInvalidParameterType() | ||
{ | ||
var command = new IRIHI_CommandBase<int>(_ => { }); | ||
|
||
Assert.Throws<ArgumentException>(() => command.Execute("invalid")); | ||
} | ||
|
||
[Fact] | ||
public void NotifyCanExecuteChangedRaisesEvent() | ||
{ | ||
var command = new IRIHI_CommandBase<object>(_ => { }); | ||
bool eventRaised = false; | ||
command.CanExecuteChanged += (_, _) => eventRaised = true; | ||
|
||
command.NotifyCanExecuteChanged(); | ||
|
||
Assert.True(eventRaised); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteWithValidParameterTypeReturnsTrue() | ||
{ | ||
var command = new IRIHI_CommandBase<int>(_ => { }, _ => true); | ||
bool canExecute = command.CanExecute(1); | ||
Assert.True(canExecute); | ||
} | ||
|
||
[Fact] | ||
public void ExecuteWithValidParameterInvokesAction() | ||
{ | ||
int executedParam = 0; | ||
var command = new IRIHI_CommandBase<int>(param => executedParam = param); | ||
command.Execute(10); | ||
Assert.Equal(10, executedParam); | ||
} | ||
|
||
[Fact] | ||
public void UnsubscribingFromCanExecuteChangedEventWorks() | ||
{ | ||
var command = new IRIHI_CommandBase<object>(_ => { }); | ||
bool eventRaised = false; | ||
|
||
EventHandler handler = (_, _) => eventRaised = true; | ||
command.CanExecuteChanged += handler; | ||
command.CanExecuteChanged -= handler; | ||
|
||
command.NotifyCanExecuteChanged(); | ||
Assert.False(eventRaised); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteWithRightTypeReturnsTrue() | ||
{ | ||
object o = 1; | ||
var command = new IRIHI_CommandBase<int>(_ => { }); | ||
bool canExecute = command.CanExecute(o); | ||
Assert.True(canExecute); | ||
} | ||
|
||
[Fact] | ||
public void ExecuteWithRightTypeInvokesAction() | ||
{ | ||
object o = 1; | ||
int executedParam = 0; | ||
var command = new IRIHI_CommandBase<int>(param => executedParam = param); | ||
command.Execute(o); | ||
Assert.Equal(1, executedParam); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
test/Irihi.Avalonia.Shared.UnitTest.Public/Common/IRIHI_CommandBaseTests.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,47 @@ | ||
namespace Irihi.Avalonia.Shared.Common.Tests; | ||
|
||
public class IRIHI_CommandBaseTests | ||
{ | ||
[Fact] | ||
public void ExecuteInvokesAction() | ||
{ | ||
bool executed = false; | ||
var command = new IRIHI_CommandBase(() => executed = true); | ||
|
||
command.Execute(null); | ||
|
||
Assert.True(executed); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteReturnsTrueByDefault() | ||
{ | ||
var command = new IRIHI_CommandBase(() => { }); | ||
|
||
bool canExecute = command.CanExecute(null); | ||
|
||
Assert.True(canExecute); | ||
} | ||
|
||
[Fact] | ||
public void CanExecuteReturnsFalseWhenSpecified() | ||
{ | ||
var command = new IRIHI_CommandBase(() => { }, () => false); | ||
|
||
bool canExecute = command.CanExecute(null); | ||
|
||
Assert.False(canExecute); | ||
} | ||
|
||
[Fact] | ||
public void NotifyCanExecuteChangedRaisesEvent() | ||
{ | ||
var command = new IRIHI_CommandBase(() => { }); | ||
bool eventRaised = false; | ||
command.CanExecuteChanged += (_, _) => eventRaised = true; | ||
|
||
command.NotifyCanExecuteChanged(); | ||
|
||
Assert.True(eventRaised); | ||
} | ||
} |
Oops, something went wrong.