Skip to content

Commit bc8c0d0

Browse files
xperiandriclaude
andcommitted
fixup! test: contracts of Stack and IStack
Tests of the sequence inputs. `StackContractTests` derives the shared contracts, which now check that `Stack.ofSeq` rejects a default flat list given as a sequence as it rejects a null one, that `concat` and `transpose` name `stacks` for a null sequence and reject a default flat list as they reject null, and that `except` rejects a default flat list given as the items to exclude, on an empty and on a non-empty stack, for `Stack` and `IStack`. The rest is per module, for what a contract over integers cannot say: - `StackTests` and `IStackTests` have a table with the rows of `consMany`, which no contract covers, of a default flat list of strings given as a sequence of objects, which a type test for the flat list of objects does not see, and of the order of the checks of `except`, which checks the items before the stack as `List.except` does. A flat list that is not default still passes as a sequence of integers, of stacks and of objects. - `StackComputationExpressionTests` has a table for `yield!` and `for` with a null sequence, a default flat list and a default flat list of strings as objects, and one for the members of the builder called without running an expression, so that the sequence, the array and the stack of `YieldFrom` and the sequence of `For` are checked when the expression calls the member. - The rows of `ofSeq`, `concat` and `transpose` of the table of other null inputs of `StackTests` are gone: the shared contracts check the same null sequence and the same parameter name for every module. Breaking each fix once in a copy of the sources (27 changes: the check back to a null check, the check removed, the check of the expression moved from the call of the member into the buffer, the checks of `except` in the other order) fails the tests in every case. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent b0d8861 commit bc8c0d0

3 files changed

Lines changed: 288 additions & 14 deletions

File tree

‎tests/FSharp.Collections.Immutable.Tests/Stack/IStackTests.fs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,37 @@ type IStackTests () =
271271
|> Array.map (fun struct (name, paramName, action) -> name, struct (paramName, action))
272272
|> Map.ofArray
273273

274+
/// The functions that take a sequence, by name and by the input that they reject, with the parameter that names the
275+
/// input and the function on it. The shared contracts cover a null sequence and a default flat list given as a
276+
/// sequence of integers for concat, transpose and except; the rows are consMany, which no contract covers, a default
277+
/// flat list of strings given as a sequence of objects, which a type test for the flat list of objects does not see,
278+
/// and the order of the checks. A default flat list converts to a sequence without a check and fails when the sequence
279+
/// is enumerated, so the other inputs are valid.
280+
static let sequenceRejections : Map<string, struct (string * (unit -> unit))> =
281+
let widenedItems = FlatListOracles.defaultStringsAsObjects
282+
let objects = Stack.ofArray [| "a" :> obj |] :> IImmutableStack<obj>
283+
let defaultItems = Inputs.DefaultFlatListAsSeq<int>()
284+
285+
Map.ofArray [|
286+
"consMany (default flat list)", struct ("values", (fun () -> IStack.consMany defaultItems (several ()) |> ignore))
287+
"consMany (default flat list of strings as objects)",
288+
struct ("values", (fun () -> IStack.consMany widenedItems objects |> ignore))
289+
"except (default flat list of strings as objects)",
290+
struct ("itemsToExclude", (fun () -> IStack.except widenedItems objects |> ignore))
291+
// List.except checks the items to exclude before the list
292+
"except (default flat list and null stack)",
293+
struct ("itemsToExclude", (fun () -> IStack.except defaultItems nullStack |> ignore))
294+
|]
295+
274296
/// The names of the functions whose result has the element type of their input, as the rows of their test.
275297
static member SameElementType = sameElementType.Keys
276298

277299
/// The names of the functions that take a stack, as the rows of their test.
278300
static member NullStackRejections = nullStackRejections.Keys
279301

302+
/// The functions that take a sequence and the inputs that they reject, as the rows of their test.
303+
static member SequenceRejections = sequenceRejections.Keys
304+
280305
[<TestMethod; DynamicData(nameof IStackTests.SameElementType)>]
281306
member _.``functions with the element type of their input keep its implementation`` (name : string) =
282307
let apply = sameElementType[name]
@@ -402,6 +427,63 @@ type IStackTests () =
402427

