diff --git a/src/DynamicExpresso.Core/Detector.cs b/src/DynamicExpresso.Core/Detector.cs index 3d58e01..d3625dc 100644 --- a/src/DynamicExpresso.Core/Detector.cs +++ b/src/DynamicExpresso.Core/Detector.cs @@ -14,11 +14,6 @@ internal class Detector private static readonly Regex RootIdentifierDetectionRegex = new Regex(@"(?<=[^\w@]|^)(?@?[\p{L}\p{Nl}_][\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}_]*)", RegexOptions.Compiled); - private static readonly Regex ChildIdentifierDetectionRegex = new Regex( - @"(?<=[^\w@]|^)(?@?[\p{L}\p{Nl}_][\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}_]*(\.[\p{L}\p{Nl}_][\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}_]*)*)", - RegexOptions.Compiled); - - private static readonly string Id = RootIdentifierDetectionRegex.ToString(); private static readonly string Type = Id.Replace("", ""); @@ -80,11 +75,7 @@ public IdentifiersInfo DetectIdentifiers(string expression, DetectorOptions opti } } - var identifierRegex = option == DetectorOptions.IncludeChildren - ? ChildIdentifierDetectionRegex - : RootIdentifierDetectionRegex; - - foreach (Match match in identifierRegex.Matches(expression)) + foreach (Match match in RootIdentifierDetectionRegex.Matches(expression)) { var idGroup = match.Groups["id"]; var identifier = idGroup.Value; diff --git a/src/DynamicExpresso.Core/DetectorOptions.cs b/src/DynamicExpresso.Core/DetectorOptions.cs index 41a17b5..da1a8f1 100644 --- a/src/DynamicExpresso.Core/DetectorOptions.cs +++ b/src/DynamicExpresso.Core/DetectorOptions.cs @@ -6,6 +6,7 @@ namespace DynamicExpresso public enum DetectorOptions { None = 0, + [System.Obsolete("IncludeChildren is deprecated and will be removed in a future version.")] IncludeChildren = 1 } } diff --git a/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs b/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs index 9f29b23..8556b3e 100644 --- a/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs +++ b/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using NUnit.Framework; +using DynamicExpresso.Exceptions; namespace DynamicExpresso.UnitTest { @@ -43,19 +44,6 @@ public void Detect_unknown_identifiers() Is.EqualTo(new[] { "x", "y" })); } - [Test] - public void Detect_unknown_identifiers_with_complete_variable_name() - { - var target = new Interpreter(); - - var detectedIdentifiers = target.DetectIdentifiers("Contact.Personal.Year_of_birth = 1987", - DetectorOptions.IncludeChildren); - - Assert.That( - detectedIdentifiers.UnknownIdentifiers, - Is.EqualTo(new[] { "Contact.Personal.Year_of_birth" })); - } - [Test] public void Should_detect_various_format_of_identifiers() { @@ -356,5 +344,34 @@ public void Dont_detect_numbers_with_suffix(string code) var detectedIdentifiers = target.DetectIdentifiers(code); Assert.That(detectedIdentifiers.UnknownIdentifiers, Is.Empty); } + + class TestClass + { + public int Age { get; set; } + public string Name { get; set; } + } + + [Test] + public void Detect_class_members_invocations() + { + TestClass customer = new TestClass() { Age = 1, Name = "abc" }; + Interpreter target = new Interpreter(); + target.SetVariable("test", customer); + + // Known properties + IdentifiersInfo detectedIdentifiers = target.DetectIdentifiers("test.Name"); + Assert.That(detectedIdentifiers.UnknownIdentifiers, Is.Empty); + Assert.That(detectedIdentifiers.Identifiers.Count(), Is.EqualTo(1)); + Assert.That(detectedIdentifiers.Identifiers.ElementAt(0).Name, Is.EqualTo("test")); + Assert.That(detectedIdentifiers.Identifiers.ElementAt(0).Expression.Type, Is.EqualTo(typeof(TestClass))); + Assert.That(target.Eval("test.Name"), Is.EqualTo("abc")); + + // Unknown properties, for now we don't detect them, eval how to support this for the future + detectedIdentifiers = target.DetectIdentifiers("test.Unknown"); + Assert.That(detectedIdentifiers.UnknownIdentifiers.Count(), Is.EqualTo(0)); + Assert.That(detectedIdentifiers.Identifiers.Count(), Is.EqualTo(1)); + Assert.That(detectedIdentifiers.Identifiers.ElementAt(0).Name, Is.EqualTo("test")); + Assert.That(() => target.Eval("test.Unknown"), Throws.TypeOf()); + } } }