Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applied case sensitivity settings to KeywordsHelper #735

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
38 changes: 20 additions & 18 deletions src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,7 @@ internal class KeywordsHelper : IKeywordsHelper
private readonly ParsingConfig _config;

// Keywords are IgnoreCase
private readonly Dictionary<string, object> _keywordMapping = new(StringComparer.OrdinalIgnoreCase)
{
{ "true", Expression.Constant(true) },
{ "false", Expression.Constant(false) },
{ "null", Expression.Constant(null) },

{ SYMBOL_IT, SYMBOL_IT },
{ SYMBOL_PARENT, SYMBOL_PARENT },
{ SYMBOL_ROOT, SYMBOL_ROOT },

{ FUNCTION_IIF, FUNCTION_IIF },
{ FUNCTION_ISNULL, FUNCTION_ISNULL },
{ FUNCTION_NEW, FUNCTION_NEW },
{ FUNCTION_NULLPROPAGATION, FUNCTION_NULLPROPAGATION },
{ FUNCTION_IS, FUNCTION_IS },
{ FUNCTION_AS, FUNCTION_AS },
{ FUNCTION_CAST, FUNCTION_CAST }
};
private readonly Dictionary<string, object> _keywordMapping;

// PreDefined Types are not IgnoreCase
private static readonly Dictionary<string, object> _preDefinedTypeMapping = new();
Expand All @@ -64,6 +47,25 @@ public KeywordsHelper(ParsingConfig config)
{
_config = Check.NotNull(config);

_keywordMapping = new(config is { AreKeywordsCaseSensitive: true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase)
abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
{
{ "true", Expression.Constant(true) },
{ "false", Expression.Constant(false) },
{ "null", Expression.Constant(null) },

{ SYMBOL_IT, SYMBOL_IT },
{ SYMBOL_PARENT, SYMBOL_PARENT },
{ SYMBOL_ROOT, SYMBOL_ROOT },

{ FUNCTION_IIF, FUNCTION_IIF },
{ FUNCTION_ISNULL, FUNCTION_ISNULL },
{ FUNCTION_NEW, FUNCTION_NEW },
{ FUNCTION_NULLPROPAGATION, FUNCTION_NULLPROPAGATION },
{ FUNCTION_IS, FUNCTION_IS },
{ FUNCTION_AS, FUNCTION_AS },
{ FUNCTION_CAST, FUNCTION_CAST }
};

if (config.AreContextKeywordsEnabled)
{
_keywordMapping.Add(KEYWORD_IT, KEYWORD_IT);
Expand Down
5 changes: 5 additions & 0 deletions src/System.Linq.Dynamic.Core/ParsingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class ParsingConfig
/// <summary>Gets or sets if parameter, method, and properties resolution should be case sensitive or not (false by default).</summary>
public bool IsCaseSensitive { get; set; }

/// <summary>
/// Gets or sets if keywords are case sensitive (false by default).
/// </summary>
public bool AreKeywordsCaseSensitive { get; set; }

/// <summary>
/// Default ParsingConfig for CosmosDb
/// </summary>
Expand Down
70 changes: 70 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Moq;
using System;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Dynamic.Core.Parser;
using Xunit;

namespace System.Linq.Dynamic.Core.Tests.Parser
{
[DynamicLinqType]
public class TestClass
{
public string Hello { get; set; }
}
abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
public class KeywordsHelperTests
{

abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
public KeywordsHelperTests()
{

abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
}

private KeywordsHelper CreateKeywordsHelper(ParsingConfig config)
{
return new KeywordsHelper(config);
}

[Theory]
abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("it",true)]
[InlineData("IT", false)]
[InlineData("TestClass",true)]
[InlineData("testClass", false)]
[InlineData("nonExisting", false)]
public void TryGetValue_WithCaseSensitive_ReturnsResultAsExpected(string name, bool expected)
{
// Arrange
var keywordsHelper = this.CreateKeywordsHelper( new ParsingConfig { AreKeywordsCaseSensitive = true});
object type = null;

// Act
var result = keywordsHelper.TryGetValue(
abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
name,
out type);

// Assert
Assert.Equal(expected,result);
}

[Theory]
[InlineData("it", true)]
[InlineData("IT", true)]
[InlineData("TestClass", true)]
[InlineData("testClass", false)]
[InlineData("nonExisting", false)]
public void TryGetValue_WithCaseInSensitive_ReturnsResultAsExpected(string name, bool expected)
{
// Arrange
var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { AreKeywordsCaseSensitive = false });
object type = null;

// Act
var result = keywordsHelper.TryGetValue(
name,
out type);

// Assert
Assert.Equal(expected, result);
}
}
}