403428
Assert.AreEqual<string | null>("values", ex.ParamName, "IStack.consMany must name the values parameter")
404429

430+
[<TestMethod; DynamicData(nameof IStackTests.SequenceRejections)>]
431+
member _.``functions that take a sequence reject a default flat list and check their inputs in order`` (name : string) =
432+
let struct (paramName, actual) = sequenceRejections[name]
433+
FlatListAssert.RejectsNull ($"IStack.%s{name}", paramName, actual)
434+
435+
[<TestMethod>]
436+
member _.``a flat list that is not default passes as a sequence`` () =
437+
let flat = FlatList.ofArray [| 1; 2; 3 |] :> seq<int>
438+
439+
let stacks =
440+
FlatList.ofArray [| Stack.ofArray [| 1; 2 |] :> IStack<int>; Stack.ofArray [| 3; 4 |] :> IStack<int> |]
441+
:> seq<IStack<int>>
442+
443+
let single = IStack.cons 99 (IStack.clear (tagged ()))
444+
445+
CollectionAssert.AreEqual (
446+
[| 1; 2; 3; 99 |],
447+
IStack.toArray (IStack.consMany flat single),
448+
"IStack.consMany must take the elements of a flat list"
449+
)
450+
451+
CollectionAssert.AreEqual (
452+
[| 99 |],
453+
IStack.toArray (IStack.except flat (IStack.cons 2 single)),
454+
"IStack.except must take the elements of a flat list"
455+
)
456+
457+
CollectionAssert.AreEqual (
458+
[| 1; 2; 3; 4 |],
459+
Stack.toArray (IStack.concat stacks),
460+
"IStack.concat must take the stacks of a flat list"
461+
)
462+
463+
CollectionAssert.AreEqual (
464+
[| 1; 3 |],
465+
Stack.toArray (Stack.head (IStack.transpose stacks)),
466+
"IStack.transpose must take the stacks of a flat list"
467+
)
468+
469+
[<TestMethod>]
470+
member _.``a flat list of strings that is not default passes as a sequence of objects`` () =
471+
let widened = FlatListOracles.stringsAsObjects (FlatList.ofArray [| "a"; "b" |])
472+
let asObjects (strings : string array) = strings |> Array.map (fun item -> item :> obj)
473+
let objects = Stack.ofArray (asObjects [| "c" |]) :> IStack<obj>
474+
475+
CollectionAssert.AreEqual (
476+
asObjects [| "a"; "b"; "c" |],
477+
IStack.toArray (IStack.consMany widened objects),
478+
"IStack.consMany must take the elements of a flat list of strings as objects"
479+
)
480+
481+
CollectionAssert.AreEqual (
482+
asObjects [| "c" |],
483+
IStack.toArray (IStack.except widened (Stack.ofArray (asObjects [| "a"; "c"; "b" |]))),
484+
"IStack.except must take the elements of a flat list of strings as objects"
485+
)
486+
405487
[<TestMethod>]
406488
member _.``uncons throws for an empty stack as List.head does`` () =
407489
ExceptionAssert.Same (

‎tests/FSharp.Collections.Immutable.Tests/Stack/StackComputationExpressionTests.fs‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,83 @@ type StackComputationExpressionTests () =
1717
"stack", (fun () -> stack { yield! Unchecked.defaultof<Stack<int>> })
1818
|]
1919

