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.AreKeywordsCaseSensitive ? StringComparer.Ordinal : 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 }
};

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
51 changes: 51 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,51 @@
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
{
public KeywordsHelperTests()
{
}

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

[Theory]
abbasc52 marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("it", false, true)]
[InlineData("IT", false, true)]
[InlineData("TestClass", false, true)]
[InlineData("testClass", false, false)]
[InlineData("nonExisting", false, false)]
[InlineData("it", true, true)]
[InlineData("IT", true, false)]
[InlineData("TestClass", true, true)]
[InlineData("testClass", true, false)]
[InlineData("nonExisting", true ,false)]
public void TryGetValue_WithCaseSensitiveSettings_ReturnsResultAsExpected(string name, bool areKeywordsCaseSensitive,bool expected)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do a format file to align the source code (adding some spaces)

{
// Arrange
var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { AreKeywordsCaseSensitive = areKeywordsCaseSensitive });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this i not needed

object type;

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

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