Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/DynamicExpresso.Core/Detector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ internal class Detector
private static readonly Regex RootIdentifierDetectionRegex =
new Regex(@"(?<=[^\w@]|^)(?<id>@?[\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@]|^)(?<id>@?[\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("<id>", "<type>");

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/DynamicExpresso.Core/DetectorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
43 changes: 30 additions & 13 deletions test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using DynamicExpresso.Exceptions;

namespace DynamicExpresso.UnitTest
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<ParseException>());
}
}
}
Loading