20+
/// The sequences that yield! and for reject, by the construct and the input, with the parameter that names the input and
21+
/// the expression. A default flat list converts to a sequence without a check and fails when the sequence is
22+
/// enumerated, and the flat list of strings is a sequence of objects, which a type test for the flat list of objects
23+
/// does not see.
24+
static let sequenceRejections : Map<string, struct (string * (unit -> unit))> =
25+
let nullSequence = Unchecked.defaultof<seq<int>>
26+
let defaultSequence = Inputs.DefaultFlatListAsSeq<int>()
27+
let widenedSequence = FlatListOracles.defaultStringsAsObjects
28+
29+
Map.ofArray [|
30+
"yield! (null sequence)", struct ("items", (fun () -> stack { yield! nullSequence } |> ignore))
31+
"yield! (default flat list)", struct ("items", (fun () -> stack { yield! defaultSequence } |> ignore))
32+
"yield! (default flat list of strings as objects)",
33+
struct ("items", (fun () -> stack { yield! widenedSequence } |> ignore))
34+
"for (null sequence)",
35+
struct ("source",
36+
(fun () ->
37+
stack {
38+
for item in nullSequence do
39+
yield item
40+
}
41+
|> ignore
42+
))
43+
"for (default flat list)",
44+
struct ("source",
45+
(fun () ->
46+
stack {
47+
for item in defaultSequence do
48+
yield item
49+
}
50+
|> ignore
51+
))
52+
"for (default flat list of strings as objects)",
53+
struct ("source",
54+
(fun () ->
55+
stack {
56+
for item in widenedSequence do
57+
yield item
58+
}
59+
|> ignore
60+
))
61+
|]
62+
63+
/// The members of the builder that take a collection, called on the builder without running an expression: the builder
64+
/// checks the input when the expression calls the member, not when the collected elements are added to the buffer.
65+
static let builderCalls : Map<string, struct (string * (unit -> unit))> =
66+
let nullSequence = Unchecked.defaultof<seq<int>>
67+
let defaultSequence = Inputs.DefaultFlatListAsSeq<int>()
68+
69+
Map.ofArray [|
70+
"YieldFrom (null sequence)", struct ("items", (fun () -> stack.YieldFrom nullSequence |> ignore))
71+
"YieldFrom (default flat list)", struct ("items", (fun () -> stack.YieldFrom defaultSequence |> ignore))
72+
"YieldFrom (null array)", struct ("items", (fun () -> stack.YieldFrom (Unchecked.defaultof<int array>) |> ignore))
73+
"YieldFrom (null stack)", struct ("items", (fun () -> stack.YieldFrom (Unchecked.defaultof<Stack<int>>) |> ignore))
74+
"For (null sequence)",
75+
struct ("source",
76+
(fun () ->
77+
stack.For (nullSequence, (fun item -> stack.Yield item))
78+
|> ignore
79+
))
80+
"For (default flat list)",
81+
struct ("source",
82+
(fun () ->
83+
stack.For (defaultSequence, (fun item -> stack.Yield item))
84+
|> ignore
85+
))
86+
|]
87+
2088
/// The shapes of the null inputs of yield!, as the rows of their test.
2189
static member NullYields = nullYields.Keys
2290

91+
/// The constructs and the inputs that take a sequence, as the rows of the test of the sequences that they reject.
92+
static member SequenceRejections = sequenceRejections.Keys
93+
94+
/// The members of the builder that take a collection, as the rows of the test of the inputs that they reject.
95+
static member BuilderCalls = builderCalls.Keys
96+
2397
[<TestMethod>]
2498
member _.``an expression without elements is empty`` () =
2599
let result : Stack<int> = stack { () }
@@ -79,6 +153,47 @@ type StackComputationExpressionTests () =
79153

80154
Assert.AreEqual<string | null>("items", ex.ParamName, $"stack must name the parameter of the null %s{shape}")
81155

