Skip to content

Commit

Permalink
Marked MarkupMatches as assertion method to help 3rd party with analy…
Browse files Browse the repository at this point in the history
…zers

* 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>
  • Loading branch information
egil and pavel-mikula-sonarsource authored Oct 8, 2020
1 parent d966bcb commit c222cf7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ List of new features.

- 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).
- 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).
- 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).
- 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).

### Changed
List of changes in existing functionality.
Expand All @@ -34,7 +34,8 @@ List of changes in existing functionality.
);
```
By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
- 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).
- 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).
- 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).
### Deprecated
List of soon-to-be removed features.
Expand Down
12 changes: 12 additions & 0 deletions src/bunit.core/Asserting/AssertionMethodAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Bunit.Asserting
{
/// <summary>
/// Add this attribute to assertion methods to indicate to
/// 3rd party analyzers that the method is an assertion method.
/// See more here: https://rules.sonarsource.com/csharp/RSPEC-2699
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class AssertionMethodAttribute : Attribute { }
}
14 changes: 14 additions & 0 deletions src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using AngleSharp.Dom;
using Bunit.Asserting;
using Bunit.Diffing;
using Bunit.Rendering;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -19,6 +20,7 @@ public static class MarkupMatchesAssertExtensions
/// <param name="actual">The markup fragment to verify.</param>
/// <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)
{
using var parser = new BunitHtmlParser();
Expand All @@ -35,6 +37,7 @@ public static void MarkupMatches(this string actual, string expected, string? us
/// <param name="actual">The markup fragment to verify.</param>
/// <param name="expected">The expected <see cref="IRenderedFragmentBase"/>.</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)
{
if (expected is null)
Expand All @@ -52,6 +55,7 @@ public static void MarkupMatches(this string actual, IRenderedFragment expected,
/// <param name="actual">The markup fragment to verify.</param>
/// <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)
{
if (expected is null)
Expand All @@ -69,6 +73,7 @@ public static void MarkupMatches(this string actual, INodeList expected, string?
/// <param name="actual">The markup fragment to verify.</param>
/// <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)
{
if (expected is null)
Expand All @@ -86,6 +91,7 @@ public static void MarkupMatches(this string actual, INode expected, string? use
/// <param name="actual">The rendered fragment to verify.</param>
/// <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)
{
if (actual is null)
Expand All @@ -105,6 +111,7 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
/// <param name="actual">The rendered fragment to verify.</param>
/// <param name="expected">The expected rendered fragment.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this IRenderedFragment actual, IRenderedFragment expected, string? userMessage = null)
{
if (actual is null)
Expand All @@ -124,6 +131,7 @@ public static void MarkupMatches(this IRenderedFragment actual, IRenderedFragmen
/// <param name="actual">The list of nodes to verify.</param>
/// <param name="expected">The expected rendered fragment.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INodeList actual, IRenderedFragment expected, string? userMessage = null)
{
if (actual is null)
Expand All @@ -143,6 +151,7 @@ public static void MarkupMatches(this INodeList actual, IRenderedFragment expect
/// <param name="actual">The node to verify.</param>
/// <param name="expected">The expected rendered fragment.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INode actual, IRenderedFragment expected, string? userMessage = null)
{
if (actual is null)
Expand All @@ -162,6 +171,7 @@ public static void MarkupMatches(this INode actual, IRenderedFragment expected,
/// <param name="actual">The node to verify.</param>
/// <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)
{
if (actual is null)
Expand All @@ -180,6 +190,7 @@ public static void MarkupMatches(this INode actual, string expected, string? use
/// <param name="actual">The list of nodes to verify.</param>
/// <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)
{
if (actual is null)
Expand All @@ -198,6 +209,7 @@ public static void MarkupMatches(this INodeList actual, string expected, string?
/// <param name="actual">The list of nodes to verify.</param>
/// <param name="expected">The expected list of nodes.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INodeList actual, INodeList expected, string? userMessage = null)
{
var diffs = actual.CompareTo(expected);
Expand All @@ -215,6 +227,7 @@ public static void MarkupMatches(this INodeList actual, INodeList expected, stri
/// <param name="actual">The list of nodes to verify.</param>
/// <param name="expected">The expected node.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INodeList actual, INode expected, string? userMessage = null)
{
var diffs = actual.CompareTo(expected);
Expand All @@ -232,6 +245,7 @@ public static void MarkupMatches(this INodeList actual, INode expected, string?
/// <param name="actual">The node to verify.</param>
/// <param name="expected">The expected list of nodes.</param>
/// <param name="userMessage">A custom user message to display in case the verification fails.</param>
[AssertionMethod]
public static void MarkupMatches(this INode actual, INodeList expected, string? userMessage = null)
{
var diffs = actual.CompareTo(expected);
Expand Down

0 comments on commit c222cf7

Please sign in to comment.