Skip to content

Use RegexGenerator in applicable tests #66179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9043494
Clean up some whitespace
Clockwork-Muse Nov 30, 2021
164a470
Use RegexGenerator for System.Diagnostics.DiagnosicSource
Clockwork-Muse Nov 30, 2021
ac0bf8a
Add RegGen for Microsoft.Extensions.Logging
Clockwork-Muse Nov 30, 2021
c3287fa
whitespace/formatting corrections
Clockwork-Muse Nov 30, 2021
ed1c97e
Add RegGen for Microsoft.Extensions.Logging.Console
Clockwork-Muse Nov 30, 2021
697104f
Correct test style to using
Clockwork-Muse Dec 6, 2021
93425a9
Remove use of some regex that wasn't required.
Clockwork-Muse Dec 6, 2021
9429797
Add RegGen to System.Diagnostics.Tracing
Clockwork-Muse Dec 6, 2021
b7ed7d5
Remove regex from System.IO.FileSystem
Clockwork-Muse Dec 9, 2021
685ae94
Remove unused using from System.IO.Pipelines
Clockwork-Muse Dec 9, 2021
7b2aebe
Add RegGen to System.IO.Ports
Clockwork-Muse Dec 12, 2021
99acb79
Add RegGen to System.Linq.Expressions.
Clockwork-Muse Dec 12, 2021
4fa0e91
Use RegGen in System.Net.Mail
Clockwork-Muse Dec 12, 2021
d0f6641
Add RegGen to System.Net.Sockets
Clockwork-Muse Dec 13, 2021
b76f3c5
Correct formatting.
Clockwork-Muse Dec 13, 2021
c9e37fd
Add RegGen to System.Private.DataContractSerialization
Clockwork-Muse Dec 13, 2021
fea9507
Correct formatting/whitespace
Clockwork-Muse Dec 13, 2021
f29abc9
Add RegGen to System.Runtime tests
Clockwork-Muse Dec 13, 2021
f80b4b9
Remove Regex from System.Runtime.Extensions
Clockwork-Muse Dec 13, 2021
1888dbd
Clean up System.Configuration.ConfigurationManager
Clockwork-Muse Feb 14, 2022
1fcc6e3
Clean up System.Diagnostics.StackTrace
Clockwork-Muse Feb 14, 2022
6b90501
Add RegGen to System.Threading.Tasks
Clockwork-Muse Feb 21, 2022
f6eb39f
Revert "Use RegexGenerator for System.Diagnostics.DiagnosicSource"
Clockwork-Muse Mar 5, 2022
a8f9064
Revert "Clean up some whitespace"
Clockwork-Muse Mar 5, 2022
e19a489
Revert "Add RegGen for Microsoft.Extensions.Logging"
Clockwork-Muse Mar 5, 2022
94661ca
Revert "Add RegGen for Microsoft.Extensions.Logging.Console"
Clockwork-Muse Mar 5, 2022
15c545f
Revert "whitespace/formatting corrections"
Clockwork-Muse Mar 5, 2022
8cd8a07
Revert "Add RegGen to System.Diagnostics.Tracing"
Clockwork-Muse Mar 5, 2022
1631bdc
Revert "Remove regex from System.IO.FileSystem"
Clockwork-Muse Mar 5, 2022
b12f27b
Revert "Add RegGen to System.IO.Ports"
Clockwork-Muse Mar 5, 2022
3ba036d
Remove RegGen from System.Linq.Expressions
Clockwork-Muse Mar 5, 2022
94eb694
Revert "Use RegGen in System.Net.Mail"
Clockwork-Muse Mar 5, 2022
51f5474
Revert "Add RegGen to System.Net.Sockets"
Clockwork-Muse Mar 5, 2022
7627069
Remove use of RegGen from System.Runtime tests where appropriate.
Clockwork-Muse Mar 6, 2022
f092513
Revert "Add RegGen to System.Threading.Tasks"
Clockwork-Muse Mar 6, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void Constructor1()
// \p{Pi} any kind of opening quote https://www.compart.com/en/unicode/category/Pi
// \p{Pf} any kind of closing quote https://www.compart.com/en/unicode/category/Pf
// \p{Po} any kind of punctuation character that is not a dash, bracket, quote or connector https://www.compart.com/en/unicode/category/Po
Assert.True(Regex.IsMatch(cee.BareMessage, @"[\p{Pi}\p{Po}]" + Regex.Escape(typeof(ConfigurationErrorsException).FullName) + @"[\p{Pf}\p{Po}]"), "#2:" + cee.BareMessage);
Assert.Matches(@"[\p{Pi}\p{Po}]" + Regex.Escape(typeof(ConfigurationErrorsException).FullName) + @"[\p{Pf}\p{Po}]", cee.BareMessage);

