From 484cff5d66b5b08f07cc5128e99366deb1ab9242 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Mon, 18 Dec 2023 08:52:08 +0100 Subject: [PATCH] Assert more default overloads --- src/FSharpPlus/Control/Collection.fs | 35 +++++++++++++--- tests/FSharpPlus.Tests/Collections.fs | 58 ++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/FSharpPlus/Control/Collection.fs b/src/FSharpPlus/Control/Collection.fs index 00d4dfd7b..9a832ee7b 100644 --- a/src/FSharpPlus/Control/Collection.fs +++ b/src/FSharpPlus/Control/Collection.fs @@ -13,10 +13,23 @@ open FSharpPlus.Internals type OfSeq = inherit Default1 - static member inline OfSeq ((x: seq<'t> , _: 'R ), _: Default5) = (^R : (new : seq<'t> -> ^R) x) : 'R - static member inline OfSeq ((x: seq>, _: 'R ), _: Default5) = (^R : (new : seq<'k*'v> -> ^R) (Seq.map (|KeyValue|) x)) : 'R + static member inline OfSeq ((x: seq<'t>, _: 'R), _: Default5) : 'R = + #if TEST_TRACE + Traces.add "OfSeq, Default5-seq<'t>" + #endif + (^R : (new : seq<'t> -> ^R) x) + + static member inline OfSeq ((x: seq>, _: 'R), _: Default5) : 'R = + #if TEST_TRACE + Traces.add "OfSeq, Default5-seq>" + #endif + (^R : (new : seq<'k*'v> -> ^R) (Seq.map (|KeyValue|) x)) - static member inline OfSeq ((x: seq<'t> , _: '``Foldable'`` ), _: Default4) = x |> Seq.map Return.Invoke |> Sum.Invoke : '``Foldable'`` + static member inline OfSeq ((x: seq<'t>, _: '``Foldable'``), _: Default4) : '``Foldable'`` = + #if TEST_TRACE + Traces.add "OfSeq, Default4-seq<'t>" + #endif + x |> Seq.map Return.Invoke |> Sum.Invoke static member OfSeq ((x: seq<'t> , _: seq<'t> ), _: Default3) = x static member OfSeq ((x: seq<'t> , _: ICollection<'t> ), _: Default3) = let d = ResizeArray () in Seq.iter d.Add x; d :> ICollection<'t> @@ -34,7 +47,13 @@ type OfSeq = static member OfSeq ((x: seq<'t> , _: IReadOnlyCollection<'t> ), _: Default3) = IReadOnlyCollection.ofSeq x #endif - static member inline OfSeq ((x: seq<'t> , _: 'F ), _: Default2) = let c = new 'F () in (Seq.iter (fun t -> ( ^F : (member Add : 't -> ^R) c, t) |> ignore) x); c + static member inline OfSeq ((x: seq<'t>, _: 'F), _: Default2) = + #if TEST_TRACE + Traces.add "OfSeq, Default2-#Add" + #endif + let coll = new 'F () + (Seq.iter (fun t -> (^F : (member Add : 't -> ^R) coll, t) |> ignore) x) + coll #if !FABLE_COMPILER static member OfSeq ((x: seq<'t> , _: 'T when 'T :> ICollection<'t> ), _: Default1) = let d = new 'T () in x |> Seq.iter d.Add; d @@ -83,7 +102,13 @@ type OfList = #endif static member OfList ((x: list<'t> , _: IReadOnlyCollection<'t> ), _: Default4) = IReadOnlyCollection.ofSeq x - static member inline OfList ((x: list<'t> , _: 'F ), _: Default2) = let c = new 'F () in (List.iter (fun t -> ( ^F : (member Add : 't -> ^R) c, t) |> ignore) x); c + static member inline OfList ((x: list<'t>, _: 'F), _: Default2) = + #if TEST_TRACE + Traces.add "OfList, Default2-#Add" + #endif + let coll = new 'F () + List.iter (fun t -> (^F : (member Add : 't -> ^R) coll, t) |> ignore) x + coll #if !FABLE_COMPILER static member OfList ((x: list<'t> , _: 'T when 'T :> ICollection<'t> ), _: Default1) = let d = new 'T () in x |> List.iter d.Add; d diff --git a/tests/FSharpPlus.Tests/Collections.fs b/tests/FSharpPlus.Tests/Collections.fs index cf6882dc8..8694fea47 100644 --- a/tests/FSharpPlus.Tests/Collections.fs +++ b/tests/FSharpPlus.Tests/Collections.fs @@ -27,7 +27,7 @@ module Collections = #if TEST_TRACE CollectionAssert.AreEqual (["ChunkBy, list<'T>"], Traces.get()) - #endif + #endif let testCollections = @@ -52,6 +52,11 @@ module Collections = let testSeqConversions = + + #if TEST_TRACE + Traces.reset() + #endif + let sk: Generic.Stack<_> = ofSeq { 1 .. 3 } let sg: string = ofSeq {'1'..'3'} // but it will come back as seq let sb: Text.StringBuilder = ofSeq {'1'..'3'} // but it will come back as seq @@ -76,12 +81,29 @@ module Collections = let _r: IReadOnlyDictionary<_,_> = ofSeq (seq [KeyValuePair(1, "One"); KeyValuePair(2, "Two")]) let rc: IReadOnlyCollection<_> = ofSeq (seq [2..7]) let ut: Hashtable = ofSeq (seq [1,'1';2, '2';3,'3']) // but it will come back as seq + + #if TEST_TRACE + CollectionAssert.AreEqual ([], Traces.get()) + #endif + let al: ArrayList = ofSeq (seq ["1";"2";"3"]) // but it will come back as seq - let us: SortedList = ofSeq (seq [4,'2';3,'4']) // but it will come back as seq + #if TEST_TRACE + CollectionAssert.AreEqual (["OfSeq, Default2-#Add"], Traces.get()) + #endif + let cc: BlockingCollection<_> = ofSeq {'1'..'3'} // but it will come back as seq + #if TEST_TRACE + CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get()) + #endif + + let cb: ConcurrentBag<_> = ofSeq {'1'..'3'} + #if TEST_TRACE + CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get()) + #endif + + let us: SortedList = ofSeq (seq [4,'2';3,'4']) // but it will come back as seq let cd: ConcurrentDictionary<_,_> = ofSeq (seq [(1, "One"); (2, "Two")]) // but it will come back as ... let _cd:ConcurrentDictionary<_,_> = ofSeq (seq [KeyValuePair(1, "One"); KeyValuePair(2, "Two")]) - let cb: ConcurrentBag<_> = ofSeq {'1'..'3'} // now go back let _sk' = toSeq sk @@ -119,7 +141,15 @@ module Collections = let _cols = columns |> toList |> map (fun x -> x.ColumnName) // Defaults + + #if TEST_TRACE + CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get()) + #endif + let _12: WrappedListI<_> = seq [1;2] |> ofSeq + #if TEST_TRACE + CollectionAssert.AreEqual (["OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default4-seq<'t>"], Traces.get()) + #endif () @@ -150,12 +180,30 @@ module Collections = let _r: IReadOnlyDictionary<_,_> = ofList ([KeyValuePair(1, "One"); KeyValuePair(2, "Two")]) let rc: IReadOnlyCollection<_> = ofList ([2..5]) let ut: Hashtable = ofList ([1,'1';2, '2';3,'3']) // but it will come back as seq + + #if TEST_TRACE + CollectionAssert.AreEqual ([], Traces.get()) + #endif + let al: ArrayList = ofList (["1";"2";"3"]) // but it will come back as seq + #if TEST_TRACE + CollectionAssert.AreEqual (["OfList, Default2-#Add"], Traces.get()) + #endif + + let cc: BlockingCollection<_> = ofList ['1'..'3'] // but it will come back as seq + #if TEST_TRACE + CollectionAssert.AreEqual (["OfList, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get()) + #endif + + let cb: ConcurrentBag<_> = ofList ['1'..'3'] + #if TEST_TRACE + CollectionAssert.AreEqual (["OfList, Default2-#Add"; "OfSeq, Default2-#Add"; "OfSeq, Default2-#Add"], Traces.get()) + #endif + + let us: SortedList = ofList ([4,'2';3,'4']) // but it will come back as seq - let cc: BlockingCollection<_> = ofList ['1'..'3'] // but it will come back as seq let cd: ConcurrentDictionary<_,_> = ofList ([(1, "One"); (2, "Two")]) // but it will come back as ... let _cd:ConcurrentDictionary<_,_> = ofList ([KeyValuePair(1, "One"); KeyValuePair(2, "Two")]) - let cb: ConcurrentBag<_> = ofList ['1'..'3'] // now go back let _sk' = toList sk