Skip to content

Commit 6e26fd2

Browse files
Added tests
1 parent f361a9c commit 6e26fd2

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using McMaster.Extensions.CommandLineUtils;
6+
using McMaster.Extensions.CommandLineUtils.Abstractions;
7+
using McMaster.Extensions.CommandLineUtils.Conventions;
8+
using McMaster.Extensions.Hosting.CommandLine.Tests.Utilities;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Hosting;
11+
using Xunit;
12+
using Xunit.Abstractions;
13+
14+
namespace McMaster.Extensions.Hosting.CommandLine.Tests
15+
{
16+
public class CustomValueParserTests
17+
{
18+
private const string DemoOptionValue = "{\"Value\": \"TheValue\"}";
19+
20+
private readonly ITestOutputHelper _output;
21+
22+
public CustomValueParserTests(ITestOutputHelper output)
23+
{
24+
_output = output;
25+
}
26+
27+
[Fact]
28+
public async Task ItParsesUsingCustomParserFromConfigAction()
29+
{
30+
var exitCode = await new HostBuilder()
31+
.ConfigureServices(collection => collection.AddSingleton<IConsole>(new TestConsole(_output)))
32+
.RunCommandLineApplicationAsync<CustomOptionTypeCommand>(
33+
new[] {"--custom-type", DemoOptionValue},
34+
app => app.ValueParsers.AddOrReplace(
35+
new CustomValueParser()));
36+
Assert.Equal(0, exitCode);
37+
}
38+
39+
[Fact]
40+
public async Task ItParsesUsingCustomParserFromInjectedConvention()
41+
{
42+
var exitCode = await new HostBuilder()
43+
.ConfigureServices(collection =>
44+
{
45+
collection.AddSingleton<IConsole>(new TestConsole(_output));
46+
collection.AddSingleton<IConvention, CustomValueParserConvention>();
47+
})
48+
.RunCommandLineApplicationAsync<CustomOptionTypeCommand>(
49+
new[] {"--custom-type", DemoOptionValue});
50+
Assert.Equal(0, exitCode);
51+
}
52+
53+
[Fact]
54+
public async Task ItParsesUsingCustomParserFromAttribute()
55+
{
56+
var exitCode = await new HostBuilder()
57+
.ConfigureServices(collection => collection.AddSingleton<IConsole>(new TestConsole(_output)))
58+
.RunCommandLineApplicationAsync<CustomOptionTypeCommandWithAttribute>(
59+
new[] {"--custom-type", DemoOptionValue});
60+
Assert.Equal(0, exitCode);
61+
}
62+
63+
class CustomType
64+
{
65+
public string Value { get; set; }
66+
}
67+
68+
class CustomValueParser : IValueParser<CustomType>
69+
{
70+
public Type TargetType => typeof(CustomType);
71+
72+
public CustomType Parse(string? argName, string? value, CultureInfo culture)
73+
{
74+
return JsonSerializer.Deserialize<CustomType>(value);
75+
}
76+
77+
object? IValueParser.Parse(string? argName, string? value, CultureInfo culture)
78+
{
79+
return Parse(argName, value, culture);
80+
}
81+
}
82+
83+
[Command]
84+
class CustomOptionTypeCommand
85+
{
86+
[Option("--custom-type", CommandOptionType.SingleValue)]
87+
public CustomType Option { get; set; }
88+
89+
private int OnExecute()
90+
{
91+
if (Option == null)
92+
{
93+
return 1;
94+
}
95+
96+
if (!"TheValue".Equals(Option.Value, StringComparison.Ordinal))
97+
{
98+
return 2;
99+
}
100+
101+
return 0;
102+
}
103+
}
104+
105+
class CustomValueParserConvention : IConvention
106+
{
107+
public void Apply(ConventionContext context)
108+
{
109+
context.Application.ValueParsers.AddOrReplace(new CustomValueParser());
110+
}
111+
}
112+
113+
[AttributeUsage(AttributeTargets.Class)]
114+
class CustomValueParserConventionAttribute : Attribute, IConvention
115+
{
116+
public void Apply(ConventionContext context)
117+
{
118+
context.Application.ValueParsers.AddOrReplace(new CustomValueParser());
119+
}
120+
}
121+
122+
[Command]
123+
[CustomValueParserConvention]
124+
class CustomOptionTypeCommandWithAttribute
125+
{
126+
[Option("--custom-type", CommandOptionType.SingleValue)]
127+
public CustomType Option { get; set; }
128+
129+
private int OnExecute()
130+
{
131+
if (Option == null)
132+
{
133+
return 1;
134+
}
135+
136+
if (!"TheValue".Equals(Option.Value, StringComparison.Ordinal))
137+
{
138+
return 2;
139+
}
140+
141+
return 0;
142+
}
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)