Skip to content

Commit fab3d92

Browse files
committed
Fixed problems with IConfigurationBuilder extension methods. Fixed testing code.
1 parent a5d35ec commit fab3d92

File tree

3 files changed

+82
-67
lines changed

3 files changed

+82
-67
lines changed

Binder.Tests/support/BaseTest.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Linq.Expressions;
44
using System.Reflection;
5+
using System.Reflection.Metadata;
56
using System.Text.Json;
67
using FluentAssertions;
78
using J4JSoftware.CommandLine;
@@ -12,7 +13,8 @@ namespace J4JSoftware.Binder.Tests
1213
{
1314
public class BaseTest
1415
{
15-
private readonly CommandLineLogger _cmdLineErrors = new CommandLineLogger();
16+
private CommandLineLogger? _cmdLineErrors;
17+
private IConfigurationBuilder? _configBuilder;
1618

1719
protected TestConfig? TestConfig { get; private set; }
1820
protected IAllocator? Allocator { get; private set; }
@@ -22,14 +24,16 @@ protected void Initialize( TestConfig testConfig )
2224
{
2325
TestConfig = testConfig;
2426

25-
Options = new OptionCollection( MasterTextCollection.WindowsDefault, _cmdLineErrors );
26-
27-
Allocator = new Allocator(
28-
new ElementTerminator( MasterTextCollection.WindowsDefault, _cmdLineErrors ),
29-
new KeyPrefixer( MasterTextCollection.WindowsDefault, _cmdLineErrors ),
30-
_cmdLineErrors );
31-
32-
TestConfig = testConfig;
27+
_configBuilder = new ConfigurationBuilder().AddJ4JCommandLineWindows(
28+
TestConfig!.CommandLine,
29+
out var options,
30+
out var allocator,
31+
out var errors );
32+
33+
errors.HasMessages.Should().BeFalse();
34+
35+
Options = options;
36+
Allocator = allocator;
3337
}
3438

3539
protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelector, bool bindNonPublic = false )
@@ -70,19 +74,17 @@ protected void ValidateAllocations()
7074
protected void ValidateConfiguration<TParsed>()
7175
where TParsed : class, new()
7276
{
73-
var configBuilder = new ConfigurationBuilder()
74-
.AddJ4JCommandLine( TestConfig!.CommandLine, MasterTextCollection.WindowsDefault, Allocator!,
75-
_cmdLineErrors, out var options );
76-
77+
var config = _configBuilder!.Build();
78+
7779
TParsed? parsed = null;
7880

7981
if( TestConfig!.OptionConfigurations.Any( x => x.ParsingWillFail ) )
8082
{
81-
var exception = Assert.Throws<InvalidOperationException>( () => _configRoot.Get<TParsed>() );
83+
var exception = Assert.Throws<InvalidOperationException>( () => config.Get<TParsed>() );
8284
return;
8385
}
8486

85-
parsed = _configRoot.Get<TParsed>();
87+
parsed = config.Get<TParsed>();
8688

8789
if( TestConfig.OptionConfigurations.TrueForAll( x => !x.ValuesSatisfied )
8890
&& TestConfig.OptionConfigurations.All( x => x.Style != OptionStyle.Switch ) )

Binder/J4JCommandLineExtensions.cs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ public static IConfigurationBuilder AddJ4JCommandLine(
1010
string cmdLine,
1111
MasterTextCollection masterText,
1212
IAllocator allocator,
13-
CommandLineLogger logger,
14-
out OptionCollection options )
13+
OptionCollection options,
14+
CommandLineLogger logger )
1515
{
16-
options = new OptionCollection( masterText, logger );
17-
1816
builder.Add( new J4JCommandLineSource( options, cmdLine, allocator ) );
1917

2018
return builder;
@@ -28,46 +26,50 @@ public static IConfigurationBuilder AddJ4JCommandLineWindows(
2826
IAllocator? allocator = null )
2927
{
3028
logger = new CommandLineLogger();
29+
30+
var mt = MasterTextCollection.GetWindowsDefault();
3131

3232
allocator ??= new Allocator(
33-
new ElementTerminator( MasterTextCollection.WindowsDefault, logger ),
34-
new KeyPrefixer( MasterTextCollection.WindowsDefault, logger ),
33+
new ElementTerminator( mt, logger ),
34+
new KeyPrefixer( mt, logger ),
3535
logger );
36-
37-
builder.AddJ4JCommandLine(
38-
cmdLine,
39-
MasterTextCollection.WindowsDefault,
40-
allocator,
41-
logger,
42-
out var innerOptions);
4336

44-
options = innerOptions;
37+
options = new OptionCollection( mt, logger );
38+
39+
builder.AddJ4JCommandLine(
40+
cmdLine,
41+
mt,
42+
allocator,
43+
options,
44+
logger );
4545

4646
return builder;
4747
}
4848

4949
public static IConfigurationBuilder AddJ4JCommandLineWindows(
5050
this IConfigurationBuilder builder,
5151
string cmdLine,
52-
out IAllocator allocator,
5352
out OptionCollection options,
54-
out CommandLineLogger logger)
53+
out IAllocator allocator,
54+
out CommandLineLogger logger )
5555
{
5656
logger = new CommandLineLogger();
5757

58+
var mt = MasterTextCollection.GetWindowsDefault();
59+
5860
allocator = new Allocator(
59-
new ElementTerminator(MasterTextCollection.WindowsDefault, logger),
60-
new KeyPrefixer(MasterTextCollection.WindowsDefault, logger),
61+
new ElementTerminator(mt, logger),
62+
new KeyPrefixer(mt, logger),
6163
logger);
6264

65+
options = new OptionCollection(mt, logger);
66+
6367
builder.AddJ4JCommandLine(
6468
cmdLine,
65-
MasterTextCollection.WindowsDefault,
69+
mt,
6670
allocator,
67-
logger,
68-
out var innerOptions);
69-
70-
options = innerOptions;
71+
options,
72+
logger);
7173

7274
return builder;
7375
}
@@ -81,47 +83,52 @@ public static IConfigurationBuilder AddJ4JCommandLineLinux(
8183
{
8284
logger = new CommandLineLogger();
8385

86+
var mt = MasterTextCollection.GetLinuxDefault();
87+
8488
allocator ??= new Allocator(
85-
new ElementTerminator(MasterTextCollection.LinuxDefault, logger),
86-
new KeyPrefixer(MasterTextCollection.LinuxDefault, logger),
89+
new ElementTerminator(mt, logger),
90+
new KeyPrefixer(mt, logger),
8791
logger);
8892

93+
options = new OptionCollection(mt, logger);
94+
8995
builder.AddJ4JCommandLine(
9096
cmdLine,
91-
MasterTextCollection.LinuxDefault,
97+
mt,
9298
allocator,
93-
logger,
94-
out var innerOptions);
95-
96-
options = innerOptions;
99+
options,
100+
logger);
97101

98102
return builder;
99103
}
100104

