Skip to content

Commit ace36cb

Browse files
committed
feat: Added StringSyntax support so that IDE's help out the user
1 parent b5a24a2 commit ace36cb

File tree

8 files changed

+50
-23
lines changed

8 files changed

+50
-23
lines changed

src/bunit.core/ComponentParameterCollectionBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ComponentParameterCollectionBuilder<TComponent> Add<TChildComponent>(Expr
7272
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
7373
/// <param name="markup">The markup string to pass to the <see cref="RenderFragment"/>.</param>
7474
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
75-
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, string markup)
75+
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, [StringSyntax("Html")]string markup)
7676
=> Add(parameterSelector, markup.ToMarkupRenderFragment());
7777

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

266266
/// <summary>

src/bunit.core/ComponentParameterFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static ComponentParameter CascadingValue(object value)
137137
/// </summary>
138138
/// <param name="markup">Markup to pass to the child content parameter.</param>
139139
/// <returns>The <see cref="ComponentParameter"/>.</returns>
140-
public static ComponentParameter ChildContent(string markup)
140+
public static ComponentParameter ChildContent([StringSyntax("Html")]string markup)
141141
{
142142
return RenderFragment(nameof(ChildContent), markup);
143143
}
@@ -173,7 +173,7 @@ public static ComponentParameter ChildContent(RenderFragment renderFragment)
173173
/// <param name="name">Parameter name.</param>
174174
/// <param name="markup">Markup to pass to the render fragment parameter.</param>
175175
/// <returns>The <see cref="ComponentParameter"/>.</returns>
176-
public static ComponentParameter RenderFragment(string name, string markup)
176+
public static ComponentParameter RenderFragment(string name, [StringSyntax("Html")]string markup)
177177
{
178178
return ComponentParameter.CreateParameter(name, markup.ToMarkupRenderFragment());
179179
}

