Skip to content

Commit c222cf7

Browse files
Marked MarkupMatches as assertion method to help 3rd party with analyzers
* Marked MarkupMatches and WaitForAssertion as assertion methods * Updated changelog * Update src/bunit.core/Asserting/AssertionMethodAttribute.cs * Tweaked CHANGELOG, removed AssertionMethod attribute from WaitForAssertion * Removed unused usings Co-authored-by: Pavel Mikula <57188685+pavel-mikula-sonarsource@users.noreply.github.com>
1 parent d966bcb commit c222cf7

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ List of new features.
1111

1212
- Two new overloads to the `RenderFragment()` and `ChildContent()` component parameter factory methods have been added that takes a `RenderFragment` as input. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
1313
- Added a `ComponentParameterCollection` type. The `ComponentParameterCollection` is a collection of component parameters, that knows how to turn those components parameters into a `RenderFragment`, which will render a component and pass any parameters inside the collection to that component. That logic was spread out over multiple places in bUnit, and is now owned by the `ComponentParameterCollection` type. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
14-
- Added additional placeholder services for `NavigationManager`, `HttpClient`, and `IStringLocalizer`, to make it easier for users to figure out why a test is failing due to missing service registration before rendering a component. By [@joro550](https://github.com/joro550) in [#203](https://github.com/egil/bUnit/pull/223).
14+
- Added additional placeholder services for `NavigationManager`, `HttpClient`, and `IStringLocalizer`, to make it easier for users to figure out why a test is failing due to missing service registration before rendering a component. By [@joro550](https://github.com/joro550) in [#223](https://github.com/egil/bUnit/pull/223).
1515

1616
### Changed
1717
List of changes in existing functionality.
@@ -34,7 +34,8 @@ List of changes in existing functionality.
3434
);
3535
```
3636
By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
37-
- All test doubles are now in the same namespace, `Bunit.TestDoubles`. So all import statements for `Bunit.TestDoubles.JSInterop` and `Bunit.TestDoubles.Authorization` must be changed to `Bunit.TestDoubles`. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/223).
37+
- All test doubles are now in the same namespace, `Bunit.TestDoubles`. So all import statements for `Bunit.TestDoubles.JSInterop` and `Bunit.TestDoubles.Authorization` must be changed to `Bunit.TestDoubles`. By [@egil](https://github.com/egil) in [#223](https://github.com/egil/bUnit/pull/223).
38+
- Marked MarkupMatches methods as assertion methods to stop SonarSource analyzers complaining about missing assertions in tests. By [@egil](https://github.com/egil) in [#229](https://github.com/egil/bUnit/pull/229).
3839

3940
### Deprecated
4041
List of soon-to-be removed features.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Bunit.Asserting
4+
{
5+
/// <summary>
6+
/// Add this attribute to assertion methods to indicate to
7+
/// 3rd party analyzers that the method is an assertion method.
8+
/// See more here: https://rules.sonarsource.com/csharp/RSPEC-2699
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Method)]
11+
public sealed class AssertionMethodAttribute : Attribute { }
12+
}

src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using AngleSharp.Dom;
3+
using Bunit.Asserting;
34
using Bunit.Diffing;
45
using Bunit.Rendering;
56
using Microsoft.Extensions.DependencyInjection;
@@ -19,6 +20,7 @@ public static class MarkupMatchesAssertExtensions
1920
/// <param name="actual">The markup fragment to verify.</param>
2021
/// <param name="expected">The expected markup fragment.</param>
2122
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
23+
[AssertionMethod]
2224
public static void MarkupMatches(this string actual, string expected, string? userMessage = null)
2325
{
2426
using var parser = new BunitHtmlParser();
@@ -35,6 +37,7 @@ public static void MarkupMatches(this string actual, string expected, string? us
3537
/// <param name="actual">The markup fragment to verify.</param>
3638
/// <param name="expected">The expected <see cref="IRenderedFragmentBase"/>.</param>
3739
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
40+
[AssertionMethod]
3841
public static void MarkupMatches(this string actual, IRenderedFragment expected, string? userMessage = null)
3942
{
4043
if (expected is null)
@@ -52,6 +55,7 @@ public static void MarkupMatches(this string actual, IRenderedFragment expected,
5255
/// <param name="actual">The markup fragment to verify.</param>
5356
/// <param name="expected">The expected <see cref="INodeList"/>.</param>
5457
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
58+
[AssertionMethod]
5559
public static void MarkupMatches(this string actual, INodeList expected, string? userMessage = null)
5660
{
5761
if (expected is null)
@@ -69,6 +73,7 @@ public static void MarkupMatches(this string actual, INodeList expected, string?
6973
/// <param name="actual">The markup fragment to verify.</param>
7074
/// <param name="expected">The expected <see cref="INode"/>.</param>
7175
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
76+
[AssertionMethod]
7277
public static void MarkupMatches(this string actual, INode expected, string? userMessage = null)
7378
{
7479
if (expected is null)
@@ -86,6 +91,7 @@ public static void MarkupMatches(this string actual, INode expected, string? use
8691
/// <param name="actual">The rendered fragment to verify.</param>
8792
/// <param name="expected">The expected markup.</param>
8893
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
94+
[AssertionMethod]
8995
public static void MarkupMatches(this IRenderedFragment actual, string expected, string? userMessage = null)
9096
{
9197
if (actual is null)
@@ -105,6 +111,7 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
105111
/// <param name="actual">The rendered fragment to verify.</param>
106112
/// <param name="expected">The expected rendered fragment.</param>
107113
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
114+
[AssertionMethod]
108115
public static void MarkupMatches(this IRenderedFragment actual, IRenderedFragment expected, string? userMessage = null)
109116
{
110117
if (actual is null)
@@ -124,6 +131,7 @@ public static void MarkupMatches(this IRenderedFragment actual, IRenderedFragmen
124131
/// <param name="actual">The list of nodes to verify.</param>
125132
/// <param name="expected">The expected rendered fragment.</param>
126133
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
134+
[AssertionMethod]
127135
public static void MarkupMatches(this INodeList actual, IRenderedFragment expected, string? userMessage = null)
128136
{
129137
if (actual is null)
@@ -143,6 +151,7 @@ public static void MarkupMatches(this INodeList actual, IRenderedFragment expect
143151
/// <param name="actual">The node to verify.</param>
144152
/// <param name="expected">The expected rendered fragment.</param>
145153
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
154+
[AssertionMethod]
146155
public static void MarkupMatches(this INode actual, IRenderedFragment expected, string? userMessage = null)
147156
{
148157
if (actual is null)
@@ -162,6 +171,7 @@ public static void MarkupMatches(this INode actual, IRenderedFragment expected,
162171
/// <param name="actual">The node to verify.</param>
163172
/// <param name="expected">The expected markup.</param>
164173
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
174+
[AssertionMethod]
165175
public static void MarkupMatches(this INode actual, string expected, string? userMessage = null)
166176
{
167177
if (actual is null)
@@ -180,6 +190,7 @@ public static void MarkupMatches(this INode actual, string expected, string? use
180190
/// <param name="actual">The list of nodes to verify.</param>
181191
/// <param name="expected">The expected markup.</param>
182192
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
193+
[AssertionMethod]
183194
public static void MarkupMatches(this INodeList actual, string expected, string? userMessage = null)
184195
{
185196
if (actual is null)
@@ -198,6 +209,7 @@ public static void MarkupMatches(this INodeList actual, string expected, string?
198209
/// <param name="actual">The list of nodes to verify.</param>
199210
/// <param name="expected">The expected list of nodes.</param>
200211
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
212+
[AssertionMethod]
201213
public static void MarkupMatches(this INodeList actual, INodeList expected, string? userMessage = null)
202214
{
203215
var diffs = actual.CompareTo(expected);
@@ -215,6 +227,7 @@ public static void MarkupMatches(this INodeList actual, INodeList expected, stri
215227
/// <param name="actual">The list of nodes to verify.</param>
216228
/// <param name="expected">The expected node.</param>
217229
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
230+
[AssertionMethod]
218231
public static void MarkupMatches(this INodeList actual, INode expected, string? userMessage = null)
219232
{
220233
var diffs = actual.CompareTo(expected);
@@ -232,6 +245,7 @@ public static void MarkupMatches(this INodeList actual, INode expected, string?
232245
/// <param name="actual">The node to verify.</param>
233246
/// <param name="expected">The expected list of nodes.</param>
234247
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
248+
[AssertionMethod]
235249
public static void MarkupMatches(this INode actual, INodeList expected, string? userMessage = null)
236250
{
237251
var diffs = actual.CompareTo(expected);

0 commit comments

Comments
 (0)