Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/227 assertionmethod #229

Merged
merged 5 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Bunit.Asserting;
egil marked this conversation as resolved.
Show resolved Hide resolved
using Bunit.Extensions.WaitForHelpers;

namespace Bunit
Expand Down
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