Skip to content

Commit

Permalink
feat: Added StringSyntax support so that IDE's help out the user
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 9, 2022
1 parent b5a24a2 commit ace36cb
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/bunit.core/ComponentParameterCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ComponentParameterCollectionBuilder<TComponent> Add<TChildComponent>(Expr
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
/// <param name="markup">The markup string to pass to the <see cref="RenderFragment"/>.</param>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, string markup)
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, [StringSyntax("Html")]string markup)
=> Add(parameterSelector, markup.ToMarkupRenderFragment());

/// <summary>
Expand Down Expand Up @@ -260,7 +260,7 @@ public ComponentParameterCollectionBuilder<TComponent> AddChildContent(RenderFra
/// </summary>
/// <param name="markup">The markup string to pass the ChildContent parameter wrapped in a <see cref="RenderFragment"/>.</param>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> AddChildContent(string markup)
public ComponentParameterCollectionBuilder<TComponent> AddChildContent([StringSyntax("Html")]string markup)
=> AddChildContent(markup.ToMarkupRenderFragment());

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.core/ComponentParameterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static ComponentParameter CascadingValue(object value)
/// </summary>
/// <param name="markup">Markup to pass to the child content parameter.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter ChildContent(string markup)
public static ComponentParameter ChildContent([StringSyntax("Html")]string markup)
{
return RenderFragment(nameof(ChildContent), markup);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public static ComponentParameter ChildContent(RenderFragment renderFragment)
/// <param name="name">Parameter name.</param>
/// <param name="markup">Markup to pass to the render fragment parameter.</param>
/// <returns>The <see cref="ComponentParameter"/>.</returns>
public static ComponentParameter RenderFragment(string name, string markup)
public static ComponentParameter RenderFragment(string name, [StringSyntax("Html")]string markup)
{
return ComponentParameter.CreateParameter(name, markup.ToMarkupRenderFragment());
}
Expand Down
2 changes: 1 addition & 1 deletion src/bunit.core/Extensions/BlazorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class BlazorExtensions
/// </summary>
/// <param name="markup">Markup to render.</param>
/// <returns>The <see cref="RenderFragment"/>.</returns>
public static RenderFragment ToMarkupRenderFragment(this string? markup)
public static RenderFragment ToMarkupRenderFragment([StringSyntax("Html")]this string? markup)
{
if (string.IsNullOrEmpty(markup))
return _ => { };
Expand Down
25 changes: 25 additions & 0 deletions src/bunit.core/StringSyntaxAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if !NET7_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

/// <summary>Fake version of the StringSyntaxAttribute, which was introduced in .NET 7</summary>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class StringSyntaxAttribute : Attribute
{
/// <summary>
/// Initializes the <see cref="StringSyntaxAttribute"/> with the identifier of the syntax used.
/// </summary>
public StringSyntaxAttribute(string syntax)
{
}

/// <summary>
/// Initializes the <see cref="StringSyntaxAttribute"/> with the identifier of the syntax used.
/// </summary>
public StringSyntaxAttribute(string syntax, params object?[] arguments)
{
}

/// <summary>The syntax identifier for strings containing URIs.</summary>
public const string Uri = nameof(Uri);
}
#endif
22 changes: 10 additions & 12 deletions src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class MarkupMatchesAssertExtensions
/// <param name="expected">The expected markup fragment.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this string actual, string expected, string? userMessage = null)
public static void MarkupMatches([StringSyntax("Html")]this string actual, [StringSyntax("Html")]string expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand All @@ -42,7 +42,7 @@ public static void MarkupMatches(this string actual, string expected, string? us
/// <param name="expected">The expected <see cref="IRenderedFragment"/>.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this string actual, IRenderedFragment expected, string? userMessage = null)
public static void MarkupMatches([StringSyntax("Html")]this string actual, IRenderedFragment expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand All @@ -62,7 +62,7 @@ public static void MarkupMatches(this string actual, IRenderedFragment expected,
/// <param name="expected">The expected <see cref="INodeList"/>.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this string actual, INodeList expected, string? userMessage = null)
public static void MarkupMatches([StringSyntax("Html")]this string actual, INodeList expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand All @@ -82,7 +82,7 @@ public static void MarkupMatches(this string actual, INodeList expected, string?
/// <param name="expected">The expected <see cref="INode"/>.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this string actual, INode expected, string? userMessage = null)
public static void MarkupMatches([StringSyntax("Html")]this string actual, INode expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand All @@ -102,7 +102,7 @@ public static void MarkupMatches(this string actual, INode expected, string? use
/// <param name="expected">The expected markup.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this IRenderedFragment actual, string expected, string? userMessage = null)
public static void MarkupMatches(this IRenderedFragment actual, [StringSyntax("Html")]string expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand Down Expand Up @@ -182,7 +182,7 @@ public static void MarkupMatches(this INode actual, IRenderedFragment expected,
/// <param name="expected">The expected markup.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INode actual, string expected, string? userMessage = null)
public static void MarkupMatches(this INode actual, [StringSyntax("Html")]string expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand All @@ -203,7 +203,7 @@ public static void MarkupMatches(this INode actual, string expected, string? use
/// <param name="expected">The expected markup.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INodeList actual, string expected, string? userMessage = null)
public static void MarkupMatches(this INodeList actual, [StringSyntax("Html")]string expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand Down Expand Up @@ -355,7 +355,7 @@ public static void MarkupMatches(this INodeList actual, RenderFragment expected,
/// <param name="expected">The expected markup fragment.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this IEnumerable<IElement> actual, string expected, string? userMessage = null)
public static void MarkupMatches(this IEnumerable<IElement> actual, [StringSyntax("Html")]string expected, string? userMessage = null)
{
if (actual is null)
throw new ArgumentNullException(nameof(actual));
Expand Down Expand Up @@ -473,9 +473,7 @@ private static INodeList ToNodeList(this IEnumerable<IElement> elements, BunitHt
using var parser = new BunitHtmlParser();
return nodesStr.ToNodeList(parser);
}
else
{
return nodesStr.ToNodeList(htmlParser);
}

return nodesStr.ToNodeList(htmlParser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static Predicate<Type> CreatePredicate(Type componentTypeToStub)
/// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param>
/// <param name="replacementMarkup">Markup that will be used as render output instead of the stubbed out component.</param>
/// <returns>A <see cref="ComponentFactoryCollection"/>.</returns>
public static ComponentFactoryCollection AddStub<TComponent>(this ComponentFactoryCollection factories, string replacementMarkup) where TComponent : IComponent
public static ComponentFactoryCollection AddStub<TComponent>(this ComponentFactoryCollection factories, [StringSyntax("Html")]string replacementMarkup) where TComponent : IComponent
=> AddStub<TComponent>(factories, b => b.AddMarkupContent(0, replacementMarkup));

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.web/Rendering/BunitHtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private BunitHtmlParser(IConfiguration angleSharpConfiguration)
/// </summary>
/// <param name="markup">The markup to parse.</param>
/// <returns>The <see cref="INodeList"/>.</returns>
public INodeList Parse(string markup)
public INodeList Parse([StringSyntax("Html")]string markup)
{
if (markup is null)
throw new ArgumentNullException(nameof(markup));
Expand Down Expand Up @@ -187,4 +187,4 @@ public IEnumerator<INode> GetEnumerator()

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}
10 changes: 7 additions & 3 deletions src/bunit.web/TestDoubles/NavigationManager/NavigationHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed class NavigationHistory : IEquatable<NavigationHistory>
/// <param name="uri"></param>
/// <param name="options"></param>
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Using string to align with NavigationManager")]
public NavigationHistory(string uri, Bunit.TestDoubles.NavigationOptions options)
public NavigationHistory([StringSyntax(StringSyntaxAttribute.Uri)]string uri, Bunit.TestDoubles.NavigationOptions options)
{
Uri = uri;
Options = options;
Expand All @@ -58,7 +58,7 @@ public NavigationHistory(string uri, Bunit.TestDoubles.NavigationOptions options
/// <param name="uri"></param>
/// <param name="options"></param>
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Using string to align with NavigationManager")]
public NavigationHistory(string uri, NavigationOptions options)
public NavigationHistory([StringSyntax(StringSyntaxAttribute.Uri)]string uri, NavigationOptions options)
{
Uri = uri;
Options = options;
Expand All @@ -74,7 +74,11 @@ public NavigationHistory(string uri, NavigationOptions options)
/// <param name="navigationState"></param>
/// <param name="exception"></param>
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Using string to align with NavigationManager")]
public NavigationHistory(string uri, NavigationOptions options, NavigationState navigationState, Exception? exception = null)
public NavigationHistory(
[StringSyntax(StringSyntaxAttribute.Uri)]string uri,
NavigationOptions options,
NavigationState navigationState,
Exception? exception = null)
{
Uri = uri;
Options = options;
Expand Down

0 comments on commit ace36cb

Please sign in to comment.