Skip to content

Commit

Permalink
Stripped Async suffix to generated command names
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Mar 31, 2021
1 parent 9d4725f commit 682179b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
28 changes: 25 additions & 3 deletions Microsoft.Toolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ private static void OnExecute(
private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(GeneratorExecutionContext context, SyntaxTriviaList leadingTrivia, IMethodSymbol methodSymbol)
{
// Get the command member names
string
propertyName = methodSymbol.Name + "Command",
fieldName = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1)}";
var (fieldName, propertyName) = GetGeneratedFieldAndPropertyNames(context, methodSymbol);

// Get the command type symbols
if (!TryMapCommandTypesFromMethod(
Expand Down Expand Up @@ -190,6 +188,30 @@ private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(Generat
return new MemberDeclarationSyntax[] { fieldDeclaration, propertyDeclaration };
}

/// <summary>
/// Get the generated field and property names for the input method.
/// </summary>
/// <param name="context">The input <see cref="GeneratorExecutionContext"/> instance to use.</param>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <returns>The generated field and property names for <paramref name="methodSymbol"/>.</returns>
[Pure]
private static (string FieldName, string PropertyName) GetGeneratedFieldAndPropertyNames(GeneratorExecutionContext context, IMethodSymbol methodSymbol)
{
string propertyName = methodSymbol.Name;

if (SymbolEqualityComparer.Default.Equals(methodSymbol.ReturnType, context.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")!) &&
methodSymbol.Name.EndsWith("Async"))
{
propertyName = propertyName.Substring(0, propertyName.Length - "Async".Length);
}

propertyName += "Command";

string fieldName = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1)}";

return (fieldName, propertyName);
}

/// <summary>
/// Gets the type symbols for the input method, if supported.
/// </summary>
Expand Down
34 changes: 17 additions & 17 deletions UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class Test_ICommandAttribute
{
[TestCategory("Mvvm")]
[TestMethod]
public void Test_ICommandAttribute_RelayCommand()
public async Task Test_ICommandAttribute_RelayCommand()
{
var model = new MyViewModel();

Expand All @@ -30,19 +30,19 @@ public void Test_ICommandAttribute_RelayCommand()

Assert.AreEqual(model.Counter, 6);

model.IncrementCounterAsyncCommand.Execute(null);
await model.DelayAndIncrementCounterCommand.ExecuteAsync(null);

Assert.AreEqual(model.Counter, 7);

model.IncrementCounterWithTokenAsyncCommand.Execute(null);
await model.DelayAndIncrementCounterWithTokenCommand.ExecuteAsync(null);

Assert.AreEqual(model.Counter, 8);

model.IncrementCounterWithValueAsyncCommand.Execute(5);
await model.DelayAndIncrementCounterWithValueCommand.ExecuteAsync(5);

Assert.AreEqual(model.Counter, 13);

model.IncrementCounterWithValueAndTokenAsyncCommand.Execute(5);
await model.DelayAndIncrementCounterWithValueAndTokenCommand.ExecuteAsync(5);

Assert.AreEqual(model.Counter, 18);
}
Expand All @@ -64,35 +64,35 @@ private void IncrementCounterWithValue(int count)
}

[ICommand]
private Task IncrementCounterAsync()
private async Task DelayAndIncrementCounterAsync()
{
Counter += 1;
await Task.Delay(50);

return Task.CompletedTask;
Counter += 1;
}

[ICommand]
private Task IncrementCounterWithTokenAsync(CancellationToken token)
private async Task DelayAndIncrementCounterWithTokenAsync(CancellationToken token)
{
Counter += 1;
await Task.Delay(50);

return Task.CompletedTask;
Counter += 1;
}

[ICommand]
private Task IncrementCounterWithValueAsync(int count)
private async Task DelayAndIncrementCounterWithValueAsync(int count)
{
Counter += count;
await Task.Delay(50);

return Task.CompletedTask;
Counter += count;
}

[ICommand]
private Task IncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
private async Task DelayAndIncrementCounterWithValueAndTokenAsync(int count, CancellationToken token)
{
Counter += count;
await Task.Delay(50);

return Task.CompletedTask;
Counter += count;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public void Test_AlsoNotifyForAttribute_Events()
{
var model = new DependentPropertyModel();

(PropertyChangedEventArgs, int) changed = default;
List<string> propertyNames = new();

model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
Expand Down

0 comments on commit 682179b

Please sign in to comment.