From c087d4a2282037c5bb2260a726bd76a410a857ba Mon Sep 17 00:00:00 2001 From: Abbas Cyclewala Date: Tue, 15 Aug 2023 19:42:39 +0530 Subject: [PATCH 1/7] Applied case sensitivity settings to KeywordHelper --- .../Parser/KeywordsHelper.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs index 087b45e5..0b227f44 100644 --- a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs @@ -21,15 +21,16 @@ internal class KeywordsHelper : IKeywordsHelper public const string FUNCTION_AS = "as"; public const string FUNCTION_CAST = "cast"; - private readonly IDictionary _keywords = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - { "true", Expression.Constant(true) }, - { "false", Expression.Constant(false) }, - { "null", Expression.Constant(null) } - }; + private readonly IDictionary _keywords; public KeywordsHelper(ParsingConfig config) { + _keywords = new Dictionary(config is { IsCaseSensitive: true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) + { + { "true", Expression.Constant(true) }, + { "false", Expression.Constant(false) }, + { "null", Expression.Constant(null) } + }; if (config.AreContextKeywordsEnabled) { _keywords.Add(KEYWORD_IT, KEYWORD_IT); From cdaf4570de2b94d7525d343e74317f52115feff6 Mon Sep 17 00:00:00 2001 From: "Abbas Cyclewala (from Dev Box)" Date: Sun, 27 Aug 2023 20:30:37 +0530 Subject: [PATCH 2/7] Added settings for Keyword case sensitivity Added test case for Keywords Helper --- .../Parser/KeywordsHelper.cs | 2 +- src/System.Linq.Dynamic.Core/ParsingConfig.cs | 5 ++ .../Parser/KeywordsHelperTests.cs | 70 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs diff --git a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs index 0b227f44..57c0089a 100644 --- a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs @@ -25,7 +25,7 @@ internal class KeywordsHelper : IKeywordsHelper public KeywordsHelper(ParsingConfig config) { - _keywords = new Dictionary(config is { IsCaseSensitive: true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) + _keywords = new Dictionary(config is { KeywordsAreCaseSensitive : true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) { { "true", Expression.Constant(true) }, { "false", Expression.Constant(false) }, diff --git a/src/System.Linq.Dynamic.Core/ParsingConfig.cs b/src/System.Linq.Dynamic.Core/ParsingConfig.cs index 6b574054..67d61bae 100644 --- a/src/System.Linq.Dynamic.Core/ParsingConfig.cs +++ b/src/System.Linq.Dynamic.Core/ParsingConfig.cs @@ -27,6 +27,11 @@ public class ParsingConfig /// Gets or sets if parameter, method, and properties resolution should be case sensitive or not (false by default). public bool IsCaseSensitive { get; set; } + /// + /// Gets or sets if keywords are case sensitive (false by default). + /// + public bool KeywordsAreCaseSensitive { get; set; } + /// /// Default ParsingConfig for CosmosDb /// diff --git a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs new file mode 100644 index 00000000..6f62405a --- /dev/null +++ b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs @@ -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; } + } + public class KeywordsHelperTests + { + + public KeywordsHelperTests() + { + + } + + private KeywordsHelper CreateKeywordsHelper(ParsingConfig config) + { + return new KeywordsHelper(config); + } + + [Theory] + [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 { KeywordsAreCaseSensitive = true}); + object type = null; + + // Act + var result = keywordsHelper.TryGetValue( + name, + out type); + + // Assert + Assert.Equal(expected,result); + } + + [Theory] + [InlineData("it", true)] + [InlineData("IT", true)] + [InlineData("TestClass", true)] + [InlineData("testClass", true)] + [InlineData("nonExisting", false)] + public void TryGetValue_WithCaseInSensitive_ReturnsResultAsExpected(string name, bool expected) + { + // Arrange + var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { KeywordsAreCaseSensitive = false }); + object type = null; + + // Act + var result = keywordsHelper.TryGetValue( + name, + out type); + + // Assert + Assert.Equal(expected, result); + } + } +} From eeabc9654014571659224e75307934ca59ad4824 Mon Sep 17 00:00:00 2001 From: "Abbas Cyclewala (from Dev Box)" Date: Sun, 27 Aug 2023 20:33:56 +0530 Subject: [PATCH 3/7] Renamed new config param to match style --- src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs | 2 +- src/System.Linq.Dynamic.Core/ParsingConfig.cs | 2 +- .../Parser/KeywordsHelperTests.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs index 57c0089a..17037202 100644 --- a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs @@ -25,7 +25,7 @@ internal class KeywordsHelper : IKeywordsHelper public KeywordsHelper(ParsingConfig config) { - _keywords = new Dictionary(config is { KeywordsAreCaseSensitive : true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) + _keywords = new Dictionary(config is { AreKeywordsCaseSensitive : true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) { { "true", Expression.Constant(true) }, { "false", Expression.Constant(false) }, diff --git a/src/System.Linq.Dynamic.Core/ParsingConfig.cs b/src/System.Linq.Dynamic.Core/ParsingConfig.cs index 67d61bae..e51c8376 100644 --- a/src/System.Linq.Dynamic.Core/ParsingConfig.cs +++ b/src/System.Linq.Dynamic.Core/ParsingConfig.cs @@ -30,7 +30,7 @@ public class ParsingConfig /// /// Gets or sets if keywords are case sensitive (false by default). /// - public bool KeywordsAreCaseSensitive { get; set; } + public bool AreKeywordsCaseSensitive { get; set; } /// /// Default ParsingConfig for CosmosDb diff --git a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs index 6f62405a..a54769c2 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs @@ -34,7 +34,7 @@ private KeywordsHelper CreateKeywordsHelper(ParsingConfig config) public void TryGetValue_WithCaseSensitive_ReturnsResultAsExpected(string name, bool expected) { // Arrange - var keywordsHelper = this.CreateKeywordsHelper( new ParsingConfig { KeywordsAreCaseSensitive = true}); + var keywordsHelper = this.CreateKeywordsHelper( new ParsingConfig { AreKeywordsCaseSensitive = true}); object type = null; // Act @@ -55,7 +55,7 @@ public void TryGetValue_WithCaseSensitive_ReturnsResultAsExpected(string name, b public void TryGetValue_WithCaseInSensitive_ReturnsResultAsExpected(string name, bool expected) { // Arrange - var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { KeywordsAreCaseSensitive = false }); + var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { AreKeywordsCaseSensitive = false }); object type = null; // Act From 662b8ea31b638590a5f73220671eada2a7e6570d Mon Sep 17 00:00:00 2001 From: "Abbas Cyclewala (from Dev Box)" Date: Sat, 25 Nov 2023 15:43:32 +0530 Subject: [PATCH 4/7] fixed merge conflicts --- .../Parser/KeywordsHelper.cs | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs index b6e852d1..1913c5c9 100644 --- a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs @@ -26,24 +26,7 @@ internal class KeywordsHelper : IKeywordsHelper private readonly ParsingConfig _config; // Keywords are IgnoreCase - private readonly Dictionary _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 _keywordMapping; // PreDefined Types are not IgnoreCase private static readonly Dictionary _preDefinedTypeMapping = new(); @@ -64,6 +47,25 @@ public KeywordsHelper(ParsingConfig config) { _config = Check.NotNull(config); + _keywordMapping = new(config is { AreKeywordsCaseSensitive: true } ? 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); From e6ab3034dc47f607c26e9c37a5b7f851c3ea6df7 Mon Sep 17 00:00:00 2001 From: "Abbas Cyclewala (from Dev Box)" Date: Sat, 25 Nov 2023 15:53:09 +0530 Subject: [PATCH 5/7] fixed broken test case --- .../Parser/KeywordsHelperTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs index a54769c2..4ae18b8d 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs @@ -50,7 +50,7 @@ public void TryGetValue_WithCaseSensitive_ReturnsResultAsExpected(string name, b [InlineData("it", true)] [InlineData("IT", true)] [InlineData("TestClass", true)] - [InlineData("testClass", true)] + [InlineData("testClass", false)] [InlineData("nonExisting", false)] public void TryGetValue_WithCaseInSensitive_ReturnsResultAsExpected(string name, bool expected) { From 63be89c257d21404a2635c5d53d30b66fca6a988 Mon Sep 17 00:00:00 2001 From: "Abbas Cyclewala (from Dev Box)" Date: Sat, 13 Jan 2024 13:41:04 +0530 Subject: [PATCH 6/7] fixed review comments --- src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs | 2 +- .../Parser/KeywordsHelperTests.cs | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs index 1913c5c9..5b865caa 100644 --- a/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs @@ -47,7 +47,7 @@ public KeywordsHelper(ParsingConfig config) { _config = Check.NotNull(config); - _keywordMapping = new(config is { AreKeywordsCaseSensitive: true } ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) + _keywordMapping = new(config.AreKeywordsCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase) { { "true", Expression.Constant(true) }, { "false", Expression.Constant(false) }, diff --git a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs index 4ae18b8d..f9197ae1 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs @@ -12,12 +12,11 @@ public class TestClass { public string Hello { get; set; } } + public class KeywordsHelperTests { - public KeywordsHelperTests() { - } private KeywordsHelper CreateKeywordsHelper(ParsingConfig config) @@ -56,12 +55,10 @@ public void TryGetValue_WithCaseInSensitive_ReturnsResultAsExpected(string name, { // Arrange var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { AreKeywordsCaseSensitive = false }); - object type = null; + object type; // Act - var result = keywordsHelper.TryGetValue( - name, - out type); + var result = keywordsHelper.TryGetValue(name,out type); // Assert Assert.Equal(expected, result); From cb5622c30d3c90e61eea35176bd9138ea598b948 Mon Sep 17 00:00:00 2001 From: "Abbas Cyclewala (from Dev Box)" Date: Sat, 13 Jan 2024 13:47:37 +0530 Subject: [PATCH 7/7] updated to single test case --- .../Parser/KeywordsHelperTests.cs | 40 ++++++------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs index f9197ae1..2bdd1a6c 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Parser/KeywordsHelperTests.cs @@ -25,36 +25,20 @@ private KeywordsHelper CreateKeywordsHelper(ParsingConfig config) } [Theory] - [InlineData("it",true)] - [InlineData("IT", false)] - [InlineData("TestClass",true)] - [InlineData("testClass", false)] - [InlineData("nonExisting", false)] - public void TryGetValue_WithCaseSensitive_ReturnsResultAsExpected(string name, bool expected) + [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) { // Arrange - var keywordsHelper = this.CreateKeywordsHelper( new ParsingConfig { AreKeywordsCaseSensitive = true}); - object type = null; - - // Act - var result = keywordsHelper.TryGetValue( - 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 }); + var keywordsHelper = this.CreateKeywordsHelper(new ParsingConfig { AreKeywordsCaseSensitive = areKeywordsCaseSensitive }); object type; // Act