Skip to content
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

Fix infinite recursion error in CommandMapper.Add() #16264

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/Core/src/CommandMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public CommandMapper(CommandMapper chained)


public void Add(string key, Action<TViewHandler, TVirtualView> action) =>
Add(key, action);
SetPropertyCore(key, (h, v, _) => action?.Invoke((TViewHandler)h, (TVirtualView)v));

public void Add(string key, Action<TViewHandler, TVirtualView, object?> action) =>
SetPropertyCore(key, (h, v, o) => action?.Invoke((TViewHandler)h, (TVirtualView)v, o));
Expand Down
37 changes: 37 additions & 0 deletions src/Core/tests/UnitTests/CommandMapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.UnitTests;
using NSubstitute;
using Xunit;

namespace Microsoft.Maui.UnitTests
Expand Down Expand Up @@ -72,5 +74,40 @@ public void GenericMappersWorks()
Assert.False(wasMapper1Called);
Assert.True(wasMapper2Called);
}

[Fact]
public void AddCommandWithArgs()
{
var mapper = new CommandMapper<IView, IViewHandler>();

// Add a command mapping with a callback function that accepts an argument
const string key = "test_key";
var action = Substitute.For<Action<IViewHandler, IView, object>>();
mapper.Add(key, action);

// Invoke the command
const int arg = 42;
mapper[key].Invoke(null, null, arg);

// Verify that the callback function was invoked
action.Received().Invoke(Arg.Any<IViewHandler>(), Arg.Any<IView>(), arg);
}

[Fact]
public void AddCommandWithNoArgs()
{
var mapper = new CommandMapper<IView, IViewHandler>();

// Add a command mapping with a callback function that does NOT take an argument
const string key = "test_key";
var action = Substitute.For<Action<IViewHandler, IView>>();
mapper.Add(key, action);

// Invoke the command
mapper.GetCommand(key)?.Invoke(null, null, null);

// Verify that the callback function was invoked
action.Received().Invoke(Arg.Any<IViewHandler>(), Arg.Any<IView>());
}
}
}