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

Improve caller identification for Visual Basic #2254

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ class Build : NukeBuild
Project[] Projects => new[]
{
Solution.Specs.FluentAssertions_Specs,
Solution.Specs.FluentAssertions_Equivalency_Specs
Solution.Specs.FluentAssertions_Equivalency_Specs,
Solution.Specs.FSharp_Specs,
Solution.Specs.VB_Specs
};

Target UnitTestsNetFramework => _ => _
Expand Down
18 changes: 18 additions & 0 deletions FluentAssertions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UWP.Specs", "Tests\UWP.Spec
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentAssertions.Equivalency.Specs", "Tests\FluentAssertions.Equivalency.Specs\FluentAssertions.Equivalency.Specs.csproj", "{A946043D-D3F8-46A4-B485-A88412C417FE}"
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VB.Specs", "Tests\VB.Specs\VB.Specs.vbproj", "{0C0211B6-D185-4518-A15A-38AC092EDC50}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Specs", "Tests\FSharp.Specs\FSharp.Specs.fsproj", "{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CI|Any CPU = CI|Any CPU
Expand Down Expand Up @@ -134,6 +138,18 @@ Global
{A946043D-D3F8-46A4-B485-A88412C417FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A946043D-D3F8-46A4-B485-A88412C417FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A946043D-D3F8-46A4-B485-A88412C417FE}.Release|Any CPU.Build.0 = Release|Any CPU
{0C0211B6-D185-4518-A15A-38AC092EDC50}.CI|Any CPU.ActiveCfg = Debug|Any CPU
{0C0211B6-D185-4518-A15A-38AC092EDC50}.CI|Any CPU.Build.0 = Debug|Any CPU
{0C0211B6-D185-4518-A15A-38AC092EDC50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C0211B6-D185-4518-A15A-38AC092EDC50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C0211B6-D185-4518-A15A-38AC092EDC50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C0211B6-D185-4518-A15A-38AC092EDC50}.Release|Any CPU.Build.0 = Release|Any CPU
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}.CI|Any CPU.ActiveCfg = Debug|Any CPU
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}.CI|Any CPU.Build.0 = Debug|Any CPU
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -152,6 +168,8 @@ Global
{6D31FFF8-E7FD-41D2-996C-CA8DDFDAE4FD} = {963262D0-9FD5-4741-8C0E-E2F34F110EF3}
{08087654-2C32-4818-95E4-45362373441D} = {963262D0-9FD5-4741-8C0E-E2F34F110EF3}
{A946043D-D3F8-46A4-B485-A88412C417FE} = {963262D0-9FD5-4741-8C0E-E2F34F110EF3}
{0C0211B6-D185-4518-A15A-38AC092EDC50} = {963262D0-9FD5-4741-8C0E-E2F34F110EF3}
{0A69DC62-CA14-44E5-BAF9-2EB2E2E2CADF} = {963262D0-9FD5-4741-8C0E-E2F34F110EF3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {75DDA3D8-9D6F-4865-93F4-DDE11DEE8290}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace FluentAssertions.CallerIdentification;

internal class ShouldCallParsingStrategy : IParsingStrategy
{
private const string ShouldCall = ".Should(";
private const string ShouldCall = ".Should";

public ParsingState Parse(char symbol, StringBuilder statement)
{
if (statement.Length >= ShouldCall.Length)
if (statement.Length >= ShouldCall.Length + 1)
{
var leftIndex = statement.Length - 1;
var leftIndex = statement.Length - 2;
var rightIndex = ShouldCall.Length - 1;

for (var i = 0; i < ShouldCall.Length; i++)
Expand All @@ -21,7 +21,12 @@ public ParsingState Parse(char symbol, StringBuilder statement)
}
}

statement.Remove(statement.Length - ShouldCall.Length, ShouldCall.Length);
if (statement[^1] is not ('(' or '.'))
{
return ParsingState.InProgress;
}

statement.Remove(statement.Length - (ShouldCall.Length + 1), ShouldCall.Length + 1);
return ParsingState.Done;
}

Expand Down
29 changes: 29 additions & 0 deletions Tests/FSharp.Specs/FSharp.Specs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>6.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<Compile Include="FSharpSpecs.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\FluentAssertions\FluentAssertions.csproj" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions Tests/FSharp.Specs/FSharpSpecs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace FSharp.Specs

open System
open FluentAssertions
open Xunit
open Xunit.Sdk

module FSharpSpecs =

[<Fact>]
let ``Caller identification works in F#`` () =
// Arrange
let subject = false

// Act
let act = Action(fun () -> subject.Should().BeTrue("") |> ignore)

// Assert
act.Should().Throw<XunitException>("").WithMessage("Expected subject to be true, but found false.", "")
24 changes: 24 additions & 0 deletions Tests/VB.Specs/VB.Specs.vbproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\FluentAssertions\FluentAssertions.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions Tests/VB.Specs/VBSpecs.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Imports FluentAssertions
Imports FluentAssertions.Execution
Imports FluentAssertions.Primitives
Imports Xunit
Imports Xunit.Sdk

Namespace VB.Specs
Public Class VBSpecs
<Fact>
Public Sub Caller_identification_works_with_parentheses()
' Arrange
Dim subject = False

' Act
Dim act As Action = Sub() subject.Should().BeTrue()

' Assert
act.Should().Throw(Of XunitException).WithMessage("Expected subject to be true, but found false.")
End Sub

<Fact>
Public Sub Caller_identification_works_without_parentheses()
' Arrange
Dim subject = False

' Act
Dim act As Action = Sub() subject.Should.BeTrue()

' Assert
act.Should().Throw(Of XunitException).WithMessage("Expected subject to be true, but found false.")
End Sub
End Class
End Namespace

1 change: 1 addition & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sidebar:

### Fixes
* `because` and `becauseArgs` were not included in the error message when collections of enums were not equivalent - [#2214](https://github.com/fluentassertions/fluentassertions/pull/2214)
* Improve caller identification for tests written in Visual Basic - [#2254](https://github.com/fluentassertions/fluentassertions/pull/2254)

## 6.11.0

Expand Down