Coder Social home page Coder Social logo

meziantou.fluentassertionsanalyzers's Introduction

NuGet build GitHub license

Meziantou.FluentAssertionsAnalyzers

Meziantou.FluentAssertionsAnalyzers is a Roslyn analyzer to help migrate from Xunit or NUnit assertions to Fluent Assertions.

Installation

Install the NuGet package: Meziantou.FluentAssertionsAnalyzers

Configuration

You can exclude assertion methods using the .editorconfig file:

[*.cs]
mfa_excluded_methods=M:NUnit.Framework.Assert.Fail;M:NUnit.Framework.Assert.Fail(System.String)

meziantou.fluentassertionsanalyzers's People

Contributors

greg-smulko avatar inyutin-maxim avatar jairbubbles avatar louis-z avatar meziantou avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

meziantou.fluentassertionsanalyzers's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • Update all (Meziantou.DotNet.CodingStandard, Microsoft.NET.Test.Sdk, System.Reflection.Metadata, actions/checkout, actions/download-artifact, actions/setup-dotnet, actions/upload-artifact, dotnet-sdk, xunit, xunit.runner.visualstudio)

Detected dependencies

github-actions
.github/workflows/publish.yml
  • actions/checkout v3
  • actions/setup-dotnet v3
  • actions/upload-artifact v3
  • actions/checkout v3
  • actions/setup-dotnet v3
  • actions/upload-artifact v3
  • actions/download-artifact v3
  • actions/setup-dotnet v3
nuget
Directory.Build.props
  • Meziantou.DotNet.CodingStandard 1.0.96
Meziantou.FluentAssertionsAnalyzers.Tests/Meziantou.FluentAssertionsAnalyzers.Tests.csproj
  • xunit.runner.visualstudio 2.4.5
  • xunit 2.4.2
  • System.Reflection.Metadata 7.0.0
  • Microsoft.NET.Test.Sdk 17.4.0
Meziantou.FluentAssertionsAnalyzers/Meziantou.FluentAssertionsAnalyzers.csproj
global.json
  • dotnet-sdk 8.0.100

  • Check this box to trigger a request for Renovate to run again on this repository

NullReferenceException in AssertAnalyzer

Thanks for this project, I've converted all our unit test projects to use FluentAssertions. I've left the analyzers in the projects to prevent Assert being used.

There are multiple unit test projects with the analyzer enabled but only one of them gives this error.

I tried creating a VSIX project and debugging the analyzer but I couldn't getting it going.

Any ideas why this null reference happens? or how to debug it locally?

##[warning]CSC(0,0): Warning AD0001: Analyzer 'Meziantou.FluentAssertionsAnalyzers.AssertAnalyzer' threw 
an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
CSC : warning AD0001: Analyzer 'Meziantou.FluentAssertionsAnalyzers.AssertAnalyzer' threw 
an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. [D:\a\1\s\OurApp\OurApp.Web.UnitTest\OurApp.Web.UnitTest.csproj]

Unit test project is .NET 4.8 and some referenced projects are .NET standard 2.0

Add Assert.NotStrictEqual migration

GeoLocation result = new GeoLocation
				{
					Latitude = 1.456789123,
					Longitude = 1.987654123
				};
Assert.NotStrictEqual(new GeoLocation
				{
					Latitude = 1.456789123,
					Longitude = 1.987654123
				}, result);

Can be replaced with:

result.Should()	.BeEquivalentTo(new GeoLocation
				{
					Latitude = 1.456789123,
					Longitude = 1.987654123
				}, options => options.WithoutStrictOrdering());

Apply fix in bulk (to the whole file/project/solution)

It would be amazing if the fix could be applied to the whole file/project/solution:

image

Currently, it's sadly not possible:
image

For context, I'd love to migrate our whole solution from native NUnit assertions to Fluent Assertions, but doing it one by one is not an option, as we have a gazillion of usages.

I wonder if that's hard, then maybe just a standalone console app for a one-go migration could be an option? Happy to give it a try myself if you think that would be easier.

Btw, I'm a big fan of your work. Big fan! ;) :)

Simplify result.Count

Assertion:

Assert.That(result.Count, Is.EqualTo(2));

can be replaced with:

result.Should().HaveCount(2);

now it replaced with:

result.Count.Should().Be(2);

Various bugs running on large codebase

Description

When running this analyzer on the codebase at https://github.com/SteeltoeOSS/Steeltoe, the fixers produce code that doesn't compile in several cases. After fixing them manually, various tests start to fail.

Repro steps

  • Clone the repo at https://github.com/SteeltoeOSS/Steeltoe
  • Add the following to /shared-test.props:
    <ItemGroup>
      <PackageReference Include="Meziantou.FluentAssertionsAnalyzers" Version="1.0.10" PrivateAssets="All" />
    </ItemGroup>
    <ItemGroup>
      <Using Include="FluentAssertions" />
      <Using Include="Xunit" />
    </ItemGroup>
  • Set all analyzer rules of this package to Warning severity in Steeltoe.Debug.ruleset: Open src/Steeltoe.All in Visual Studio, expand the Dependencies/Analyzers/FluentAssertions.Analyzers node in project Common/Steeltoe.Common.Expression.Test, select all rules, and right-click > Set severity > Warning.
  • Rebuild the Steeltoe.Common.Expression.Test project and use the fixers to update existing Xunit tests to FluentAssertions.

Expected behavior:

No broken code and no failing tests.

Actual behavior:

Some fixers don't actually change code; others produce code that doesn't compile. Some result in test failures.

Versions

  • Which version of FluentAssertionsAnalyzers are you using? v1.0.10 with FA v6.8.0
  • Which .NET runtime and version are you targeting? .NET 6

Additional Information

I only tried running on the Steeltoe.Common.Expression.Test project. Running on the complete solution may surface additional issues.

I noticed that the fixers add a namespace import for FluentAssertions at the top of the file, which is unneeded because it's already in the .props file. I'm not bothered by that, as it's easy to fix. Just wanted to point it out for awareness.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.