Skip to content

Commit ad476f4

Browse files
committed
Add regex test for parsing runtime-assets expressions
1 parent 62b2333 commit ad476f4

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<SystemPrivateRuntimeUnicodeDataVersion>7.0.0-beta.21602.1</SystemPrivateRuntimeUnicodeDataVersion>
128128
<SystemRuntimeTimeZoneDataVersion>7.0.0-beta.21602.1</SystemRuntimeTimeZoneDataVersion>
129129
<SystemSecurityCryptographyX509CertificatesTestDataVersion>7.0.0-beta.21602.1</SystemSecurityCryptographyX509CertificatesTestDataVersion>
130+
<SystemTextRegularExpressionsTestDataVersion>7.0.0-beta.21602.1</SystemTextRegularExpressionsTestDataVersion>
130131
<SystemWindowsExtensionsTestDataVersion>7.0.0-beta.21602.1</SystemWindowsExtensionsTestDataVersion>
131132
<MicrosoftDotNetCilStripSourcesVersion>7.0.0-beta.21602.1</MicrosoftDotNetCilStripSourcesVersion>
132133
<!-- dotnet-optimization dependencies -->

src/libraries/System.Text.RegularExpressions/tests/Regex.KnownPattern.Tests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System.Collections.Generic;
55
using System.Globalization;
6+
using System.IO;
67
using System.Linq;
8+
using System.Text.Json;
79
using System.Threading.Tasks;
810
using Xunit;
911

@@ -1405,5 +1407,58 @@ public void TerminationInNonBacktrackingVsBackTracking(RegexOptions options, int
14051407
// NonBacktracking needs way less than 1s
14061408
Assert.False(re.Match(input).Success);
14071409
}
1410+
1411+
//
1412+
// dotnet/runtime-assets contains a set a regular expressions sourced from
1413+
// permissively-licensed packages. Validate Regex behavior with those expressions.
1414+
//
1415+
1416+
[Theory]
1417+
[InlineData(RegexEngine.Interpreter)]
1418+
[InlineData(RegexEngine.Compiled)]
1419+
public void PatternsDataSet_ConstructRegexForAll(RegexEngine engine)
1420+
{
1421+
Parallel.ForEach(s_patternsDataSet.Value, r =>
1422+
{
1423+
try
1424+
{
1425+
RegexHelpers.GetRegexAsync(engine, r.Pattern, r.Options).GetAwaiter().GetResult();
1426+
}
1427+
catch (Exception e) when (RegexHelpers.IsNonBacktracking(engine) && e.Message.Contains("NonBacktracking"))
1428+
{
1429+
// Some tests aren't supported with RegexOptions.NonBacktracking.
1430+
}
1431+
});
1432+
}
1433+
1434+
[OuterLoop("Takes many seconds")]
1435+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "NonBacktracking isn't supported on .NET Framework")]
1436+
[Fact]
1437+
public void PatternsDataSet_ConstructRegexForAll_NonBacktracking() =>
1438+
PatternsDataSet_ConstructRegexForAll(RegexEngine.NonBacktracking);
1439+
1440+
[OuterLoop("Takes minutes to generate and compile thousands of expressions")]
1441+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Source generator isn't supported on .NET Framework")]
1442+
[Fact]
1443+
public void PatternsDataSet_ConstructRegexForAll_SourceGenerated() =>
1444+
PatternsDataSet_ConstructRegexForAll(RegexEngine.SourceGenerated);
1445+
1446+
private static Lazy<DataSetExpression[]> s_patternsDataSet = new Lazy<DataSetExpression[]>(() =>
1447+
{
1448+
using Stream json = File.OpenRead("Regex_RealWorldPatterns.json");
1449+
return JsonSerializer.Deserialize<DataSetExpression[]>(json, new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip }).Distinct().ToArray();
1450+
});
1451+
1452+
private sealed class DataSetExpression : IEquatable<DataSetExpression>
1453+
{
1454+
public int Count { get; set; }
1455+
public RegexOptions Options { get; set; }
1456+
public string Pattern { get; set; }
1457+
1458+
public bool Equals(DataSetExpression? other) =>
1459+
other is not null &&
1460+
other.Pattern == Pattern &&
1461+
(Options & ~RegexOptions.Compiled) == (other.Options & ~RegexOptions.Compiled); // Compiled doesn't affect semantics, so remove it from equality for our purposes
1462+
}
14081463
}
14091464
}

src/libraries/System.Text.RegularExpressions/tests/System.Text.RegularExpressions.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<Compile Include="RegexAssert.netfx.cs" />
3939
<Compile Include="RegexParserTests.netfx.cs" />
4040
<Compile Include="RegexGeneratorHelper.netfx.cs" />
41+
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
4142
</ItemGroup>
4243
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
4344
<Compile Include="RegexAssert.netcoreapp.cs" />
@@ -57,4 +58,7 @@
5758
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
5859
<ProjectReference Include="..\gen\System.Text.RegularExpressions.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
5960
</ItemGroup>
61+
<ItemGroup>
62+
<PackageReference Include="System.Text.RegularExpressions.TestData" Version="$(SystemTextRegularExpressionsTestDataVersion)" />
63+
</ItemGroup>
6064
</Project>

0 commit comments

Comments
 (0)