From 5eb81f2744c09f7210bfec13cc9c9afcdfde7aa3 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Fri, 18 Feb 2022 16:37:29 +0100 Subject: [PATCH] Annotated [Not]MatchRegex with [StringSyntax("Regex")] --- .../Primitives/StringAssertions.cs | 7 ++- Src/FluentAssertions/StringSyntaxAttribute.cs | 52 +++++++++++++++++++ docs/_pages/releases.md | 1 + 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 Src/FluentAssertions/StringSyntaxAttribute.cs diff --git a/Src/FluentAssertions/Primitives/StringAssertions.cs b/Src/FluentAssertions/Primitives/StringAssertions.cs index c75f0ddce1..83ff92ec69 100644 --- a/Src/FluentAssertions/Primitives/StringAssertions.cs +++ b/Src/FluentAssertions/Primitives/StringAssertions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text.RegularExpressions; using FluentAssertions.Common; @@ -399,7 +400,8 @@ public AndConstraint NotMatchEquivalentOf(string wildcardPattern, s /// /// Zero or more objects to format using the placeholders in . /// - public AndConstraint MatchRegex([RegexPattern] string regularExpression, string because = "", params object[] becauseArgs) + public AndConstraint MatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression, + string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression), "Cannot match string against . Provide a regex pattern or use the BeNull method."); @@ -475,7 +477,8 @@ public AndConstraint MatchRegex(Regex regularExpression, string bec /// /// Zero or more objects to format using the placeholders in . /// - public AndConstraint NotMatchRegex([RegexPattern] string regularExpression, string because = "", params object[] becauseArgs) + public AndConstraint NotMatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression, + string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression), "Cannot match string against . Provide a regex pattern or use the NotBeNull method."); diff --git a/Src/FluentAssertions/StringSyntaxAttribute.cs b/Src/FluentAssertions/StringSyntaxAttribute.cs new file mode 100644 index 0000000000..13ce1ad65e --- /dev/null +++ b/Src/FluentAssertions/StringSyntaxAttribute.cs @@ -0,0 +1,52 @@ +// copied from https://raw.githubusercontent.com/dotnet/runtime/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/StringSyntaxAttribute.cs +#if !NET7_0_OR_GREATER +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +namespace System.Diagnostics.CodeAnalysis +{ + /// Specifies the syntax used in a string. + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] +#if SYSTEM_PRIVATE_CORELIB + public +#else + internal +#endif + sealed class StringSyntaxAttribute : Attribute + { + /// Initializes the with the identifier of the syntax used. + /// The syntax identifier. + public StringSyntaxAttribute(string syntax) + { + Syntax = syntax; + Arguments = Array.Empty(); + } + + /// Initializes the with the identifier of the syntax used. + /// The syntax identifier. + /// Optional arguments associated with the specific syntax employed. + public StringSyntaxAttribute(string syntax, params object?[] arguments) + { + Syntax = syntax; + Arguments = arguments; + } + + /// Gets the identifier of the syntax used. + public string Syntax { get; } + + /// Optional arguments associated with the specific syntax employed. + public object?[] Arguments { get; } + + /// The syntax identifier for strings containing date and time format specifiers. + public const string DateTimeFormat = nameof(DateTimeFormat); + + /// The syntax identifier for strings containing JavaScript Object Notation (JSON). + public const string Json = nameof(Json); + + /// The syntax identifier for strings containing regular expressions. + public const string Regex = nameof(Regex); + } +} +#endif diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index bbaf88671c..ab0e3359c5 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -10,6 +10,7 @@ sidebar: ## Unreleased ### What's New +* Annotated `[Not]MatchRegex(string)` with `[StringSyntax("Regex")]` which IDEs can use to colorize the regular expression argument - [#1816](https://github.com/fluentassertions/fluentassertions/pull/1816) ### Fixes