public static async Task <Regex> GetRegexAsync(RegexEngine engine, string pattern, RegexOptions?options = null, TimeSpan?matchTimeout = null) { if (options is null) { Assert.Null(matchTimeout); } switch (engine) { case RegexEngine.Interpreter: return (options is null ? new Regex(pattern) : matchTimeout is null ? new Regex(pattern, options.Value) : new Regex(pattern, options.Value, matchTimeout.Value)); case RegexEngine.Compiled: return (options is null ? new Regex(pattern, RegexOptions.Compiled) : matchTimeout is null ? new Regex(pattern, options.Value | RegexOptions.Compiled) : new Regex(pattern, options.Value | RegexOptions.Compiled, matchTimeout.Value)); case RegexEngine.SourceGenerated: return(await RegexGeneratorHelper.SourceGenRegexAsync(pattern, options, matchTimeout)); } throw new ArgumentException($"Unknown engine: {engine}"); }
public static async Task <Regex> GetRegexAsync(RegexEngine engine, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern, RegexOptions options, Globalization.CultureInfo culture) { if (engine == RegexEngine.SourceGenerated) { return(await RegexGeneratorHelper.SourceGenRegexAsync(pattern, culture, options)); } using (new System.Tests.ThreadCultureChange(culture)) { return(await GetRegexAsync(engine, pattern, options)); } }
public async Task Diagnostic_InvalidRegexPattern(string pattern) { IReadOnlyList<Diagnostic> diagnostics = await RegexGeneratorHelper.RunGenerator($@" using System.Text.RegularExpressions; partial class C {{ [RegexGenerator({pattern})] private static partial Regex InvalidPattern(); }} "); Assert.Equal("SYSLIB1042", Assert.Single(diagnostics).Id); }
public async Task Diagnostic_MultipleAttributes() { IReadOnlyList<Diagnostic> diagnostics = await RegexGeneratorHelper.RunGenerator(@" using System.Text.RegularExpressions; partial class C { [RegexGenerator(""ab"")] [RegexGenerator(""abc"")] private static partial Regex MultipleAttributes(); } "); Assert.Equal("SYSLIB1041", Assert.Single(diagnostics).Id); }
public async Task Diagnostic_MalformedCtor(string attribute) { // Validate the generator doesn't crash with an incomplete attribute IReadOnlyList<Diagnostic> diagnostics = await RegexGeneratorHelper.RunGenerator($@" using System.Text.RegularExpressions; partial class C {{ {attribute} private static partial Regex MultipleAttributes(); }} "); if (diagnostics.Count != 0) { Assert.Contains(Assert.Single(diagnostics).Id, new[] { "SYSLIB1040", "SYSLIB1042" }); } }