TUnit icon indicating copy to clipboard operation
TUnit copied to clipboard

Added more f# tests

Open licon4812 opened this issue 8 months ago • 5 comments

This PR will add more Tests to the Example project. This is to ensure all Assertions and Attributes work correctly in F#

licon4812 avatar May 21 '25 04:05 licon4812

So far a couple of potential issues.

  • String Assertions don't seem to work in F#

    image

    No overloads match for method 'IsEqualTo'.  Known types of arguments: string * StringComparison  Available overloads:  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo(expected: DateTime, ?doNotPopulateThisValue1: string) : AssertionBuilders.Wrappers.DateTimeEqualToAssertionBuilderWrapper  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo(expected: DateTimeOffset, ?doNotPopulateThisValue1: string) : AssertionBuilders.Wrappers.DateTimeOffsetEqualToAssertionBuilderWrapper  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo(expected: TimeSpan, ?doNotPopulateThisValue1: string) : AssertionBuilders.Wrappers.TimeSpanEqualToAssertionBuilderWrapper  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo(expected: string, ?doNotPopulateThisValue1: string) : AssertionBuilders.Wrappers.StringEqualToAssertionBuilderWrapper  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo(expected: string, stringComparison: StringComparison, ?doNotPopulateThisValue1: string, ?doNotPopulateThisValue2: string) : AssertionBuilders.Wrappers.StringEqualToAssertionBuilderWrapper  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo<'TActual>(expected: 'TActual, ?doNotPopulateThisValue1: string) : AssertionBuilders.Wrappers.GenericEqualToAssertionBuilderWrapper<'TActual> // Argument 'expected' doesn't match  - (extension) AssertConditions.Interfaces.IValueSource.IsEqualTo<'TActual>(expected: 'TActual, equalityComparer: Collections.Generic.IEqualityComparer<'TActual>, ?doNotPopulateThisValue1: string) : AssertionBuilders.Wrappers.GenericEqualToAssertionBuilderWrapper<'TActual> // Argument 'expected' doesn't match
    
    

Update

  • So sometimes extension methods in c# cannot be found easily in f#. So in the case some of the assertions. I had to do something as scoffed as this. May need to declare or wrap them in the TUnit specifically.Assertions.FSharp library

    Example Test

     [<Test>]
     [<Category("Pass")>]
     member _.String_And_Condition() = async {
         do! check (TUnit.Assertions.Extensions.StringIsExtensions.IsEqualTo(Assert.That<string>("1"), "1").And.HasLength().EqualTo(1))
    
    // this also works
        do! check (Assert.That<string>(value).IsEqualTo("1"))
     }
    
     [<Test>]
     [<Category("Fail")>]
     member _.String_And_Condition2() = async {
         do! check (TUnit.Assertions.Extensions.StringIsExtensions.IsEqualTo(Assert.That<string>("1"), "2").And.HasLength().EqualTo(2))
     }
     ```
    
    

licon4812 avatar May 21 '25 04:05 licon4812

Assertions that Throw

  • Assertions seem to default to the ValueAssertions and not delegates. I am also unsure of what type they are meant to throw in the following tests
 [<Test>]
 [<Category("Fail")>]
 member _.Throws1() = async {
     do! check (Assert.That<Func<string>>(fun () -> new string([||])).ThrowsException())
 }

 [<Test>]
 [<Category("Fail")>]
 member _.Throws2() = async {
     do! check (Assert.That(fun () -> task { do! Task.Yield() }).ThrowsException())
 }

 [<Test>]
 [<Category("Pass")>]
 member _.Throws3() = async {
     do! check (Assert.That(fun () -> raise (ApplicationException())).ThrowsException())
 }

licon4812 avatar May 21 '25 07:05 licon4812

Annoying that F# doesn't pick up things as nicely!

thomhurst avatar May 21 '25 10:05 thomhurst

Annoying that F# doesn't pick up things as nicely!

Yeah, unlike VB, F# uses a completely different compiler and has a bunch of functional paradigms that don't really carry over.

Unlike VB, VB is basically older OOP so for the most part it works with the C# stuff. Although we will find out when I make more VB tests 🤣.

I will say, though, apart from a few things, F# has surprisingly picked up most of the C# stuff fine, considering they use two different compilers and paradigms

licon4812 avatar May 21 '25 10:05 licon4812

Assertions that Throw

  • Assertions seem to default to the ValueAssertions and not delegates. I am also unsure of what type they are meant to throw in the following tests
 [<Test>]
 [<Category("Fail")>]
 member _.Throws1() = async {
     do! check (Assert.That<Func<string>>(fun () -> new string([||])).ThrowsException())
 }

 [<Test>]
 [<Category("Fail")>]
 member _.Throws2() = async {
     do! check (Assert.That(fun () -> task { do! Task.Yield() }).ThrowsException())
 }

 [<Test>]
 [<Category("Pass")>]
 member _.Throws3() = async {
     do! check (Assert.That(fun () -> raise (ApplicationException())).ThrowsException())
 }

Fixed in #2431

Example assertion usage

[<Test>]
[<Category("Fail")>]
member _.Throws1() = async {
    do! check (Assert.That<string>(fun () -> task { return new string([||]) }).ThrowsException())
}

[<Test>]
[<Category("Fail")>]
member _.Throws2() = async {
    do! check (Assert.That<unit>(fun () -> task { do! Task.Yield() }).ThrowsException())
}

[<Test>]
[<Category("Pass")>]
member _.Throws3() = async {
    do! check (Assert.That(fun () -> raise (ApplicationException())).ThrowsException())
}

licon4812 avatar May 21 '25 23:05 licon4812

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Jun 23 '25 00:06 github-actions[bot]

This PR was closed because it has been stalled for 10 days with no activity.

github-actions[bot] avatar Jul 04 '25 00:07 github-actions[bot]