Skip to content

Commit 16d0eb5

Browse files
authored
Add some more unit tests for DynamicExpressionParser.ParseLambda (#985)
1 parent e22a370 commit 16d0eb5

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ internal class KeywordsHelper : IKeywordsHelper
2929
private readonly Dictionary<string, AnyOf<string, Expression, Type>> _mappings;
3030

3131
// Pre-defined Types are not IgnoreCase
32-
private static readonly Dictionary<string, Type> PreDefinedTypeMapping = new();
32+
private static readonly Dictionary<string, Type> PreDefinedTypeMapping = [];
3333

3434
// Custom DefinedTypes are not IgnoreCase
35-
private readonly Dictionary<string, Type> _customTypeMapping = new();
35+
private readonly Dictionary<string, Type> _customTypeMapping = [];
3636

3737
static KeywordsHelper()
3838
{

test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public int this[int i1]
8282
get => i1 + "-" + i2;
8383
}
8484
}
85-
85+
8686
private class ComplexParseLambda1Result
8787
{
8888
public int? Age;
@@ -730,7 +730,7 @@ public void DynamicExpressionParser_ParseLambda_Indexer1D()
730730
// Assert
731731
Check.That(result).Equals(4);
732732
}
733-
733+
734734
[Fact]
735735
public void DynamicExpressionParser_ParseLambda_Indexer2D()
736736
{
@@ -757,7 +757,7 @@ public void DynamicExpressionParser_ParseLambda_Indexer2D()
757757
// Assert
758758
Check.That(result).Equals("3-1");
759759
}
760-
760+
761761
[Fact]
762762
public void DynamicExpressionParser_ParseLambda_IndexerParameterMismatch()
763763
{
@@ -772,11 +772,32 @@ public void DynamicExpressionParser_ParseLambda_IndexerParameterMismatch()
772772
{
773773
Expression.Parameter(typeof(ClassWithIndexers), "myObj")
774774
};
775-
775+
776776
Assert.Throws<ParseException>(() =>
777777
DynamicExpressionParser.ParseLambda(config, false, expressionParams, null, "myObj[3,\"1\",1]"));
778778
}
779779

780+
[Fact]
781+
public void DynamicExpressionParser_ParseLambda_Int16_CaseSensitive_IsValid()
782+
{
783+
// Act
784+
var lambda = DynamicExpressionParser.ParseLambda(typeof(object), null, "Int16(5)");
785+
var result = lambda.Compile().DynamicInvoke("x");
786+
787+
// Assert
788+
result.Should().Be(5);
789+
}
790+
791+
[Fact]
792+
public void DynamicExpressionParser_ParseLambda_Int16_CaseInsensitive_Throws()
793+
{
794+
// Act
795+
Action act = () => DynamicExpressionParser.ParseLambda(typeof(object), null, "int16(5)");
796+
797+
// Assert
798+
act.Should().Throw<ParseException>();
799+
}
800+
780801
[Fact]
781802
public void DynamicExpressionParser_ParseLambda_DuplicateParameterNames_ThrowsException()
782803
{
@@ -989,7 +1010,7 @@ public void DynamicExpressionParser_ParseLambda_HexToLong(string expression, lon
9891010
var parameters = Array.Empty<ParameterExpression>();
9901011

9911012
// Act
992-
var lambda = DynamicExpressionParser.ParseLambda( parameters, typeof(long), expression);
1013+
var lambda = DynamicExpressionParser.ParseLambda(parameters, typeof(long), expression);
9931014
var result = lambda.Compile().DynamicInvoke();
9941015

9951016
// Assert
@@ -2372,6 +2393,6 @@ public DefaultDynamicLinqCustomTypeProviderForGenericExtensionMethod() : base(Pa
23722393
}
23732394

23742395
public override HashSet<Type> GetCustomTypes() =>
2375-
[..base.GetCustomTypes(), typeof(Methods), typeof(MethodsItemExtension)];
2396+
[.. base.GetCustomTypes(), typeof(Methods), typeof(MethodsItemExtension)];
23762397
}
23772398
}

0 commit comments

Comments
 (0)