Skip to content

Commit 10da86a

Browse files
committed
Added additional Binder tests. Fixed problems with Allocator. Reverted to using strings as context paths.
1 parent 648160a commit 10da86a

File tree

14 files changed

+303
-200
lines changed

14 files changed

+303
-200
lines changed

Binder.Tests/CollectionTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
9+
using FluentAssertions;
10+
using J4JSoftware.CommandLine;
11+
using Xunit;
12+
13+
namespace J4JSoftware.Binder.Tests
14+
{
15+
public class CollectionTests : BaseTest
16+
{
17+
[Theory]
18+
[InlineData("ACollection", "x", "-x", false, 0, 0)]
19+
[InlineData("ACollection", "x", "-z", false, 1, 0)]
20+
[InlineData("ACollection", "x", "-x expected", true, 0, 0)]
21+
[InlineData("ACollection", "x", "-z expected", false, 1, 1)]
22+
[InlineData("ACollection", "x", "-x expected expected", true, 0, 0)]
23+
[InlineData("ACollection", "x", "-z expected expected", false, 1, 2)]
24+
public void ByContextDefinition(
25+
string contextKey,
26+
string cmdLineKey,
27+
string cmdLine,
28+
bool valuesSatisfied,
29+
int unknownKeys,
30+
int unkeyedParams )
31+
{
32+
var options = CompositionRoot.Default.Options;
33+
34+
var option = options.Add( contextKey );
35+
36+
option.AddCommandLineKey( cmdLineKey )
37+
.SetStyle( OptionStyle.Collection );
38+
39+
var allocator = CompositionRoot.Default.Allocator;
40+
41+
var result = allocator.AllocateCommandLine( cmdLine, options );
42+
43+
option.ValuesSatisfied.Should().Be( valuesSatisfied );
44+
45+
result.UnknownKeys.Count.Should().Be( unknownKeys );
46+
result.UnkeyedParameters.Count.Should().Be( unkeyedParams );
47+
}
48+
}
49+
}

Binder.Tests/CompositionRoot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ private void ConfigureDependencyInjection( HostBuilderContext context, Container
6060
} )
6161
.AsSelf();
6262

63-
builder.RegisterType<DefaultTypeInitializer>()
64-
.AsImplementedInterfaces();
63+
//builder.RegisterType<DefaultTypeInitializer>()
64+
// .AsImplementedInterfaces();
6565

6666
builder.RegisterType<Allocator>()
6767
.AsImplementedInterfaces();

Binder.Tests/SingleValueTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
9+
using FluentAssertions;
10+
using J4JSoftware.CommandLine;
11+
using Xunit;
12+
13+
namespace J4JSoftware.Binder.Tests
14+
{
15+
public class SingleValueTests : BaseTest
16+
{
17+
[Theory]
18+
[InlineData("ASingleValue", "x", "-x", false, 0, 0)]
19+
[InlineData("ASingleValue", "x", "-z", false, 1, 0)]
20+
[InlineData("ASingleValue", "x", "-x expected", true, 0, 0)]
21+
[InlineData("ASingleValue", "x", "-z expected", false, 1, 1)]
22+
[InlineData("ASingleValue", "x", "-x expected excess", true, 0, 1)]
23+
[InlineData("ASingleValue", "x", "-z expected excess", false, 1, 2)]
24+
public void ByContextDefinition(
25+
string contextKey,
26+
string cmdLineKey,
27+
string cmdLine,
28+
bool valuesSatisfied,
29+
int unknownKeys,
30+
int unkeyedParams )
31+
{
32+
var options = CompositionRoot.Default.Options;
33+
34+
var option = options.Add( contextKey );
35+
36+
option.AddCommandLineKey( cmdLineKey )
37+
.SetStyle( OptionStyle.SingleValued );
38+
39+
var allocator = CompositionRoot.Default.Allocator;
40+
41+
var result = allocator.AllocateCommandLine( cmdLine, options );
42+
43+
option.ValuesSatisfied.Should().Be( valuesSatisfied );
44+
45+
result.UnknownKeys.Count.Should().Be( unknownKeys );
46+
result.UnkeyedParameters.Count.Should().Be( unkeyedParams );
47+
}
48+
}
49+
}

