Skip to content

Commit bc3dc14

Browse files
committed
Fixed various problems in allocation and in binding to target type properties. Added a number of tests, all of which are currently being passed.
1 parent 10da86a commit bc3dc14

22 files changed

+535
-100
lines changed

Binder.Tests/TestProperties.cs

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

Binder.Tests/CollectionTests.cs renamed to Binder.Tests/allocation-tests/CollectionTests.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading;
7-
using System.Threading.Tasks;
8-
using Binder.Tests;
1+
using Binder.Tests;
92
using FluentAssertions;
103
using J4JSoftware.CommandLine;
114
using Xunit;
@@ -21,7 +14,7 @@ public class CollectionTests : BaseTest
2114
[InlineData("ACollection", "x", "-z expected", false, 1, 1)]
2215
[InlineData("ACollection", "x", "-x expected expected", true, 0, 0)]
2316
[InlineData("ACollection", "x", "-z expected expected", false, 1, 2)]
24-
public void ByContextDefinition(
17+
public void ContextDefinition(
2518
string contextKey,
2619
string cmdLineKey,
2720
string cmdLine,
@@ -45,5 +38,41 @@ public void ByContextDefinition(
4538
result.UnknownKeys.Count.Should().Be( unknownKeys );
4639
result.UnkeyedParameters.Count.Should().Be( unkeyedParams );
4740
}
41+
42+
[Theory]
43+
[InlineData(true, "x", "-x", false, 0, 0)]
44+
[InlineData(true, "x", "-z", false, 1, 0)]
45+
[InlineData(true, "x", "-x expected", true, 0, 0)]
46+
[InlineData(true, "x", "-z expected", false, 1, 1)]
47+
[InlineData(true, "x", "-x expected expected", true, 0, 0)]
48+
[InlineData(true, "x", "-z expected expected", false, 1, 2)]
49+
public void TypeBound(
50+
bool shouldBind,
51+
string cmdLineKey,
52+
string cmdLine,
53+
bool valuesSatisfied,
54+
int unknownKeys,
55+
int unkeyedParams)
56+
{
57+
var options = CompositionRoot.Default.GetTypeBoundOptions<ConfigTarget>();
58+
59+
options.Bind(x => x.ACollection, out var option)
60+
.Should()
61+
.Be(shouldBind);
62+
63+
if (!shouldBind)
64+
return;
65+
66+
option!.AddCommandLineKey(cmdLineKey);
67+
68+
var allocator = CompositionRoot.Default.Allocator;
69+
70+
var result = allocator.AllocateCommandLine(cmdLine, options);
71+
72+
option.ValuesSatisfied.Should().Be(valuesSatisfied);
73+
74+
result.UnknownKeys.Count.Should().Be(unknownKeys);
75+
result.UnkeyedParameters.Count.Should().Be(unkeyedParams);
76+
}
4877
}
4978
}

Binder.Tests/SingleValueTests.cs renamed to Binder.Tests/allocation-tests/SingleValueTests.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading;
7-
using System.Threading.Tasks;
8-
using Binder.Tests;
1+
using Binder.Tests;
92
using FluentAssertions;
103
using J4JSoftware.CommandLine;
114
using Xunit;
@@ -21,7 +14,7 @@ public class SingleValueTests : BaseTest
2114
[InlineData("ASingleValue", "x", "-z expected", false, 1, 1)]
2215
[InlineData("ASingleValue", "x", "-x expected excess", true, 0, 1)]
2316
[InlineData("ASingleValue", "x", "-z expected excess", false, 1, 2)]
24-
public void ByContextDefinition(
17+
public void ContextDefinition(
2518
string contextKey,
2619
string cmdLineKey,
2720
string cmdLine,
@@ -45,5 +38,41 @@ public void ByContextDefinition(
4538
result.UnknownKeys.Count.Should().Be( unknownKeys );
4639
result.UnkeyedParameters.Count.Should().Be( unkeyedParams );
4740
}
41+
42+
[Theory]
43+
[InlineData(true, "x", "-x", false, 0, 0)]
44+
[InlineData(true, "x", "-z", false, 1, 0)]
45+
[InlineData(true, "x", "-x expected", true, 0, 0)]
46+
[InlineData(true, "x", "-z expected", false, 1, 1)]
47+
[InlineData(true, "x", "-x expected excess", true, 0, 1)]
48+
[InlineData(true, "x", "-z expected excess", false, 1, 2)]
49+
public void TypeBound(
50+
bool shouldBind,
51+
string cmdLineKey,
52+
string cmdLine,
53+
bool valuesSatisfied,
54+
int unknownKeys,
55+
int unkeyedParams)
56+
{
57+
var options = CompositionRoot.Default.GetTypeBoundOptions<ConfigTarget>();
58+
59+
options.Bind(x => x.ASingleValue, out var option)
60+
.Should()
61+
.Be(shouldBind);
62+
63+
if (!shouldBind)
64+
return;
65+
66+
option!.AddCommandLineKey( cmdLineKey );
67+
68+
var allocator = CompositionRoot.Default.Allocator;
69+
70+
var result = allocator.AllocateCommandLine(cmdLine, options);
71+
72+
option!.ValuesSatisfied.Should().Be(valuesSatisfied);
73+
74+
result.UnknownKeys.Count.Should().Be(unknownKeys);
75+
result.UnkeyedParameters.Count.Should().Be(unkeyedParams);
76+
}
4877
}
4978
}

