Skip to content

Commit fa42fd3

Browse files
committed
Binder, using tokenized "LL(1)" grammar approach, now passes all current tests. Cleanup of old/unneeded files. Moved Binder project into J4JSoftware.Configuration.CommandLine namespace.
1 parent 42c8d83 commit fa42fd3

File tree

78 files changed

+1220
-1836
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1220
-1836
lines changed

Binder.Tests/Binder.Tests.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@
4747
<None Update="appConfig.json">
4848
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4949
</None>
50-
<None Update="embeddedProperties.json">
50+
<None Update="test-files\embeddedProperties.json">
5151
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5252
</None>
53-
<None Update="singleProperties.json">
53+
<None Update="test-files\singleProperties.json">
5454
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5555
</None>
56-
<None Update="tokenizer.json">
56+
<None Update="test-files\parser.json">
57+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
58+
</None>
59+
<None Update="test-files\tokenizer.json">
5760
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5861
</None>
5962
</ItemGroup>

Binder.Tests/EmbeddedProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void Allocations( TestConfig config )
1717
Bind<EmbeddedTarget, TestFlagEnum>(x => x.Target1.AFlagEnumValue);
1818
Bind<EmbeddedTarget, List<string>>(x => x.Target1.ACollection);
1919

20-
ValidateAllocations();
20+
ValidateTokenizing();
2121
}
2222

2323
[Theory]

Binder.Tests/MiscAllocation.cs

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

Binder.Tests/MiscTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using FluentAssertions;
2+
using J4JSoftware.Configuration.CommandLine;
3+
using Xunit;
4+
5+
namespace J4JSoftware.Binder.Tests
6+
{
7+
public class MiscTests : BaseTest
8+
{
9+
[ Theory ]
10+
[ InlineData( CommandLineStyle.Linux, "-x abc", "abc" ) ]
11+
[ InlineData(CommandLineStyle.Linux, "-x \"abc\"", "abc" ) ]
12+
public void StringHandling( CommandLineStyle style, string cmdLine, params string[] result )
13+
{
14+
Initialize(style);
15+
16+
var option = Options.Bind<MiscTarget, string?>( x => x.AStringValue, "x" );
17+
option.Should().NotBeNull();
18+
Options.Log.HasMessages().Should().BeFalse();
19+
20+
var parser = new Parser(Options, Logger);
21+
parser.Parse( cmdLine ).Should().BeTrue();
22+
23+
Options.UnknownKeys.Should().BeEmpty();
24+
Options.UnkeyedValues.Should().BeEmpty();
25+
26+
option!.Values.Count.Should().Be( result.Length );
27+
28+
for( var idx = 0; idx < result.Length; idx++ )
29+
{
30+
option.Values[ idx ].Should().Be( result[ idx ] );
31+
}
32+
}
33+
}
34+
}

Binder.Tests/ParserTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using J4JSoftware.Configuration.CommandLine;
8+
using Xunit;
9+
10+
namespace J4JSoftware.Binder.Tests
11+
{
12+
public class ParserTests : BaseTest
13+
{
14+
[Theory]
15+
[MemberData(nameof(TestDataSource.GetTokenizerData), MemberType = typeof(TestDataSource))]
16+
public void Tokenizer( TokenizerConfig config )
17+
{
18+
var cmdLogger = new CommandLineLogger();
19+
20+
var tokenizer = new Tokenizer(
21+
AvailableTokens.GetDefault(CommandLineStyle.Windows, cmdLogger),
22+
cmdLogger,
23+
new ConsolidateQuotedText(StringComparison.OrdinalIgnoreCase, cmdLogger),
24+
new MergeSequentialSeparators(cmdLogger) );
25+
26+
var tokens = tokenizer.Tokenize( config.CommandLine );
27+
28+
tokens.Count.Should().Be( config.Data.Count );
29+
30+
for( var idx = 0; idx < tokens.Count; idx++ )
31+
{
32+
var token = tokens[ idx ];
33+
token.Should().NotBeNull();
34+
35+
token!.Type.Should().Be( config.Data[ idx ].Type );
36+
token!.Text.Should().Be( config.Data[ idx ].Text );
37+
}
38+
}
39+
40+
[ Theory ]
41+
[ MemberData( nameof(TestDataSource.GetParserData), MemberType = typeof(TestDataSource) ) ]
42+
public void Parser( TestConfig config )
43+
{
44+
Initialize(config);
45+
46+
var cmdLogger = new CommandLineLogger();
47+
var parser = new Parser( Options, cmdLogger );
48+
49+
parser.Options.CreateOptionsFromContextKeys( config.OptionConfigurations );
50+
51+
parser.Parse( config.CommandLine ).Should().BeTrue();
52+
53+
ValidateConfiguration<BasicTarget>();
54+
}
55+
}
56+
}

Binder.Tests/SingleProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void Allocations( TestConfig config )
1212

1313
Options.CreateOptionsFromContextKeys( config.OptionConfigurations);
1414

15-
ValidateAllocations();
15+
ValidateTokenizing();
1616
}
1717

1818
[ Theory ]

Binder.Tests/new-alloc/TokenizerTests.cs

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