101105
public static IConfigurationBuilder AddJ4JCommandLineLinux(
102106
this IConfigurationBuilder builder,
103107
string cmdLine,
104-
out IAllocator allocator,
105108
out OptionCollection options,
109+
out IAllocator allocator,
106110
out CommandLineLogger logger)
107111
{
108112
logger = new CommandLineLogger();
109113

114+
var mt = MasterTextCollection.GetLinuxDefault();
115+
110116
allocator = new Allocator(
111-
new ElementTerminator(MasterTextCollection.LinuxDefault, logger),
112-
new KeyPrefixer(MasterTextCollection.LinuxDefault, logger),
117+
new ElementTerminator(mt, logger),
118+
new KeyPrefixer(mt, logger),
113119
logger);
114120

121+
options = new OptionCollection(mt, logger);
122+
115123
builder.AddJ4JCommandLine(
116124
cmdLine,
117-
MasterTextCollection.LinuxDefault,
125+
mt,
118126
allocator,
119-
logger,
120-
out var innerOptions);
121-
122-
options = innerOptions;
127+
options,
128+
logger);
123129

124130
return builder;
125131
}
132+
126133
}
127134
}

Binder/allocating/MasterTextCollection.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,32 @@ namespace J4JSoftware.CommandLine
1111
// option key) used in the framework and ensures they are all used uniquely.
1212
public class MasterTextCollection
1313
{
14-
static MasterTextCollection()
14+
public static MasterTextCollection GetWindowsDefault( StringComparison? comparison = null )
1515
{
16-
WindowsDefault = new MasterTextCollection();
17-
WindowsDefault.AddRange( TextUsageType.Prefix, "/", "-", "--" );
18-
WindowsDefault.Add( TextUsageType.Quote, "\"" );
19-
WindowsDefault.Add( TextUsageType.ValueEncloser, "=" );
16+
var retVal = new MasterTextCollection();
17+
18+
retVal.AddRange( TextUsageType.Prefix, "/", "-", "--" );
19+
retVal.Add( TextUsageType.Quote, "\"" );
20+
retVal.Add( TextUsageType.ValueEncloser, "=" );
21+
22+
retVal.Initialize( comparison.HasValue ? comparison.Value : StringComparison.OrdinalIgnoreCase );
23+
24+
return retVal;
25+
}
26+
27+
public static MasterTextCollection GetLinuxDefault( StringComparison? comparison = null )
28+
{
29+
var retVal = new MasterTextCollection();
2030

21-
WindowsDefault.Initialize(StringComparison.OrdinalIgnoreCase);
31+
retVal.AddRange(TextUsageType.Prefix, "-", "--");
32+
retVal.AddRange(TextUsageType.Quote, "\"", "'");
33+
retVal.Add(TextUsageType.ValueEncloser, "=");
2234

23-
LinuxDefault = new MasterTextCollection();
24-
LinuxDefault.AddRange( TextUsageType.Prefix, "-", "--" );
25-
LinuxDefault.AddRange( TextUsageType.Quote, "\"", "'" );
26-
LinuxDefault.Add( TextUsageType.ValueEncloser, "=" );
35+
retVal.Initialize(comparison.HasValue ? comparison.Value : StringComparison.Ordinal);
2736

28-
LinuxDefault.Initialize( StringComparison.Ordinal );
37+
return retVal;
2938
}
3039

31-
public static MasterTextCollection WindowsDefault { get; }
32-
public static MasterTextCollection LinuxDefault { get; }
33-
3440
private readonly List<TextUsage> _items = new List<TextUsage>();
3541

3642
public bool IsValid => TextComparer != null && TextComparison != null;

0 commit comments

Comments
 (0)