Assert.NotNull(cee.Data);
Assert.Equal(0, cee.Data.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void ToString_ShowILOffset()
catch (Exception e)
{
Assert.Contains(asmName, e.InnerException.StackTrace);
Assert.True(Regex.Match(e.InnerException.StackTrace, p).Success);
Assert.Matches(p, e.InnerException.StackTrace);
}
}, SourceTestAssemblyPath, AssemblyName, regPattern).Dispose();

Expand All @@ -357,7 +357,7 @@ public void ToString_ShowILOffset()
catch (Exception e)
{
Assert.Contains(asmName, e.InnerException.StackTrace);
Assert.True(Regex.Match(e.InnerException.StackTrace, p).Success);
Assert.Matches(p, e.InnerException.StackTrace);
}
}, SourceTestAssemblyPath, AssemblyName, regPattern).Dispose();

Expand All @@ -381,7 +381,7 @@ public void ToString_ShowILOffset()
catch (Exception e)
{
Assert.Contains("RefEmit_InMemoryManifestModule", e.InnerException.StackTrace);
Assert.True(Regex.Match(e.InnerException.StackTrace, p).Success);
Assert.Matches(p, e.InnerException.StackTrace);
}
}, regPattern).Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void Test_BadTypes_Manifest(EventSource source)

string message = _event.PayloadString(0, "message");
// expected message: "ERROR: Exception in Command Processing for EventSource BadEventSource_Bad_Type_ByteArray: Unsupported type Byte[] in event source. "
Assert.Matches("Unsupported type", message);
Assert.Contains("Unsupported type", message);
}
}
finally
Expand Down Expand Up @@ -126,7 +126,7 @@ private void Test_Bad_EventSource_Startup(bool onStartup, Listener listener, Eve
Debug.WriteLine(string.Format("Message=\"{0}\"", message));
// expected message: "ERROR: Exception in Command Processing for EventSource BadEventSource_MismatchedIds: Event Event2 was assigned event ID 2 but 1 was passed to WriteEvent. "
if (!PlatformDetection.IsNetFramework) // .NET Framework has typo
Assert.Matches("Event Event2 was assigned event ID 2 but 1 was passed to WriteEvent", message);
Assert.Contains("Event Event2 was assigned event ID 2 but 1 was passed to WriteEvent", message);

// Validate the details of the EventWrittenEventArgs object
if (_event is EventListenerListener.EventListenerEvent elEvent)
Expand Down Expand Up @@ -158,46 +158,28 @@ public void Test_Bad_WriteRelatedID_ParameterName()
#if true
Debug.WriteLine("Test disabled because the fix it tests is not in CoreCLR yet.");
#else
BadEventSource_IncorrectWriteRelatedActivityIDFirstParameter bes = null;
EventListenerListener listener = null;
try
{
Guid oldGuid;
Guid newGuid = Guid.NewGuid();
Guid newGuid2 = Guid.NewGuid();
EventSource.SetCurrentThreadActivityId(newGuid, out oldGuid);

bes = new BadEventSource_IncorrectWriteRelatedActivityIDFirstParameter();
Guid oldGuid;
Guid newGuid = Guid.NewGuid();
Guid newGuid2 = Guid.NewGuid();
EventSource.SetCurrentThreadActivityId(newGuid, out oldGuid);

using (var listener = new EventListenerListener())
{
var events = new List<Event>();
listener.OnEvent = delegate (Event data) { events.Add(data); };
using (var bes = new BadEventSource_IncorrectWriteRelatedActivityIDFirstParameter())
using (var listener = new EventListenerListener())
{
var events = new List<Event>();
listener.OnEvent = delegate (Event data) { events.Add(data); };

listener.EventSourceCommand(bes.Name, EventCommand.Enable);
listener.EventSourceCommand(bes.Name, EventCommand.Enable);

bes.RelatedActivity(newGuid2, "Hello", 42, "AA", "BB");
bes.RelatedActivity(newGuid2, "Hello", 42, "AA", "BB");

// Confirm that we get exactly one event from this whole process, that has the error message we expect.
Assert.Equal(1, events.Count);
Event _event = events[0];
Assert.Equal("EventSourceMessage", _event.EventName);
string message = _event.PayloadString(0, "message");
// expected message: "EventSource expects the first parameter of the Event method to be of type Guid and to be named "relatedActivityId" when calling WriteEventWithRelatedActivityId."
Assert.True(Regex.IsMatch(message, "EventSource expects the first parameter of the Event method to be of type Guid and to be named \"relatedActivityId\" when calling WriteEventWithRelatedActivityId."));
}
}
finally
{
if (bes != null)
{
bes.Dispose();
}

if (listener != null)
{
listener.Dispose();
}
// Confirm that we get exactly one event from this whole process, that has the error message we expect.
Assert.Equal(1, events.Count);
Event _event = events[0];
Assert.Equal("EventSourceMessage", _event.EventName);
string message = _event.PayloadString(0, "message");
// expected message: "EventSource expects the first parameter of the Event method to be of type Guid and to be named "relatedActivityId" when calling WriteEventWithRelatedActivityId."
Assert.Contains("EventSource expects the first parameter of the Event method to be of type Guid and to be named \"relatedActivityId\" when calling WriteEventWithRelatedActivityId.", message);
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down
13 changes: 6 additions & 7 deletions src/libraries/System.Linq.Expressions/tests/CompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using Xunit;

namespace System.Linq.Expressions.Tests
Expand Down Expand Up @@ -385,15 +384,15 @@ internal static void VerifyIL(this LambdaExpression expression, string expected,

private static string Normalize(string s)
{
var normalizeRegex = new Regex(@"lambda_method[0-9]*");

Collections.Generic.IEnumerable<string> lines =
s
.Replace("\r\n", "\n")
.Split(new[] { '\n' })
.Select(line => line.Trim())
.Where(line => line != "" && !line.StartsWith("//"));
.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(line => !line.StartsWith("//"))
.Select(beforeLambdaUniquifierRemoval => normalizeRegex.Replace(beforeLambdaUniquifierRemoval, "lambda_method"));

string beforeLambdaUniquifierRemoval = string.Join("\n", lines);
return Regex.Replace(beforeLambdaUniquifierRemoval, "lambda_method[0-9]*", "lambda_method");
return string.Join("\n", lines);
}

private static void VerifyEmitConstantsToIL<T>(T value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Xunit;

namespace System.Linq.Expressions.Tests
Expand Down Expand Up @@ -66,8 +65,8 @@ public void CantBeFunc(Type[] typeArgs)
{
Type delType = Expression.GetDelegateType(typeArgs);
Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
Assert.DoesNotMatch(new Regex(@"System\.Action"), delType.FullName);
Assert.DoesNotMatch(new Regex(@"System\.Func"), delType.FullName);
Assert.DoesNotContain("System.Action", delType.FullName);
Assert.DoesNotContain("System.Func", delType.FullName);
Reflection.MethodInfo method = delType.GetMethod("Invoke");
Assert.Equal(typeArgs.Last(), method.ReturnType);
Assert.Equal(typeArgs.Take(typeArgs.Length - 1), method.GetParameters().Select(p => p.ParameterType));
Expand All @@ -92,8 +91,8 @@ public void CantBeAction(Type[] typeArgs)
{
Type delType = Expression.GetDelegateType(delegateArgs);
Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
Assert.DoesNotMatch(new Regex(@"System\.Action"), delType.FullName);
Assert.DoesNotMatch(new Regex(@"System\.Func"), delType.FullName);
Assert.DoesNotContain("System.Action", delType.FullName);
Assert.DoesNotContain("System.Func", delType.FullName);
Reflection.MethodInfo method = delType.GetMethod("Invoke");
Assert.Equal(typeof(void), method.ReturnType);
Assert.Equal(typeArgs, method.GetParameters().Select(p => p.ParameterType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<!-- Too much private reflection. Do not bother with trimming -->
<ILLinkTrimAssembly>false</ILLinkTrimAssembly>
<Nullable>enable</Nullable>
<EnableRegexGenerator>true</EnableRegexGenerator>
</PropertyGroup>
<PropertyGroup>
<RuntimeSerializationSources>System\Runtime\Serialization</RuntimeSerializationSources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,7 @@ internal static bool IsAssemblyFriendOfSerialization(Assembly assembly)
string internalsVisibleAttributeAssemblyName = internalsVisibleAttribute.AssemblyName;

if (internalsVisibleAttributeAssemblyName.Trim().Equals("System.Runtime.Serialization") ||
Regex.IsMatch(internalsVisibleAttributeAssemblyName, Globals.FullSRSInternalsVisiblePattern))
Globals.FullSRSInternalsVisibleRegex().IsMatch(internalsVisibleAttributeAssemblyName))
{
return true;
}
Expand All @@ -2238,7 +2238,8 @@ internal interface IGenericNameProvider
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
string GetNamespaces();
string GetGenericTypeName();
bool ParametersFromBuiltInNamespaces {
bool ParametersFromBuiltInNamespaces
{
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
get;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace System.Runtime.Serialization
{
internal static class Globals
internal static partial class Globals
{
/// <SecurityNote>
/// Review - changes to const could affect code generation logic; any changes should be reviewed.
Expand Down Expand Up @@ -831,6 +832,8 @@ internal static bool TypeOfScriptObject_IsAssignableFrom(Type type)
public static readonly string NewObjectId = string.Empty;
public const string NullObjectId = null;
public const string FullSRSInternalsVisiblePattern = @"^[\s]*System\.Runtime\.Serialization[\s]*,[\s]*PublicKey[\s]*=[\s]*(?i:00240000048000009400000006020000002400005253413100040000010001008d56c76f9e8649383049f383c44be0ec204181822a6c31cf5eb7ef486944d032188ea1d3920763712ccb12d75fb77e9811149e6148e5d32fbaab37611c1878ddc19e20ef135d0cb2cff2bfec3d115810c3d9069638fe4be215dbf795861920e5ab6f7db2e2ceef136ac23d5dd2bf031700aec232f6c6b1c785b4305c123b37ab)[\s]*$";
[RegexGenerator(FullSRSInternalsVisiblePattern)]
public static partial Regex FullSRSInternalsVisibleRegex();
public const string Space = " ";
public const string XsiPrefix = "i";
public const string XsdPrefix = "x";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ public static void GetFullPath_Windows_83Paths()

// Make sure the shortened name expands back to the original one
// Sometimes shortening or GetFullPath is changing the casing of "temp" on some test machines: normalize both sides
tempFilePath = Regex.Replace(tempFilePath, @"\\temp\\", @"\TEMP\", RegexOptions.IgnoreCase);
shortName = Regex.Replace(Path.GetFullPath(shortName), @"\\temp\\", @"\TEMP\", RegexOptions.IgnoreCase);
tempFilePath = tempFilePath.Replace(@"\\temp\\", @"\TEMP\", ignoreCase: true, culture: null);
shortName = Path.GetFullPath(shortName).Replace(@"\\temp\\", @"\TEMP\", ignoreCase: true, culture: null);
Assert.Equal(tempFilePath, shortName);

// Should work with device paths that aren't well-formed extended syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Nullable>disable</Nullable>
<!-- Disable nullability public only feature for NullabilityInfoContextTests -->
<Features>$(Features.Replace('nullablePublicOnly', '')</Features>
<EnableRegexGenerator>true</EnableRegexGenerator>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(FeatureGenericMath)' == 'true'">$(DefineConstants);FEATURE_GENERIC_MATH</DefineConstants>
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Runtime/tests/System/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void Exception_TargetSite()

Assert.True(caught);
}

static void RethrowException()
{
try
Expand Down Expand Up @@ -136,7 +136,7 @@ private static (string, string, int) ThrowAndRethrowSameMethod(out (string, stri
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))]
// [ActiveIssue(https://github.com/dotnet/runtime/issues/1871)] can't use ActiveIssue for archs
[ActiveIssue("https://github.com/mono/mono/issues/15141", TestRuntimes.Mono)]
public static void ThrowStatementDoesNotResetExceptionStackLineOtherMethod()
Expand Down Expand Up @@ -198,7 +198,7 @@ private static void VerifyCallStack(
var match = Regex.Match(frame, frameParserRegex);
Assert.True(match.Success);
Assert.Equal(expectedStackFrame.CallerMemberName, match.Groups["memberName"].Value);

if (PlatformDetection.IsLineNumbersSupported)
{
Assert.Equal(expectedStackFrame.SourceFilePath, match.Groups["filePath"].Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void StaticThrow_UpdatesStackTraceAppropriately()
for (int i = 0; i < 3; i++)
{
Assert.Same(e, Assert.Throws<FormatException>(() => ExceptionDispatchInfo.Throw(e)));
Assert.Equal(i, Regex.Matches(e.StackTrace, RethrowMessageSubstring).Count);
Assert.Equal(i, Regex.Count(e.StackTrace, RethrowMessageSubstring));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static Utf8Tests()

public static byte[] DecodeHex(ReadOnlySpan<char> inputHex)
{
Assert.True(Regex.IsMatch(inputHex.ToString(), "^([0-9a-fA-F]{2})*$"), "Input must be an even number of hex characters.");
Assert.Matches(@"^([0-9a-fA-F]{2})*$", inputHex.ToString());

return Convert.FromHexString(inputHex);
}
Expand Down
Loading