Skip to content

Commit e061531

Browse files
committed
ref keyword ok + inline init
1 parent c0994f4 commit e061531

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

+86
Original file line numberDiff line numberDiff line change
@@ -2285,5 +2285,91 @@ public void MethodWithOutParameterInlineVarDeclaration()
22852285
}
22862286

22872287
#endregion
2288+
2289+
#region Method with ref parameter
2290+
2291+
[Test]
2292+
[Category("RefKeywordMethod")]
2293+
public void MethodWithRefParameter()
2294+
{
2295+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2296+
2297+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2298+
2299+
evaluator.Variables["x"] = 3;
2300+
2301+
evaluator.Evaluate("refObj.AddOneOnRef(ref x)");
2302+
2303+
evaluator.Variables.ShouldContainKey("x");
2304+
evaluator.Variables["x"]
2305+
.ShouldBeOfType<int>()
2306+
.ShouldBe(4);
2307+
}
2308+
2309+
[Test]
2310+
[Category("RefKeywordMethod")]
2311+
public void MethodWithRefParameterWithoutExistingVariable()
2312+
{
2313+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2314+
2315+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2316+
2317+
evaluator.Evaluate("refObj.AddOneOnRef(ref x)");
2318+
2319+
evaluator.Variables.ShouldContainKey("x");
2320+
evaluator.Variables["x"]
2321+
.ShouldBeOfType<int>()
2322+
.ShouldBe(1);
2323+
}
2324+
2325+
[Test]
2326+
[Category("RefKeywordMethod")]
2327+
public void MethodWithRefParameterInlineVarDeclaration()
2328+
{
2329+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2330+
2331+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2332+
2333+
evaluator.Evaluate("refObj.AddOneOnRef(ref int x)");
2334+
2335+
evaluator.Variables.ShouldContainKey("x");
2336+
evaluator.Variables["x"]
2337+
.ShouldBeOfType<StronglyTypedVariable>()
2338+
.Value.ShouldBe(1);
2339+
}
2340+
2341+
[Test]
2342+
[Category("RefKeywordMethod")]
2343+
public void MethodWithRefParameterWithoutExistingVariableAndInit()
2344+
{
2345+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2346+
2347+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2348+
2349+
evaluator.Evaluate("refObj.AddOneOnRef(ref x = 4)");
2350+
2351+
evaluator.Variables.ShouldContainKey("x");
2352+
evaluator.Variables["x"]
2353+
.ShouldBeOfType<int>()
2354+
.ShouldBe(5);
2355+
}
2356+
2357+
[Test]
2358+
[Category("RefKeywordMethod")]
2359+
public void MethodWithRefParameterInlineVarDeclarationAndInit()
2360+
{
2361+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
2362+
2363+
evaluator.Variables["refObj"] = new MethodArgKeywordClass();
2364+
2365+
evaluator.Evaluate("refObj.AddOneOnRef(ref int x = 6)");
2366+
2367+
evaluator.Variables.ShouldContainKey("x");
2368+
evaluator.Variables["x"]
2369+
.ShouldBeOfType<StronglyTypedVariable>()
2370+
.Value.ShouldBe(7);
2371+
}
2372+
2373+
#endregion
22882374
}
22892375
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CodingSeb.ExpressionEvaluator.Tests
2+
{
3+
public class MethodArgKeywordClass
4+
{
5+
public void AddOneOnRef(ref int i)
6+
{
7+
i++;
8+
}
9+
}
10+
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public partial class ExpressionEvaluator
5353
protected static readonly Regex lambdaExpressionRegex = new Regex(@"^(?>\s*)(?<args>((?>\s*)[(](?>\s*)([\p{L}_](?>[\p{L}_0-9]*)(?>\s*)([,](?>\s*)[\p{L}_][\p{L}_0-9]*(?>\s*))*)?[)])|[\p{L}_](?>[\p{L}_0-9]*))(?>\s*)=>(?<expression>.*)$", RegexOptions.Singleline | RegexOptions.Compiled);
5454
protected static readonly Regex lambdaArgRegex = new Regex(@"[\p{L}_](?>[\p{L}_0-9]*)", RegexOptions.Compiled);
5555
protected static readonly Regex initInNewBeginningRegex = new Regex(@"^(?>\s*){", RegexOptions.Compiled);
56-
protected static readonly Regex functionArgKeywordsRegex = new Regex(@"^\s*(?<keyword>out|ref)\s+((?<typeName>[\p{L}_][\p{L}_0-9\.\[\]<>]*[?]?)\s+(?=[\p{L}_]))?(?<varName>[\p{L}_](?>[\p{L}_0-9]*))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
56+
protected static readonly Regex functionArgKeywordsRegex = new Regex(@"^\s*(?<keyword>out|ref)\s+((?<typeName>[\p{L}_][\p{L}_0-9\.\[\]<>]*[?]?)\s+(?=[\p{L}_]))?(?<toEval>(?<varName>[\p{L}_](?>[\p{L}_0-9]*))\s*(=.*)?)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
5757

5858
// Depending on OptionInlineNamespacesEvaluationActive. Initialized in constructor
5959
protected string InstanceCreationWithNewKeywordRegexPattern { get { return @"^new(?>\s*)((?<isAnonymous>[{{])|((?<name>[\p{L}_][\p{L}_0-9"+ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) + @"]*)(?>\s*)(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?>\s*)((?<isfunction>[(])|(?<isArray>\[)|(?<isInit>[{{]))?))"; } }
@@ -1919,7 +1919,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
19191919
variables[outOrRefArg.VariableName] = null;
19201920
}
19211921

1922-
argValue = Evaluate(outOrRefArg.VariableName);
1922+
argValue = Evaluate(functionArgKeywordsMatch.Groups["toEval"].Value);
19231923
}
19241924
else
19251925
{

0 commit comments

Comments
 (0)