Binder.Tests/support/BaseTest.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Linq;
33
using System.Linq.Expressions;
44
using FluentAssertions;
5-
using J4JSoftware.CommandLine;
5+
using J4JSoftware.Configuration.CommandLine;
66
using Microsoft.Extensions.Configuration;
77
using Xunit;
88

@@ -11,12 +11,19 @@ namespace J4JSoftware.Binder.Tests
1111
public class BaseTest
1212
{
1313
protected TestConfig? TestConfig { get; private set; }
14-
protected OptionCollection Options { get; } = new();
14+
protected IOptionCollection Options { get; private set; }
15+
protected CommandLineLogger Logger => Options.Log;
16+
17+
protected void Initialize(CommandLineStyle style)
18+
{
19+
Options = new OptionCollectionNG(style);
20+
Options.Log.HasMessages().Should().BeFalse();
21+
}
1522

1623
protected void Initialize( TestConfig testConfig )
1724
{
25+
Initialize( testConfig.Style );
1826
TestConfig = testConfig;
19-
Options.Log.HasMessages().Should().BeFalse();
2027
}
2128

2229
protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelector )
@@ -40,12 +47,13 @@ protected void Bind<TTarget, TProp>( Expression<Func<TTarget, TProp>> propSelect
4047
optConfig.Option = option;
4148
}
4249

43-
protected void ValidateAllocations()
50+
protected void ValidateTokenizing()
4451
{
45-
var result = Options.Allocator.AllocateCommandLine(TestConfig!.CommandLine!, Options!);
52+
var parser = new Parser(Options, Logger);
53+
parser.Parse( TestConfig!.CommandLine ).Should().Be( Options.UnknownKeys.Count == 0 );
4654

47-
result.UnknownKeys.Count.Should().Be( TestConfig.UnknownKeys );
48-
result.UnkeyedParameters.Count.Should().Be( TestConfig.UnkeyedParameters );
55+
Options.UnknownKeys.Count.Should().Be( TestConfig.UnknownKeys );
56+
Options.UnkeyedValues.Count.Should().Be( TestConfig.UnkeyedValues );
4957

5058
foreach( var optConfig in TestConfig.OptionConfigurations )
5159
{
@@ -64,7 +72,7 @@ protected void ValidateConfiguration<TParsed>()
6472

6573
TParsed? parsed = null;
6674

67-
if( TestConfig!.OptionConfigurations.Any( x => x.ParsingWillFail ) )
75+
if( TestConfig!.OptionConfigurations.Any( x => x.ConversionWillFail ) )
6876
{
6977
var exception = Assert.Throws<InvalidOperationException>( () => config.Get<TParsed>() );
7078
return;
@@ -73,7 +81,7 @@ protected void ValidateConfiguration<TParsed>()
7381
parsed = config.Get<TParsed>();
7482

7583
if( TestConfig.OptionConfigurations.TrueForAll( x => !x.ValuesSatisfied )
76-
&& TestConfig.OptionConfigurations.All( x => x.Style != OptionStyle.Switch ) )
84+
/*&& TestConfig.OptionConfigurations.All( x => x.Style != OptionStyle.Switch )*/ )
7785
{
7886
parsed.Should().BeNull();
7987
return;
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class TestDataSource
1212
{
1313
public static IEnumerable<object[]> GetSinglePropertyData()
1414
{
15-
foreach ( var config in GetConfigurations<TestConfig>("singleProperties.json") )
15+
foreach ( var config in GetConfigurations<TestConfig>("singleProperties.json").Skip(0) )
1616
{
1717
yield return new object[] { config };
1818
}
@@ -28,17 +28,24 @@ public static IEnumerable<object[]> GetEmbeddedPropertyData()
2828

2929
public static IEnumerable<object[]> GetTokenizerData()
3030
{
31-
//yield return new object[] { GetConfigurations<TokenizerConfig>( "tokenizer.json" ).Last() };
3231
foreach (var config in GetConfigurations<TokenizerConfig>("tokenizer.json"))
3332
{
3433
yield return new object[] { config };
3534
}
3635
}
3736

37+
public static IEnumerable<object[]> GetParserData()
38+
{
39+
foreach (var config in GetConfigurations<TestConfig>("parser.json"))
40+
{
41+
yield return new object[] { config };
42+
}
43+
}
44+
3845
private static List<T> GetConfigurations<T>( string jsonFile )
3946
where T: class, new()
4047
{
41-
var text = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, jsonFile));
48+
var text = File.ReadAllText(Path.Combine(Environment.CurrentDirectory,"test-files", jsonFile));
4249

4350
return JsonSerializer.Deserialize<List<T>>(
4451
text,

Binder.Tests/test-data/OptionConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using J4JSoftware.CommandLine;
2+
using J4JSoftware.Configuration.CommandLine;
33
#pragma warning disable 8618
44

55
namespace J4JSoftware.Binder.Tests
@@ -10,7 +10,7 @@ public class OptionConfig
1010
public string ContextPath { get; set; }
1111
public OptionStyle Style { get; set; }
1212
public bool Required { get; set; }
13-
public bool ParsingWillFail { get; set; }
13+
public bool ConversionWillFail { get; set; }
1414
public bool ValuesSatisfied { get; set; }
1515
public string? CorrectText { get; set; }
1616
public List<string> CorrectTextArray { get; set; }

0 commit comments

Comments
 (0)