Skip to content

Commit 75c7581

Browse files
committed
Including a comment wiht the regex pattern when translating Regex constants
1 parent 9fef60d commit 75c7581

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

ReadableExpressions.UnitTests/WhenTranslatingConstants.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Linq;
88
using System.Linq.Expressions;
9+
using System.Text.RegularExpressions;
910
using Microsoft.VisualStudio.TestTools.UnitTesting;
1011

1112
[TestClass]
@@ -295,7 +296,7 @@ public void ShouldTranslateADefaultString()
295296
}
296297

297298
[TestMethod]
298-
public void ShouldEscapteTranslatedStrings()
299+
public void ShouldEscapeTranslatedStrings()
299300
{
300301
var stringConstant = Expression.Constant("Escape: \"THIS\"!");
301302

@@ -314,6 +315,16 @@ public void ShouldTranslateADefaultGuid()
314315
Assert.AreEqual("default(Guid)", translated);
315316
}
316317

318+
[TestMethod]
319+
public void ShouldTranslateARegex()
320+
{
321+
var regexConstant = Expression.Constant(new Regex("^[0-9]+$"));
322+
323+
var translated = regexConstant.ToReadableString();
324+
325+
Assert.AreEqual("Regex /* ^[0-9]+$ */", translated);
326+
}
327+
317328
[TestMethod]
318329
public void ShouldTranslateAParameterlessFunc()
319330
{

ReadableExpressions/Translators/ConstantExpressionTranslator.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private static bool TryTranslateByTypeCode(
8787
if (IsType(constant, out translation) ||
8888
IsLambda(constant, context, out translation) ||
8989
IsFunc(constant, out translation) ||
90+
IsRegex(constant, out translation) ||
9091
IsDefault<Guid>(constant, out translation) ||
9192
IsTimeSpan(constant, out translation))
9293
{
@@ -261,6 +262,18 @@ private static bool IsLambda(
261262
return false;
262263
}
263264

265+
private static bool IsRegex(ConstantExpression constant, out string translation)
266+
{
267+
if (constant.Type == typeof(Regex))
268+
{
269+
translation = nameof(Regex) + " /* " + constant.Value + " */";
270+
return true;
271+
}
272+
273+
translation = null;
274+
return false;
275+
}
276+
264277
private static readonly Regex _funcMatcher = new Regex(@"^System\.(?:Func|Action)`\d+\[.+\]$");
265278

266279
private static bool IsFunc(ConstantExpression constant, out string translation)

0 commit comments

Comments
 (0)