Skip to content

Commit 5ba7d6a

Browse files
committed
Extended OptionStyle to include single valued properties which expect multiple arguments. Added more tests.
1 parent bc3dc14 commit 5ba7d6a

File tree

7 files changed

+432
-41
lines changed

7 files changed

+432
-41
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Binder.Tests;
5+
using FluentAssertions;
6+
using J4JSoftware.CommandLine;
7+
using Microsoft.Extensions.Configuration;
8+
using Xunit;
9+
10+
namespace J4JSoftware.Binder.Tests
11+
{
12+
public class CollectionConfigTests : BaseTest
13+
{
14+
[ Theory ]
15+
[ InlineData( "ACollection", "x", "-x", null, false ) ]
16+
[ InlineData( "ACollection", "x", "-z", null, false ) ]
17+
[ InlineData( "ACollection", "x", "-x expected", new string[] {"expected"}, false ) ]
18+
[ InlineData( "ACollection", "x", "-z expected", null, false ) ]
19+
[ InlineData( "ACollection", "x", "-x expected excess", new string[] {"expected", "excess"}, false ) ]
20+
[ InlineData( "ACollection", "x", "-z expected excess", null, false ) ]
21+
public void ContextDefinition(
22+
string contextKey,
23+
string cmdLineKey,
24+
string cmdLine,
25+
string[]? parsedValue,
26+
bool throws )
27+
{
28+
var options = CompositionRoot.Default.Options;
29+
30+
var option = options.Add( contextKey );
31+
32+
option.AddCommandLineKey( cmdLineKey )
33+
.SetStyle( OptionStyle.Collection );
34+
35+
var configBuilder = new ConfigurationBuilder();
36+
var allocator = CompositionRoot.Default.Allocator;
37+
38+
configBuilder.AddJ4JCommandLine( options, cmdLine, allocator );
39+
var config = configBuilder.Build();
40+
41+
if( throws )
42+
{
43+
var exception = Assert.Throws<InvalidOperationException>( () => config.Get<ConfigTarget>() );
44+
return;
45+
}
46+
47+
var result = config.Get<ConfigTarget>();
48+
49+
if( parsedValue == null )
50+
{
51+
result.Should().BeNull();
52+
return;
53+
}
54+
55+
var tgtProp = typeof(ConfigTarget).GetProperty( contextKey );
56+
tgtProp.Should().NotBeNull();
57+
58+
var tgtValue = tgtProp!.GetValue( result );
59+
tgtValue.Should().NotBeNull();
60+
tgtValue.Should().BeAssignableTo<List<string>>();
61+
62+
( (List<string>) tgtValue! ).Should().BeEquivalentTo( parsedValue.ToList() );
63+
}
64+
65+
[Theory]
66+
[InlineData(true, "x", "-x", null, false)]
67+
[InlineData(true, "x", "-z", null, false)]
68+
[InlineData(true, "x", "-x expected", new string[]{ "expected"}, false)]
69+
[InlineData(true, "x", "-z expected", null, false)]
70+
[InlineData(true, "x", "-x expected excess", new string[] {"expected", "excess"}, false)]
71+
[InlineData(true, "x", "-z expected excess", null, false)]
72+
public void TypeBound(
73+
bool shouldBind,
74+
string cmdLineKey,
75+
string cmdLine,
76+
string[]? parsedValue,
77+
bool throws)
78+
{
79+
var options = CompositionRoot.Default.GetTypeBoundOptions<ConfigTarget>();
80+
81+
options.Bind(x => x.ACollection, out var option)
82+
.Should()
83+
.Be(shouldBind);
84+
85+
if (!shouldBind)
86+
return;
87+
88+
option!.AddCommandLineKey(cmdLineKey);
89+
90+
var configBuilder = new ConfigurationBuilder();
91+
var allocator = CompositionRoot.Default.Allocator;
92+
93+
configBuilder.AddJ4JCommandLine(options, cmdLine, allocator);
94+
var config = configBuilder.Build();
95+
96+
if (throws)
97+
{
98+
var exception = Assert.Throws<InvalidOperationException>(() => config.Get<ConfigTarget>());
99+
return;
100+
}
101+
102+
var result = config.Get<ConfigTarget>();
103+
104+
if (parsedValue == null)
105+
{
106+
result.Should().BeNull();
107+
return;
108+
}
109+
110+
result.ACollection.Should().BeEquivalentTo( parsedValue.ToList() );
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)