src/bunit.core/Extensions/BlazorExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class BlazorExtensions
1010
/// </summary>
1111
/// <param name="markup">Markup to render.</param>
1212
/// <returns>The <see cref="RenderFragment"/>.</returns>
13-
public static RenderFragment ToMarkupRenderFragment(this string? markup)
13+
public static RenderFragment ToMarkupRenderFragment([StringSyntax("Html")]this string? markup)
1414
{
1515
if (string.IsNullOrEmpty(markup))
1616
return _ => { };
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if !NET7_0_OR_GREATER
2+
namespace System.Diagnostics.CodeAnalysis;
3+
4+
/// <summary>Fake version of the StringSyntaxAttribute, which was introduced in .NET 7</summary>
5+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
6+
public sealed class StringSyntaxAttribute : Attribute
7+
{
8+
/// <summary>
9+
/// Initializes the <see cref="StringSyntaxAttribute"/> with the identifier of the syntax used.
10+
/// </summary>
11+
public StringSyntaxAttribute(string syntax)
12+
{
13+
}
14+
15+
/// <summary>
16+
/// Initializes the <see cref="StringSyntaxAttribute"/> with the identifier of the syntax used.
17+
/// </summary>
18+
public StringSyntaxAttribute(string syntax, params object?[] arguments)
19+
{
20+
}
21+
22+
/// <summary>The syntax identifier for strings containing URIs.</summary>
23+
public const string Uri = nameof(Uri);
24+
}
25+
#endif

src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class MarkupMatchesAssertExtensions
2020
/// <param name="expected">The expected markup fragment.</param>
2121
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
2222
[AssertionMethod]
23-
public static void MarkupMatches(this string actual, string expected, string? userMessage = null)
23+
public static void MarkupMatches([StringSyntax("Html")]this string actual, [StringSyntax("Html")]string expected, string? userMessage = null)
2424
{
2525
if (actual is null)
2626
throw new ArgumentNullException(nameof(actual));
@@ -42,7 +42,7 @@ public static void MarkupMatches(this string actual, string expected, string? us
4242
/// <param name="expected">The expected <see cref="IRenderedFragment"/>.</param>
4343
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
4444
[AssertionMethod]
45-
public static void MarkupMatches(this string actual, IRenderedFragment expected, string? userMessage = null)
45+
public static void MarkupMatches([StringSyntax("Html")]this string actual, IRenderedFragment expected, string? userMessage = null)
4646
{
4747
if (actual is null)
4848
throw new ArgumentNullException(nameof(actual));
@@ -62,7 +62,7 @@ public static void MarkupMatches(this string actual, IRenderedFragment expected,
6262
/// <param name="expected">The expected <see cref="INodeList"/>.</param>
6363
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
6464
[AssertionMethod]
65-
public static void MarkupMatches(this string actual, INodeList expected, string? userMessage = null)
65+
public static void MarkupMatches([StringSyntax("Html")]this string actual, INodeList expected, string? userMessage = null)
6666
{
6767
if (actual is null)
6868
throw new ArgumentNullException(nameof(actual));
@@ -82,7 +82,7 @@ public static void MarkupMatches(this string actual, INodeList expected, string?
8282
/// <param name="expected">The expected <see cref="INode"/>.</param>
8383
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
8484
[AssertionMethod]
85-
public static void MarkupMatches(this string actual, INode expected, string? userMessage = null)
85+
public static void MarkupMatches([StringSyntax("Html")]this string actual, INode expected, string? userMessage = null)
8686
{
8787
if (actual is null)
8888
throw new ArgumentNullException(nameof(actual));
@@ -102,7 +102,7 @@ public static void MarkupMatches(this string actual, INode expected, string? use
102102
/// <param name="expected">The expected markup.</param>
103103
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
104104
[AssertionMethod]
105-
public static void MarkupMatches(this IRenderedFragment actual, string expected, string? userMessage = null)
105+
public static void MarkupMatches(this IRenderedFragment actual, [StringSyntax("Html")]string expected, string? userMessage = null)
106106
{
107107
if (actual is null)
108108
throw new ArgumentNullException(nameof(actual));
@@ -182,7 +182,7 @@ public static void MarkupMatches(this INode actual, IRenderedFragment expected,
182182
/// <param name="expected">The expected markup.</param>
183183
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
184184
[AssertionMethod]
185-
public static void MarkupMatches(this INode actual, string expected, string? userMessage = null)
185+
public static void MarkupMatches(this INode actual, [StringSyntax("Html")]string expected, string? userMessage = null)
186186
{
187187
if (actual is null)
188188
throw new ArgumentNullException(nameof(actual));
@@ -203,7 +203,7 @@ public static void MarkupMatches(this INode actual, string expected, string? use
203203
/// <param name="expected">The expected markup.</param>
204204
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
205205
[AssertionMethod]
206-
public static void MarkupMatches(this INodeList actual, string expected, string? userMessage = null)
206+
public static void MarkupMatches(this INodeList actual, [StringSyntax("Html")]string expected, string? userMessage = null)
207207
{
208208
if (actual is null)
209209
throw new ArgumentNullException(nameof(actual));
@@ -355,7 +355,7 @@ public static void MarkupMatches(this INodeList actual, RenderFragment expected,
355355
/// <param name="expected">The expected markup fragment.</param>
356356
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
357357
[AssertionMethod]
358-
public static void MarkupMatches(this IEnumerable<IElement> actual, string expected, string? userMessage = null)
358+
public static void MarkupMatches(this IEnumerable<IElement> actual, [StringSyntax("Html")]string expected, string? userMessage = null)
359359
{
360360
if (actual is null)
361361
throw new ArgumentNullException(nameof(actual));
@@ -473,9 +473,7 @@ private static INodeList ToNodeList(this IEnumerable<IElement> elements, BunitHt
473473
using var parser = new BunitHtmlParser();
474474
return nodesStr.ToNodeList(parser);
475475
}
476-
else
477-
{
478-
return nodesStr.ToNodeList(htmlParser);
479-
}
476+
477+
return nodesStr.ToNodeList(htmlParser);
480478
}
481479
}

src/bunit.web/Extensions/StubComponentFactoryCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static Predicate<Type> CreatePredicate(Type componentTypeToStub)
3939
/// <param name="factories">The bUnit <see cref="ComponentFactoryCollection"/> to configure.</param>
4040
/// <param name="replacementMarkup">Markup that will be used as render output instead of the stubbed out component.</param>
4141
/// <returns>A <see cref="ComponentFactoryCollection"/>.</returns>
42-
public static ComponentFactoryCollection AddStub<TComponent>(this ComponentFactoryCollection factories, string replacementMarkup) where TComponent : IComponent
42+
public static ComponentFactoryCollection AddStub<TComponent>(this ComponentFactoryCollection factories, [StringSyntax("Html")]string replacementMarkup) where TComponent : IComponent
4343
=> AddStub<TComponent>(factories, b => b.AddMarkupContent(0, replacementMarkup));
4444

4545
/// <summary>

src/bunit.web/Rendering/BunitHtmlParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private BunitHtmlParser(IConfiguration angleSharpConfiguration)
5757
/// </summary>
5858
/// <param name="markup">The markup to parse.</param>
5959
/// <returns>The <see cref="INodeList"/>.</returns>
60-
public INodeList Parse(string markup)
60+
public INodeList Parse([StringSyntax("Html")]string markup)
6161
{
6262
if (markup is null)
6363
throw new ArgumentNullException(nameof(markup));
@@ -187,4 +187,4 @@ public IEnumerator<INode> GetEnumerator()
187187

188188
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
189189
}
190-
}
190+
}

src/bunit.web/TestDoubles/NavigationManager/NavigationHistory.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public sealed class NavigationHistory : IEquatable<NavigationHistory>
4545
/// <param name="uri"></param>
4646
/// <param name="options"></param>
4747
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Using string to align with NavigationManager")]
48-
public NavigationHistory(string uri, Bunit.TestDoubles.NavigationOptions options)
48+
public NavigationHistory([StringSyntax(StringSyntaxAttribute.Uri)]string uri, Bunit.TestDoubles.NavigationOptions options)
4949
{
5050
Uri = uri;
5151
Options = options;
@@ -58,7 +58,7 @@ public NavigationHistory(string uri, Bunit.TestDoubles.NavigationOptions options
5858
/// <param name="uri"></param>
5959
/// <param name="options"></param>
6060
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Using string to align with NavigationManager")]
61-
public NavigationHistory(string uri, NavigationOptions options)
61+
public NavigationHistory([StringSyntax(StringSyntaxAttribute.Uri)]string uri, NavigationOptions options)
6262
{
6363
Uri = uri;
6464
Options = options;
@@ -74,7 +74,11 @@ public NavigationHistory(string uri, NavigationOptions options)
7474
/// <param name="navigationState"></param>
7575
/// <param name="exception"></param>
7676
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Using string to align with NavigationManager")]
77-
public NavigationHistory(string uri, NavigationOptions options, NavigationState navigationState, Exception? exception = null)
77+
public NavigationHistory(
78+
[StringSyntax(StringSyntaxAttribute.Uri)]string uri,
79+
NavigationOptions options,
80+
NavigationState navigationState,
81+
Exception? exception = null)
7882
{
7983
Uri = uri;
8084
Options = options;

0 commit comments

Comments
 (0)