-
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.
Merge pull request #50 from s2quake/fix/command-context-settings
Fix an issue with settings not being applied in CommandContext
- Loading branch information
Showing
5 changed files
with
116 additions
and
24 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
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
86 changes: 86 additions & 0 deletions
86
test/JSSoft.Commands.Tests/CommandContextTests/ServiceProviderTest.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,86 @@ | ||
// <copyright file="ServiceProviderTest.cs" company="JSSoft"> | ||
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE.md in the project root for license information. | ||
// </copyright> | ||
|
||
using System.ComponentModel; | ||
using System.Globalization; | ||
|
||
namespace JSSoft.Commands.Tests.CommandContextTests; | ||
|
||
public class ServiceProviderTest | ||
{ | ||
[Fact] | ||
public void Execute_Test() | ||
{ | ||
var testCommand = new TestCommand(); | ||
var commands = new ICommand[] | ||
{ | ||
testCommand, | ||
}; | ||
var settings = new CommandSettings | ||
{ | ||
ServiceProvider = new ServiceProvider(), | ||
}; | ||
var commandContext = new TestCommandContext(settings, commands); | ||
|
||
commandContext.Execute("test --data 10"); | ||
|
||
Assert.Equal(11, testCommand.Data.Value); | ||
} | ||
|
||
private sealed class TestCommand : CommandBase | ||
{ | ||
[CommandProperty] | ||
public Data Data { get; set; } | ||
|
||
protected override void OnExecute() | ||
{ | ||
// do nothing | ||
} | ||
} | ||
|
||
[TypeConverter(typeof(DataTypeConverter))] | ||
private struct Data | ||
{ | ||
public int Value { get; set; } | ||
} | ||
|
||
private sealed class DataTypeConverter : TypeConverter | ||
{ | ||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
{ | ||
return sourceType == typeof(string); | ||
} | ||
|
||
public override object? ConvertFrom( | ||
ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
{ | ||
if (value is string text) | ||
{ | ||
var i = int.Parse(text); | ||
if (context is not null && context.GetService(typeof(int)) is int sum) | ||
{ | ||
i += sum; | ||
} | ||
|
||
return new Data() { Value = i }; | ||
} | ||
|
||
return base.ConvertFrom(context, culture, value); | ||
} | ||
} | ||
|
||
private sealed class ServiceProvider : IServiceProvider | ||
{ | ||
public object? GetService(Type serviceType) | ||
{ | ||
if (serviceType == typeof(int)) | ||
{ | ||
return 1; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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