Skip to content

Commit ceb77cd

Browse files
committed
Added test to check that options can be bound to properties without setters provided they're initialized.
1 parent 50c9127 commit ceb77cd

File tree

6 files changed

+80
-9
lines changed

6 files changed

+80
-9
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace J4JSoftware.Binder.Tests
5+
{
6+
public class EmbeddedPropertiesNoSetter : BaseTest
7+
{
8+
[ Theory ]
9+
[ MemberData( nameof(TestDataSource.GetEmbeddedPropertyData), MemberType = typeof(TestDataSource) ) ]
10+
public void Allocations( TestConfig config )
11+
{
12+
Initialize( config );
13+
14+
Bind<EmbeddedTargetNoSetter, bool>( x => x.Target1.ASwitch );
15+
Bind<EmbeddedTargetNoSetter, string>( x => x.Target1.ASingleValue );
16+
Bind<EmbeddedTargetNoSetter, TestEnum>( x => x.Target1.AnEnumValue );
17+
Bind<EmbeddedTargetNoSetter, TestFlagEnum>( x => x.Target1.AFlagEnumValue );
18+
Bind<EmbeddedTargetNoSetter, List<string>>( x => x.Target1.ACollection );
19+
20+
ValidateTokenizing();
21+
}
22+
23+
[ Theory ]
24+
[ MemberData( nameof(TestDataSource.GetEmbeddedPropertyData), MemberType = typeof(TestDataSource) ) ]
25+
public void Parsing( TestConfig config )
26+
{
27+
Initialize( config );
28+
29+
Bind<EmbeddedTargetNoSetter, bool>( x => x.Target1.ASwitch );
30+
Bind<EmbeddedTargetNoSetter, string>( x => x.Target1.ASingleValue );
31+
Bind<EmbeddedTargetNoSetter, TestEnum>( x => x.Target1.AnEnumValue );
32+
Bind<EmbeddedTargetNoSetter, TestFlagEnum>( x => x.Target1.AFlagEnumValue );
33+
Bind<EmbeddedTargetNoSetter, List<string>>( x => x.Target1.ACollection );
34+
35+
ValidateConfiguration<EmbeddedTargetNoSetter>();
36+
}
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace J4JSoftware.Binder.Tests
4+
{
5+
public class BasicTargetParameteredCtor
6+
{
7+
private readonly int _value;
8+
9+
public BasicTargetParameteredCtor( int value )
10+
{
11+
_value = value;
12+
}
13+
14+
public bool ASwitch { get; set; }
15+
public string ASingleValue { get; set; }
16+
public List<string> ACollection { get; set; }
17+
public TestEnum AnEnumValue { get; set; }
18+
public TestFlagEnum AFlagEnumValue { get; set; }
19+
}
20+
}

Binder.Tests/results/EmbeddedTarget.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma warning disable 8618
2+
#pragma warning disable 8618
23

34
namespace J4JSoftware.Binder.Tests
45
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma warning disable 8618
2+
3+
namespace J4JSoftware.Binder.Tests
4+
{
5+
public class EmbeddedTargetNoSetter
6+
{
7+
public BasicTargetParameteredCtor Target1 { get; } = new( 0 );
8+
public BasicTargetParameteredCtor Target2 { get; } = new( 0 );
9+
}
10+
}

Binder/options/OptionCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ private bool ValidatePropertyInfo( PropertyInfo propInfo, bool isOuterMostLeaf =
165165

166166
var piEntry = ((ValidationEntry<PropertyInfo>)piContext.Current)
167167
.HasSupportedGetter();
168-
169-
var typeEntry = piEntry.CreateChild(piEntry.Value.PropertyType)
170-
.IsSupportedType();
168+
169+
var typeEntry = piEntry.CreateChild( piEntry.Value.PropertyType )
170+
.IsSupportedType( isOuterMostLeaf );
171171

172172
if( isOuterMostLeaf )
173173
{

Binder/options/PropertyValidators.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public static ValidationEntry<PropertyInfo> HasSupportedSetter( this ValidationE
4242
return entry;
4343
}
4444

45-
public static ValidationEntry<Type> IsSupportedType( this ValidationEntry<Type> entry )
45+
public static ValidationEntry<Type> IsSupportedType( this ValidationEntry<Type> entry, bool isOuterMostLeaf = false )
4646
{
4747
if( entry.Value.IsGenericType )
4848
entry.IsSupportedGenericType();
49-
else entry.IsSupportedNonGenericType();
49+
else entry.IsSupportedNonGenericType( isOuterMostLeaf );
5050

5151
return entry;
5252
}
@@ -63,7 +63,7 @@ public static ValidationEntry<Type> IsSupportedGenericType( this ValidationEntry
6363
var typeParamEntry =
6464
entry.CreateChild<Type>( entry.Value.GetGenericArguments()[ 0 ], "from generic type check" );
6565

66-
typeParamEntry.IsSupportedNonGenericType();
66+
typeParamEntry.IsSupportedNonGenericType( true );
6767

6868
if (!typeof(List<>).MakeGenericType(entry.Value.GenericTypeArguments[0]).IsAssignableFrom(entry.Value))
6969
entry.AddError("Generic type is not a List<> type");
@@ -72,7 +72,7 @@ public static ValidationEntry<Type> IsSupportedGenericType( this ValidationEntry
7272
return entry;
7373
}
7474

75-
public static ValidationEntry<Type> IsSupportedNonGenericType( this ValidationEntry<Type> entry )
75+
public static ValidationEntry<Type> IsSupportedNonGenericType( this ValidationEntry<Type> entry, bool isOuterMostLeaf = false )
7676
{
7777
if( entry.Value.IsGenericType )
7878
entry.AddError( "Type must be non-generic" );
@@ -87,8 +87,10 @@ public static ValidationEntry<Type> IsSupportedNonGenericType( this ValidationEn
8787
}
8888
else
8989
{
90-
if( entry.Value.IsValueType
91-
|| typeof(string).IsAssignableFrom( entry.Value )
90+
if( entry.Value.IsValueType || typeof(string).IsAssignableFrom( entry.Value ) )
91+
return entry;
92+
93+
if( !isOuterMostLeaf
9294
|| entry.Value.GetConstructors().Any( c => !c.GetParameters().Any() ) )
9395
return entry;
9496

0 commit comments

Comments
 (0)