156+
[<TestMethod; DynamicData(nameof StackComputationExpressionTests.SequenceRejections)>]
157+
member _.``yield! and for reject a null sequence, a default flat list and a default flat list of strings`` (name : string) =
158+
let struct (paramName, expression) = sequenceRejections[name]
159+
FlatListAssert.RejectsNull ($"stack with %s{name}", paramName, expression)
160+
161+
[<TestMethod; DynamicData(nameof StackComputationExpressionTests.BuilderCalls)>]
162+
member _.``the builder checks its input when the expression calls the member`` (name : string) =
163+
let struct (paramName, call) = builderCalls[name]
164+
FlatListAssert.RejectsNull ($"StackCE.%s{name}", paramName, call)
165+
166+
[<TestMethod>]
167+
member _.``yield! and for take a flat list that is not default as a sequence`` () =
168+
let flat = FlatList.ofArray [| 1; 2 |] :> seq<int>
169+
let widened = FlatListOracles.stringsAsObjects (FlatList.ofArray [| "a"; "b" |])
170+
171+
let numbers = stack {
172+
yield! flat
173+
174+
for item in flat do
175+
yield item * 10
176+
}
177+
178+
CollectionAssert.AreEqual (
179+
[| 1; 2; 10; 20 |],
180+
Stack.toArray numbers,
181+
"stack must take the elements of a flat list in yield! and for"
182+
)
183+
184+
let objects = stack {
185+
yield! widened
186+
187+
for item in widened do
188+
yield item
189+
}
190+
191+
CollectionAssert.AreEqual (
192+
[| "a" :> obj; "b" :> obj; "a" :> obj; "b" :> obj |],
193+
Stack.toArray objects,
194+
"stack must take the elements of a flat list of strings as objects in yield! and for"
195+
)
196+
82197
[<TestMethod>]
83198
member _.``for adds the elements of the body for each item`` () =
84199
let result = stack {

‎tests/FSharp.Collections.Immutable.Tests/Stack/StackTests.fs‎

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,35 +264,45 @@ type StackTests () =
264264
|> Map.ofArray
265265

266266
/// The functions that take another input that can be null, by name, with the name of that parameter and the function
267-
/// on a null input.
267+
/// on a null input. The shared contracts cover a null sequence for ofSeq, concat, transpose and except.
268268
static let otherNullRejections =
269269
let stack = several ()
270270

271271
[|
272272
struct (nameof Stack.ofArray, "array", (fun () -> Stack.ofArray (Unchecked.defaultof<int array>) |> ignore))
273-
struct (nameof Stack.ofSeq, "source", (fun () -> Stack.ofSeq (Unchecked.defaultof<seq<int>>) |> ignore))
274-
struct (nameof Stack.concat,
275-
"stacks",
276-
(fun () ->
277-
Stack.concat (Unchecked.defaultof<seq<Stack<int>>>)
278-
|> ignore
279-
))
280273
struct (nameof Stack.consMany,
281274
"values",
282275
(fun () ->
283276
Stack.consMany (Unchecked.defaultof<seq<int>>) stack
284277
|> ignore
285278
))
286-
struct (nameof Stack.transpose,
287-
"stacks",
288-
(fun () ->
289-
Stack.transpose (Unchecked.defaultof<seq<Stack<int>>>)
290-
|> ignore
291-
))
292279
|]
293280
|> Array.map (fun struct (name, paramName, action) -> name, struct (paramName, action))
294281
|> Map.ofArray
295282

283+
/// The functions that take a sequence, by name and by the input that they reject, with the parameter that names the
284+
/// input and the function on it. The shared contracts cover a null sequence and a default flat list given as a
285+
/// sequence of integers for ofSeq, concat, transpose and except; the rows are consMany, which no contract covers, a
286+
/// default flat list of strings given as a sequence of objects, which a type test for the flat list of objects does
287+
/// not see, and the order of the checks. A default flat list converts to a sequence without a check and fails when the
288+
/// sequence is enumerated, so the other inputs are valid.
289+
static let sequenceRejections : Map<string, struct (string * (unit -> unit))> =
290+
let widenedItems = FlatListOracles.defaultStringsAsObjects
291+
let objects = Stack.ofArray [| "a" :> obj |]
292+
let defaultItems = Inputs.DefaultFlatListAsSeq<int>()
293+
294+
Map.ofArray [|
295+
"ofSeq (default flat list of strings as objects)", struct ("source", (fun () -> Stack.ofSeq widenedItems |> ignore))
296+
"consMany (default flat list)", struct ("values", (fun () -> Stack.consMany defaultItems (several ()) |> ignore))
297+
"consMany (default flat list of strings as objects)",
298+
struct ("values", (fun () -> Stack.consMany widenedItems objects |> ignore))
299+
"except (default flat list of strings as objects)",
300+
struct ("itemsToExclude", (fun () -> Stack.except widenedItems objects |> ignore))
301+
// List.except checks the items to exclude before the list
302+
"except (default flat list and null stack)",
303+
struct ("itemsToExclude", (fun () -> Stack.except defaultItems nullStack |> ignore))
304+
|]
305+
296306
/// The functions of two stacks, by name, with the List function that they mirror, as functions of the action that
297307
/// records the pairs; both run on a shorter and a longer input.
298308
static let commonPrefixWalks =
@@ -437,6 +447,9 @@ type StackTests () =
437447
/// The names of the functions that take another input that can be null, as the rows of their test.
438448
static member OtherNullRejections = otherNullRejections.Keys
439449

450+
/// The functions that take a sequence and the inputs that they reject, as the rows of their test.
451+
static member SequenceRejections = sequenceRejections.Keys
452+
440453
/// The names of the functions of two stacks, as the rows of the test of the common prefix.
441454
static member CommonPrefixWalks = commonPrefixWalks.Keys
442455

@@ -717,6 +730,70 @@ type StackTests () =
717730

718731
Assert.AreEqual<string | null>(paramName, ex.ParamName, $"Stack.%s{name} must name the null parameter")
719732

733+
[<TestMethod; DynamicData(nameof StackTests.SequenceRejections)>]
734+
member _.``functions that take a sequence reject a default flat list and check their inputs in order`` (name : string) =
735+
let struct (paramName, actual) = sequenceRejections[name]
736+
FlatListAssert.RejectsNull ($"Stack.%s{name}", paramName, actual)
737+
738+
[<TestMethod>]
739+
member _.``a flat list that is not default passes as a sequence`` () =
740+
let flat = FlatList.ofArray [| 1; 2; 3 |] :> seq<int>
741+
let stacks =
742+
FlatList.ofArray [| Stack.ofArray [| 1; 2 |]; Stack.ofArray [| 3; 4 |] |] :> seq<Stack<int>>
743+
744+
CollectionAssert.AreEqual (
745+
[| 1; 2; 3 |],
746+
Stack.toArray (Stack.ofSeq flat),
747+
"Stack.ofSeq must take the elements of a flat list"
748+
)
749+
750+
CollectionAssert.AreEqual (
751+
[| 1; 2; 3; 9 |],
752+
Stack.toArray (Stack.consMany flat (Stack.ofArray [| 9 |])),
753+
"Stack.consMany must take the elements of a flat list"
754+
)
755+
756+
CollectionAssert.AreEqual (
757+
[| 9 |],
758+
Stack.toArray (Stack.except flat (Stack.ofArray [| 2; 9; 3 |])),
759+
"Stack.except must take the elements of a flat list"
760+
)
761+
762+
CollectionAssert.AreEqual (
763+
[| 1; 2; 3; 4 |],
764+
Stack.toArray (Stack.concat stacks),
765+
"Stack.concat must take the stacks of a flat list"
766+
)
767+
768+
CollectionAssert.AreEqual (
769+
[| 1; 3 |],
770+
Stack.toArray (Stack.head (Stack.transpose stacks)),
771+
"Stack.transpose must take the stacks of a flat list"
772+
)
773+
774+
[<TestMethod>]
775+
member _.``a flat list of strings that is not default passes as a sequence of objects`` () =
776+
let widened = FlatListOracles.stringsAsObjects (FlatList.ofArray [| "a"; "b" |])
777+
let asObjects (strings : string array) = strings |> Array.map (fun item -> item :> obj)
778+
779+
CollectionAssert.AreEqual (
780+
asObjects [| "a"; "b" |],
781+
Stack.toArray (Stack.ofSeq widened),
782+
"Stack.ofSeq must take the elements of a flat list of strings as objects"
783+
)
784+
785+
CollectionAssert.AreEqual (
786+
asObjects [| "a"; "b"; "c" |],
787+
Stack.toArray (Stack.consMany widened (Stack.ofArray (asObjects [| "c" |]))),
788+
"Stack.consMany must take the elements of a flat list of strings as objects"
789+
)
790+
791+
CollectionAssert.AreEqual (
792+
asObjects [| "c" |],
793+
Stack.toArray (Stack.except widened (Stack.ofArray (asObjects [| "a"; "c"; "b" |]))),
794+
"Stack.except must take the elements of a flat list of strings as objects"
795+
)
796+
720797
// ---- Semantics of List ----
721798

722799
[<TestMethod>]

0 commit comments

Comments
 (0)