Binder.Tests/SwitchTests.cs renamed to Binder.Tests/allocation-tests/SwitchTests.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading;
7-
using System.Threading.Tasks;
8-
using Binder.Tests;
1+
using Binder.Tests;
92
using FluentAssertions;
103
using J4JSoftware.CommandLine;
114
using Xunit;
@@ -19,7 +12,7 @@ public class SwitchTests : BaseTest
1912
[InlineData("ASwitch", "x", "-z", false, 1, 0)]
2013
[InlineData("ASwitch", "x", "-x excess", true, 0, 1)]
2114
[InlineData("ASwitch", "x", "-z excess", false, 1, 1)]
22-
public void ByContextDefinition(
15+
public void ContextDefinition(
2316
string contextKey,
2417
string cmdLineKey,
2518
string cmdLine,
@@ -43,5 +36,39 @@ public void ByContextDefinition(
4336
result.UnknownKeys.Count.Should().Be( unknownKeys );
4437
result.UnkeyedParameters.Count.Should().Be( unkeyedParams );
4538
}
39+
40+
[Theory]
41+
[InlineData(true, "x", "-x", true, 0, 0)]
42+
[InlineData(true, "x", "-z", false, 1, 0)]
43+
[InlineData(true, "x", "-x excess", true, 0, 1)]
44+
[InlineData(true, "x", "-z excess", false, 1, 1)]
45+
public void TypeBound(
46+
bool shouldBind,
47+
string cmdLineKey,
48+
string cmdLine,
49+
bool valuesSatisfied,
50+
int unknownKeys,
51+
int unkeyedParams)
52+
{
53+
var options = CompositionRoot.Default.GetTypeBoundOptions<ConfigTarget>();
54+
55+
options.Bind( x => x.ASwitch, out var option)
56+
.Should()
57+
.Be( shouldBind );
58+
59+
if( !shouldBind )
60+
return;
61+
62+
option!.AddCommandLineKey(cmdLineKey);
63+
64+
var allocator = CompositionRoot.Default.Allocator;
65+
66+
var result = allocator.AllocateCommandLine(cmdLine, options);
67+
68+
option!.ValuesSatisfied.Should().Be(valuesSatisfied);
69+
70+
result.UnknownKeys.Count.Should().Be(unknownKeys);
71+
result.UnkeyedParameters.Count.Should().Be(unkeyedParams);
72+
}
4673
}
4774
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using Binder.Tests;
3+
using FluentAssertions;
4+
using J4JSoftware.CommandLine;
5+
using Microsoft.Extensions.Configuration;
6+
using Xunit;
7+
8+
namespace J4JSoftware.Binder.Tests
9+
{
10+
public class SingleValueConfigTests : BaseTest
11+
{
12+
[ Theory ]
13+
[ InlineData( "ASingleValue", OptionStyle.SingleValued, "x", "-x", "", false ) ]
14+
[ InlineData( "ASingleValue", OptionStyle.SingleValued, "x", "-z", "", false ) ]
15+
[ InlineData( "ASingleValue", OptionStyle.SingleValued, "x", "-x expected", "expected", false ) ]
16+
[ InlineData( "ASingleValue", OptionStyle.SingleValued, "x", "-z expected", "", false ) ]
17+
[ InlineData( "ASingleValue", OptionStyle.SingleValued, "x", "-x expected excess", "expected", false ) ]
18+
[ InlineData( "ASingleValue", OptionStyle.SingleValued, "x", "-z expected excess", "", false ) ]
19+
[ InlineData( "AnEnumValue", OptionStyle.SingleValued, "x", "-x EnumValue1", TestEnum.EnumValue1, false ) ]
20+
[ InlineData( "AnEnumValue", OptionStyle.SingleValued, "x", "-x EnumValue2", TestEnum.EnumValue2, false ) ]
21+
[ InlineData( "AnEnumValue", OptionStyle.SingleValued, "x", "-x EnumValue3", null, true ) ]
22+
[ InlineData( "AnEnumValue", OptionStyle.SingleValued, "x", "-x enumvalue1", TestEnum.EnumValue1, false ) ]
23+
[ InlineData( "AFlagEnumValue", OptionStyle.Collection, "x", "-x EnumValue1", TestFlagEnum.EnumValue1, false ) ]
24+
[ InlineData( "AFlagEnumValue", OptionStyle.Collection, "x", "-x EnumValue2", TestFlagEnum.EnumValue2, false ) ]
25+
[ InlineData( "AFlagEnumValue", OptionStyle.Collection, "x", "-x EnumValue1 EnumValue2",
26+
TestFlagEnum.EnumValue1 | TestFlagEnum.EnumValue2, false ) ]
27+
public void ByContextDefinition(
28+
string contextKey,
29+
OptionStyle style,
30+
string cmdLineKey,
31+
string cmdLine,
32+
object parsedValue,
33+
bool throws )
34+
{
35+
var options = CompositionRoot.Default.Options;
36+
37+
var option = options.Add( contextKey );
38+
39+
option.AddCommandLineKey( cmdLineKey )
40+
.SetStyle( style );
41+
42+
var configBuilder = new ConfigurationBuilder();
43+
var allocator = CompositionRoot.Default.Allocator;
44+
45+
configBuilder.AddJ4JCommandLine( options, cmdLine, allocator );
46+
var config = configBuilder.Build();
47+
48+
if( throws )
49+
{
50+
var exception = Assert.Throws<InvalidOperationException>( () => config.Get<ConfigTarget>() );
51+
return;
52+
}
53+
54+
var result = config.Get<ConfigTarget>();
55+
56+
var tgtProp = typeof(ConfigTarget).GetProperty( contextKey );
57+
tgtProp.Should().NotBeNull();
58+
59+
var tgtValue = tgtProp!.GetValue( result );
60+
61+
tgtValue.Should().Be( parsedValue );
62+
}
63+
}
64+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Binder.Tests;
2+
using FluentAssertions;
3+
using J4JSoftware.CommandLine;
4+
using Microsoft.Extensions.Configuration;
5+
using Xunit;
6+
7+
namespace J4JSoftware.Binder.Tests
8+
{
9+
public class SwitchConfigTests : BaseTest
10+
{
11+
[Theory]
12+
[InlineData("ASwitch", "x", "-x", true)]
13+
[InlineData("ASwitch", "x", "-z", false)]
14+
[InlineData("ASwitch", "x", "-x excess", true)]
15+
[InlineData("ASwitch", "x", "-z excess", false)]
16+
public void ContextDefinition(
17+
string contextKey,
18+
string cmdLineKey,
19+
string cmdLine,
20+
bool parsedValue
21+
)
22+
{
23+
var options = CompositionRoot.Default.Options;
24+
25+
var option = options.Add( contextKey );
26+
27+
option.AddCommandLineKey( cmdLineKey )
28+
.SetStyle( OptionStyle.Switch );
29+
30+
var allocator = CompositionRoot.Default.Allocator;
31+
32+
var configBuilder = new ConfigurationBuilder();
33+
configBuilder.AddJ4JCommandLine( options, cmdLine, allocator );
34+
var config = configBuilder.Build();
35+
36+
var result = config.Get<ConfigTarget>();
37+
38+
result.ASwitch.Should().Be( parsedValue );
39+
}
40+
41+
[Theory]
42+
[InlineData(true, "x", "-x", true)]
43+
[InlineData(true, "x", "-z", false)]
44+
[InlineData(true, "x", "-x excess", true)]
45+
[InlineData(true, "x", "-z excess", false)]
46+
public void TypeBound(
47+
bool shouldBind,
48+
string cmdLineKey,
49+
string cmdLine,
50+
bool parsedValue
51+
)
52+
{
53+
var options = CompositionRoot.Default.GetTypeBoundOptions<ConfigTarget>();
54+
55+
options.Bind(x => x.ASwitch, out var option)
56+
.Should()
57+
.Be(shouldBind);
58+
59+
if (!shouldBind)
60+
return;
61+
62+
option!.AddCommandLineKey(cmdLineKey);
63+
64+
var allocator = CompositionRoot.Default.Allocator;
65+
66+
var configBuilder = new ConfigurationBuilder();
67+
configBuilder.AddJ4JCommandLine(options, cmdLine, allocator);
68+
var config = configBuilder.Build();
69+
70+
var result = config.Get<ConfigTarget>();
71+
72+
result.ASwitch.Should().Be(parsedValue);
73+
}
74+
}
75+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace J4JSoftware.Binder.Tests
4+
{
5+
public class ConfigTarget
6+
{
7+
public bool ASwitch { get; set; }
8+
public string ASingleValue { get; set; }
9+
public List<string> ACollection { get; set; }
10+
public TestEnum AnEnumValue { get; set; }
11+
public TestFlagEnum AFlagEnumValue { get; set; }
12+
}
13+
}

Binder.Tests/results/TestEnum.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace J4JSoftware.Binder.Tests
2+
{
3+
public enum TestEnum
4+
{
5+
EnumValue1,
6+
EnumValue2
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace J4JSoftware.Binder.Tests
4+
{
5+
[Flags]
6+
public enum TestFlagEnum
7+
{
8+
EnumValue1 = 1 << 0,
9+
EnumValue2 = 1 << 1,
10+
}
11+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Binder.Tests;
1+
using Binder.Tests;
72
using J4JSoftware.Logging;
8-
using Microsoft.Extensions.Hosting;
93

104
namespace J4JSoftware.Binder.Tests
115
{

0 commit comments

Comments
 (0)