Skip to content

Commit 326ff26

Browse files
committed
Restructured Binder testing to use a JSON test data file. Fixed some problems related to allocations.
1 parent 5ba7d6a commit 326ff26

25 files changed

+767
-773
lines changed

Binder.Tests/Binder.Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<Nullable>enable</Nullable>
1313
</PropertyGroup>
1414

15+
<ItemGroup>
16+
<Compile Remove="test-data\CollectionTests.cs" />
17+
<Compile Remove="test-data\SingleValueTests.cs" />
18+
<Compile Remove="test-data\SwitchTests.cs" />
19+
<Compile Remove="EmbeddedTest.cs" />
20+
</ItemGroup>
21+
1522
<ItemGroup>
1623
<PackageReference Include="Autofac" Version="6.1.0" />
1724
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
@@ -39,6 +46,9 @@
3946
<None Update="appConfig.json">
4047
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4148
</None>
49+
<None Update="singleProperties.json">
50+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
51+
</None>
4252
</ItemGroup>
4353

4454
</Project>

Binder.Tests/ContextTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Xunit;
2+
3+
namespace J4JSoftware.Binder.Tests
4+
{
5+
public class ContextTests : BaseTest
6+
{
7+
[Theory]
8+
[MemberData(nameof(TestDataSource.GetSinglePropertyData), MemberType=typeof(TestDataSource))]
9+
public void SinglePropertyAllocations( TestConfig config )
10+
{
11+
Initialize( config );
12+
13+
ValidateAllocations();
14+
}
15+
16+
[Theory]
17+
[MemberData(nameof(TestDataSource.GetSinglePropertyData), MemberType = typeof(TestDataSource))]
18+
public void SinglePropertyParsing(TestConfig config)
19+
{
20+
Initialize(config);
21+
22+
ValidateConfiguration<BasicTarget>();
23+
}
24+
}
25+
}

Binder.Tests/EmbeddedTest.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using FluentAssertions;
2+
using J4JSoftware.CommandLine;
3+
using Xunit;
4+
5+
namespace J4JSoftware.Binder.Tests
6+
{
7+
public class EmbeddedTest : BaseTest
8+
{
9+
private OptionsBase? _options;
10+
private string? _parentContextKey;
11+
private IAllocator? _allocator;
12+
13+
[Theory]
14+
[InlineData("Target1", "ASwitch", "x", OptionStyle.Switch, "-x", true, 0, 0 )]
15+
public void ContextDefinition(
16+
string parentContextKey,
17+
string contextKey,
18+
string cmdLineKey,
19+
OptionStyle style,
20+
string cmdLine,
21+
bool valuesSatisfied,
22+
int unknownKeys,
23+
int unkeyedParams )
24+
{
25+
Initialize(parentContextKey);
26+
27+
var option = AddOption( contextKey, cmdLineKey, style );
28+
29+
var result = _allocator!.AllocateCommandLine(cmdLine, _options!);
30+
31+
option.ValuesSatisfied.Should().Be(valuesSatisfied);
32+
33+
result.UnknownKeys.Count.Should().Be(unknownKeys);
34+
result.UnkeyedParameters.Count.Should().Be(unkeyedParams);
35+
}
36+
37+
[ Theory ]
38+
[ InlineData( true, "ASwitch", "x", OptionStyle.Switch, "-x", true, 0, 0 ) ]
39+
public void TypeBound(
40+
bool shouldBind,
41+
string cmdLineKey,
42+
string cmdLine,
43+
bool parsedValue,
44+
bool throws )
45+
{
46+
Initialize();
47+
48+
options.Bind(x => x.ASwitch, out var option)
49+
.Should()
50+
.Be(shouldBind);
51+
52+
if (!shouldBind)
53+
return;
54+
55+
option!.AddCommandLineKey(cmdLineKey);
56+
57+
var allocator = CompositionRoot.Default.GetAllocator();
58+
59+
var configBuilder = new ConfigurationBuilder();
60+
configBuilder.AddJ4JCommandLine(options, cmdLine, allocator);
61+
var config = configBuilder.Build();
62+
63+
if (throws)
64+
{
65+
var exception = Assert.Throws<InvalidOperationException>(() => config.Get<BasicTarget>());
66+
return;
67+
}
68+
69+
var result = config.Get<BasicTarget>();
70+
71+
result.ASwitch.Should().Be(parsedValue);
72+
}
73+
74+
private void Initialize( string? parentContextKey = null )
75+
{
76+
_options = parentContextKey == null
77+
? CompositionRoot.Default.GetTypeBoundOptions<EmbeddedTarget>()
78+
: CompositionRoot.Default.GetOptions();
79+
80+
_parentContextKey = parentContextKey;
81+
_allocator = CompositionRoot.Default.GetAllocator();
82+
}
83+
84+
private Option AddOption(string contextKey, string cmdLineKey, OptionStyle style )
85+
{
86+
var retVal = ((Options) _options!).Add( $"{_parentContextKey}:{contextKey}" );
87+
88+
return retVal.AddCommandLineKey( cmdLineKey )
89+
.SetStyle( style );
90+
}
91+
}
92+
}

Binder.Tests/allocation-tests/CollectionTests.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.

Binder.Tests/allocation-tests/SingleValueTests.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.

Binder.Tests/allocation-tests/SwitchTests.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)