Binder.Tests/SwitchTests.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,30 @@ namespace J4JSoftware.Binder.Tests
1515
public class SwitchTests : BaseTest
1616
{
1717
[Theory]
18-
[InlineData("ASwitch", "x", "-x", OptionStyle.Switch, true, true, 0, 0)]
19-
[InlineData("ASwitch", "x", "-z", OptionStyle.Switch, false, false, 1, 0)]
18+
[InlineData("ASwitch", "x", "-x", true, 0, 0)]
19+
[InlineData("ASwitch", "x", "-z", false, 1, 0)]
20+
[InlineData("ASwitch", "x", "-x excess", true, 0, 1)]
21+
[InlineData("ASwitch", "x", "-z excess", false, 1, 1)]
2022
public void ByContextDefinition(
2123
string contextKey,
2224
string cmdLineKey,
2325
string cmdLine,
24-
OptionStyle style,
25-
bool allocResult,
26-
bool valueAssigned,
26+
bool valuesSatisfied,
2727
int unknownKeys,
2828
int unkeyedParams )
2929
{
3030
var options = CompositionRoot.Default.Options;
3131

32-
var option = options.Add( new SimpleContextKey( contextKey ) );
32+
var option = options.Add( contextKey );
3333

3434
option.AddCommandLineKey( cmdLineKey )
35-
.SetStyle( style );
35+
.SetStyle( OptionStyle.Switch );
3636

3737
var allocator = CompositionRoot.Default.Allocator;
3838

3939
var result = allocator.AllocateCommandLine( cmdLine, options );
4040

41-
( (bool) result ).Should().Be( allocResult );
42-
43-
option.WasAssignedValue.Should().Be( valueAssigned );
41+
option.ValuesSatisfied.Should().Be( valuesSatisfied );
4442

4543
result.UnknownKeys.Count.Should().Be( unknownKeys );
4644
result.UnkeyedParameters.Count.Should().Be( unkeyedParams );

Binder/Binder.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<Compile Remove="type-initializer\**" />
12+
<EmbeddedResource Remove="type-initializer\**" />
13+
<None Remove="type-initializer\**" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<Compile Remove="options\IContextKey.cs" />
1118
<Compile Remove="options\OptionConfiguration.cs" />
1219
<Compile Remove="options\OptionRequiredAttribute.cs" />
20+
<Compile Remove="options\SimpleContextKey.cs" />
1321
</ItemGroup>
1422

1523
<ItemGroup>

Binder/allocating/AllocationResult.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ namespace J4JSoftware.CommandLine
44
{
55
public class AllocationResult
66
{
7-
public static implicit operator bool( AllocationResult result ) => result.UnknownKeys.Count <= 0;
8-
97
public List<string> UnkeyedParameters { get; } = new List<string>();
108
public List<string> UnknownKeys { get; } = new List<string>();
119
}

Binder/allocating/Allocator.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public AllocationResult AllocateCommandLine( string cmdLine, Options options )
3939
var accumulator = new StringBuilder();
4040
Option? curOption = null;
4141
var charsProcessed = 0;
42-
var lastElementWasKey = false;
4342

4443
for( var idx = 0; idx < cmdLine.Length; idx++ )
4544
{
@@ -72,8 +71,6 @@ public AllocationResult AllocateCommandLine( string cmdLine, Options options )
7271
{
7372
curOption = options[element];
7473
curOption!.CommandLineKeyProvided = element;
75-
76-
lastElementWasKey = true;
7774
}
7875
else
7976
{
@@ -84,11 +81,10 @@ public AllocationResult AllocateCommandLine( string cmdLine, Options options )
8481
else
8582
{
8683
// element is parameter value
87-
if( curOption == null || !lastElementWasKey )
84+
if( curOption == null
85+
|| curOption.NumValuesAllocated >= curOption.MaxValues )
8886
retVal.UnkeyedParameters.Add( element );
8987
else curOption.AddAllocatedValue( element );
90-
91-
lastElementWasKey = false;
9288
}
9389

9490
// clear the accumulator so we can start processing the next character sequence

Binder/options/IContextKey.cs

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

Binder/options/Option.cs

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.ObjectModel;
44
using System.ComponentModel;
55
using System.Linq;
6+
using System.Reflection.Metadata.Ecma335;
67

78
namespace J4JSoftware.CommandLine
89
{
@@ -12,62 +13,45 @@ public class Option
1213
private readonly List<string> _allocatedValues = new List<string>();
1314
private readonly MasterTextCollection _masterText;
1415

15-
private int _allowedNumValues;
16-
1716
internal Option(
18-
Options container,
19-
IContextKey contextKey,
17+
OptionsBase container,
18+
string contextPath,
2019
MasterTextCollection masterText
2120
)
2221
{
2322
Container = container;
24-
ContextKey = contextKey;
23+
ContextPath = contextPath;
2524
_masterText = masterText;
2625

2726
if( Container.UsesContextPath( ContextPath! ) )
2827
throw new ArgumentException( $"Duplicate context key path '{ContextPath}'" );
2928
}
3029

3130
// the collection of Options used by the parsing activity
32-
public Options Container { get; }
33-
34-
public IContextKey? ContextKey { get; }
35-
36-
public List<IContextKey>? ContextPath
37-
{
38-
get
39-
{
40-
if( Parent == null || ContextKey == null )
41-
{
42-
if( ContextKey == null )
43-
return null;
44-
45-
return new List<IContextKey> { ContextKey };
46-
}
31+
public OptionsBase Container { get; }
4732

48-
var retVal = Parent.ContextPath!;
49-
50-
retVal.Add( ContextKey );
51-
52-
return retVal;
53-
}
54-
}
55-
56-
public Option? Parent { get; private set; }
33+
public string? ContextPath { get; }
5734

5835
public ReadOnlyCollection<string> Keys => _cmdLineKeys.AsReadOnly();
5936

60-
// the first key defined for an option, sorted alphabetically (Options can define multiple keys
61-
// but they must be unique within the scope of all Options)
62-
public string FirstKey => _cmdLineKeys.OrderBy( k => k ).First();
63-
6437
public string? CommandLineKeyProvided { get; set; }
6538

66-
public bool WasAssignedValue
39+
public int MaxValues =>
40+
Style switch
41+
{
42+
OptionStyle.Collection => int.MaxValue,
43+
OptionStyle.SingleValued => 1,
44+
OptionStyle.Switch => 0,
45+
_ => throw new InvalidEnumArgumentException( $"Unsupported OptionStyle '{Style}'" )
46+
};
47+
48+
public int NumValuesAllocated => _allocatedValues.Count;
49+
50+
public bool ValuesSatisfied
6751
{
6852
get
6953
{
70-
if( string.IsNullOrEmpty( CommandLineKeyProvided ) )
54+
if (string.IsNullOrEmpty(CommandLineKeyProvided))
7155
return false;
7256

7357
var numValuesAlloc = _allocatedValues.Count;
@@ -81,7 +65,7 @@ public bool WasAssignedValue
8165
};
8266
}
8367
}
84-
68+
8569
public ReadOnlyCollection<string> CommandLineValues => _allocatedValues.AsReadOnly();
8670

8771
public OptionStyle Style { get; private set; }
@@ -114,14 +98,7 @@ public Option AddCommandLineKeys(params string[] cmdLineKeys)
11498

11599
public Option SetStyle( OptionStyle style )
116100
{
117-
_allowedNumValues = style switch
118-
{
119-
OptionStyle.Collection => Int32.MaxValue,
120-
OptionStyle.SingleValued => 1,
121-
OptionStyle.Switch => 0,
122-
_ => throw new InvalidEnumArgumentException( $"Unsupported OptionStyle '{style}'" )
123-
};
124-
101+
Style = style;
125102
return this;
126103
}
127104

@@ -142,13 +119,5 @@ public Option SetDescription(string description)
142119
Description = description;
143120
return this;
144121
}
145-
146-
public Option ChildOf(Option parent)
147-
{
148-
if (parent != this)
149-
Parent = parent;
150-
151-
return this;
152-
}
153122
}
154123
}

Binder/options/OptionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ namespace J4JSoftware.CommandLine
77
{
88
public static class OptionExtensions
99
{
10-
public static string ToTextPath( this List<IContextKey> contextKeys ) =>
10+
public static string ToTextPath( this List<string> contextKeys ) =>
1111
contextKeys.Aggregate(
1212
new StringBuilder(),
13-
( sb, ick ) =>
13+
( sb, t ) =>
1414
{
1515
if( sb.Length > 0 )
1616
sb.Append( ":" );
1717

18-
sb.Append( ick.Text );
18+
sb.Append( t );
1919

2020
return sb;
2121
},

0 commit comments

Comments
 (0)