Skip to content

Commit

Permalink
Try to differentiate between null and empty strings in test theories (#…
Browse files Browse the repository at this point in the history
…494)

* Try to differentiate between null and empty strings in test theories

* Group theory tests showing null and empty string produce different test names

* Try wrapping string parameters to test theories in quotes to male any leading/trailing whitespace more visible

---------

Co-authored-by: Spencer Farley <2847259+farlee2121@users.noreply.github.com>
  • Loading branch information
Numpsy and farlee2121 committed Apr 4, 2024
1 parent cfa55e6 commit f359e21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
15 changes: 14 additions & 1 deletion Expecto.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1841,4 +1841,17 @@ let theory =
testTheoryTask "task odd numbers" [1; 3; 5;] <| fun x -> task {
Expect.isTrue (x % 2 = 1) "should be odd"
}
]

testList "Null and empty string cases should result in different test names" [
testTheory "testTheory" [""; null] <| fun x ->
Expect.isTrue (System.String.IsNullOrEmpty(x)) "should be null or empty"

testTheoryTask "testTheoryTask" [""; null] <| fun x -> task {
Expect.isTrue (System.String.IsNullOrEmpty(x)) "should be null or empty"
}

testTheoryAsync "testTheoryAsync" [""; null] <| fun x -> async {
Expect.isTrue (System.String.IsNullOrEmpty(x)) "should be null or empty"
}
]
]
24 changes: 15 additions & 9 deletions Expecto/Expecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,32 @@ module Tests =
/// Test case or list needs to run sequenced with other tests in this group.
let inline testSequencedGroup name test = Sequenced (SynchronousGroup name,test)

let inline private stringify (value : 'T) =
if typeof<'T> = typeof<string> then
if value = Unchecked.defaultof<'T> then "null" else $"\"{value}\""
else
string value

/// Applies a function to a list of values to build test cases
let inline testFixture setup =
Seq.map (fun (name, partialTest) ->
testCase name (setup partialTest))
/// Builds a theory test case
let inline testTheory name cases test =
let caseToTest case =
testCase (string case) <| fun () ->
testCase (stringify case) <| fun () ->
test case |> ignore
testList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Builds a theory test case that will make Expecto to ignore other unfocused tests
let inline ftestTheory name cases test =
let caseToTest case =
ftestCase (string case) <| fun () ->
ftestCase (stringify case) <| fun () ->
test case |> ignore
ftestList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Builds a theory test case that will be ignored by Expecto
let inline ptestTheory name cases test =
let caseToTest case =
ptestCase (string case) <| fun () ->
ptestCase (stringify case) <| fun () ->
test case |> ignore
ptestList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Applies a value to a list of partial tests
Expand Down Expand Up @@ -215,17 +221,17 @@ module Tests =
/// Builds an async theory test case
let inline testTheoryAsync name cases test =
let caseToTest case =
testAsync (string case) { do! test case }
testAsync (stringify case) { do! test case }
testList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Builds an async theory test case that will make Expecto to ignore other unfocused tests
let inline ftestTheoryAsync name cases test =
let caseToTest case =
ftestAsync (string case) { do! test case }
ftestAsync (stringify case) { do! test case }
ftestList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Builds an async theory test case that will be ignored by Expecto
let inline ptestTheoryAsync name cases test =
let caseToTest case =
ptestAsync (string case) { do! test case }
ptestAsync (stringify case) { do! test case }
ptestList name (cases |> Seq.map caseToTest |> List.ofSeq)

type TestTaskBuilder(name, focusState) =
Expand Down Expand Up @@ -271,17 +277,17 @@ module Tests =
/// Builds a task theory test case
let inline testTheoryTask name cases test =
let caseToTest case =
testTask (string case) { do! test case }
testTask (stringify case) { do! test case }
testList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Builds a task theory test case that will make Expecto to ignore other unfocused tests
let inline ftestTheoryTask name cases test =
let caseToTest case =
ftestTask (string case) { do! test case }
ftestTask (stringify case) { do! test case }
ftestList name (cases |> Seq.map caseToTest |> List.ofSeq)
/// Builds a task theory test case that will be ignored by Expecto
let inline ptestTheoryTask name cases test =
let caseToTest case =
ptestTask (string case) { do! test case }
ptestTask (stringify case) { do! test case }
ptestList name (cases |> Seq.map caseToTest |> List.ofSeq)

/// The default configuration for Expecto.
Expand Down

0 comments on commit f359e21

Please sign in to comment.