Skip to content

Commit

Permalink
Improve caller identification for Visual Basic
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Aug 13, 2023
1 parent 7428fe2 commit f7f2857
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
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

0 comments on commit f7f2857

Please sign in to comment.