I'm looking for a clean set of Test Specific Equality control methods in F # unit tests . 90% of the time, standard structural equality corresponds to the bill, and I can use its unquote to express the connection between mine result
and my expected
.
TL DR "I cannot find a clean way to have my own Equality function for one or two properties in a value that 90% of them are well served by structural equality, does F # have a way to map an arbitrary record to a regular equality for only one or two fields?"
An example of a common technique that works for me
When checking a function that maps a 1: 1 data type to a different data type, I often extract the appropriate tuples from both sides in some cases and compare the input and output sets. For example, I have a statement: -
let (====) x y = (x |> Set.ofSeq) = (y |> Set.ofSeq)
So, I can do:
let inputs = ["KeyA",DateTime.Today; "KeyB",DateTime.Today.AddDays(1); "KeyC",DateTime.Today.AddDays(2)]
let trivialFun (a:string,b) = a.ToLower(),b
let expected = inputs |> Seq.map trivialFun
let result = inputs |> MyMagicMapper
test <@ expected ==== actual @>
This allows me Assert
so that each of my inputs is displayed on the output without extra outputs.
Problem
The problem is when I want to have my own comparison for one or two fields.
For example, if my DateTime goes through a layer with a low degree of serialization using SUT, I need a test tolerance comparison DateTime
. Or maybe I want to do case-insensitive validation for a fieldstring
, Mark Seemann SemanticComparison library Likeness<Source,Destination>
, Test Specific, :
- tuples: F #
.ItemX
Tuple
, .With
Expression<T>
- : TTBOMK
sealed
F # , SemanticComparison Object.Equals
, , - , .
, , ( , , IEqualityComparer
, ?)
- (.. F # Set
). :
let sut (a:string,b:DateTime) = a.ToLower(),b + TimeSpan.FromTicks(1L)
let inputs = ["KeyA",DateTime.Today; "KeyB",DateTime.Today.AddDays(1.0); "KeyC",DateTime.Today.AddDays(2.0)]
let toResemblance (a,b) = TODO generate Resemblance which will case insensitively compare fst and tolerantly compare snd
let expected = inputs |> List.map toResemblance
let result = inputs |> List.map sut
test <@ expected = result @>