Skip to content

Commit 0f9552d

Browse files
xperiandriclaude
andcommitted
test(quotations): cover the parameterizer
47 functions of a person, every one of a shape of its own, are written twice over two sets of captured values; with the second set every choice takes its other branch. Five laws are checked on them: a function has one key and one hash code whatever values it captured, no two functions have one key, the key and the values are the same with and without the template, the template holds a variable for every slot and no captured value, and the template with the values put back computes what the compiled code computes. The compiler passes each function together with its quotation, and Unquote evaluates the template. The other tests state what the laws cannot: which values are slots, under which names and with which values, which default value is a literal and which is data, also for a struct whose constructor throws (a struct that F# cannot declare, which one helper makes at run time for this test and for the one of the evaluator), that two nodes are two slots whatever they hold, what is a part of the key, that a value which is bound once is computed once, that a quotation which is spliced into two places is one slot, that a branch that is not taken is not computed, what the functions of Query do, the tokens of a small key one by one, and every error. The check that tells which parts can be computed is tested with the evaluator: it accepts every part the evaluator computes, refuses a function, a variable and a quotation, and runs no code to tell. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0b34270 commit 0f9552d

8 files changed

Lines changed: 1691 additions & 25 deletions
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace FSharp.Azure.Cosmos.Quotations.Tests
2+
3+
open System
4+
open System.Reflection
5+
open System.Reflection.Emit
6+
7+
/// Structs that F# cannot declare, which a test makes at run time.
8+
[<RequireQualifiedAccess>]
9+
module internal DynamicStruct =
10+
11+
/// <summary>
12+
/// A struct with one field of an integer, named Value, and a constructor without parameters, as C# can declare
13+
/// one and F# cannot. The default value of such a struct is the one with every field zero: the constructor runs for
14+
/// a value that <see cref="M:System.Activator.CreateInstance(System.Type)"/> makes, and never for the default
15+
/// value.
16+
/// </summary>
17+
/// <param name="name">The name of the struct.</param>
18+
/// <param name="writeConstructor">
19+
/// Writes the code of the constructor, from its code generator and the field, up to and including the return or the
20+
/// throw that ends it.
21+
/// </param>
22+
let withConstructor (name : string) (writeConstructor : ILGenerator -> FieldInfo -> unit) : Type =
23+
let declared =
24+
AssemblyBuilder
25+
.DefineDynamicAssembly(AssemblyName $"{name}.Dynamic", AssemblyBuilderAccess.Run)
26+
.DefineDynamicModule("Structs")
27+
.DefineType(
28+
name,
29+
TypeAttributes.Public
30+
||| TypeAttributes.Sealed
31+
||| TypeAttributes.SequentialLayout,
32+
typeof<ValueType>
33+
)
34+
35+
let field = declared.DefineField ("Value", typeof<int>, FieldAttributes.Public)
36+
37+
let code =
38+
declared.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes).GetILGenerator()
39+
40+
writeConstructor code field
41+
declared.CreateType ()

‎tests/Cosmos.Quotations.Tests/EvaluatorTests.fs‎

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,50 @@ type EvaluatorTests () =
439439
"A version of FSharp.Core that keeps another operator from reflection needs another row in the evaluator."
440440
)
441441

442+
[<TestMethod>]
443+
member _.``Every part that the evaluator computes must be told computable`` () =
444+
let refused =
445+
seq {
446+
for computation in computations do
447+
if not (Evaluator.canEvaluate (Evaluator.Refused ()) computation.Part) then
448+
computation.Name
449+
450+
for struct (name, part, _) in failures do
451+
if not (Evaluator.canEvaluate (Evaluator.Refused ()) part) then
452+
name
453+
}
454+
|> Seq.toArray
455+
456+
Assert.IsEmpty (
457+
refused,
458+
"A part that has a value, or that raises while it is computed, is made only of what the evaluator computes."
459+
)
460+
461+
[<TestMethod>]
462+
member _.``A part that holds a function, a variable or a quotation must be told not computable, and none of it must run`` () =
463+
let meter = Meter ()
464+
let variable = Expr.Var (Var ("number", typeof<int>))
465+
466+
// In each of the first two the part before the one that cannot be computed can be: asking must not run it.
467+
let withVariable = <@@ [| meter.Read 1; %%variable |] @@>
468+
let withFunction = <@@ List.map (fun number -> number + 1) [ meter.Read 1 ] @@>
469+
let quotation = Expr.QuoteTyped <@@ 1 @@>
470+
471+
let refused = Evaluator.Refused ()
472+
473+
Assert.IsFalse (Evaluator.canEvaluate refused withVariable, "A variable has a value only inside the query.")
474+
Assert.IsFalse (Evaluator.canEvaluate refused withFunction, "A function cannot be computed without compiling it.")
475+
Assert.IsFalse (Evaluator.canEvaluate refused quotation, "A quotation inside the quotation is not computed.")
476+
Assert.AreEqual<int>(0, meter.Readings, "Asking whether a part can be computed should run no code of it.")
477+
478+
Assert.IsFalse (Evaluator.canEvaluate refused withVariable, "A part that was refused stays refused.")
479+
Assert.IsTrue (refused.Holds variable, "The variable is the reason, so it is remembered with the array around it.")
480+
481+
match withVariable with
482+
| NewArray (_, [ first; _ ]) ->
483+
Assert.IsTrue (Evaluator.canEvaluate refused first, "The item before the variable can be computed on its own.")
484+
| other -> Assert.Fail $"The quotation should be an array of two items: %A{other}"
485+
442486
[<TestMethod>]
443487
member _.``A value that is bound once must be computed once for all its places in a part`` () =
444488
let meter = Meter ()
@@ -508,29 +552,16 @@ type EvaluatorTests () =
508552
member _.``The default value of a struct must be its zeroed value when the struct has a constructor without parameters`` () =
509553
// F# declares no struct with a constructor without parameters, as C# can, so the test makes one: its
510554
// constructor sets the field to 7.
511-
let declared =
512-
AssemblyBuilder
513-
.DefineDynamicAssembly(AssemblyName "EvaluatorTests.Dynamic", AssemblyBuilderAccess.Run)
514-
.DefineDynamicModule("Structs")
515-
.DefineType(
516-
"Seven",
517-
TypeAttributes.Public
518-
||| TypeAttributes.Sealed
519-
||| TypeAttributes.SequentialLayout,
520-
typeof<ValueType>
555+
let seven =
556+
DynamicStruct.withConstructor
557+
"Seven"
558+
(fun code field ->
559+
code.Emit OpCodes.Ldarg_0
560+
code.Emit (OpCodes.Ldc_I4, 7)
561+
code.Emit (OpCodes.Stfld, field)
562+
code.Emit OpCodes.Ret
521563
)
522564

523-
let field = declared.DefineField ("Value", typeof<int>, FieldAttributes.Public)
524-
525-
let code =
526-
declared.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes).GetILGenerator()
527-
528-
code.Emit OpCodes.Ldarg_0
529-
code.Emit (OpCodes.Ldc_I4, 7)
530-
code.Emit (OpCodes.Stfld, field)
531-
code.Emit OpCodes.Ret
532-
533-
let seven = declared.CreateType ()
534565
let valueOf (instance : objnull) = (nonNull (seven.GetField "Value")).GetValue instance
535566

536567
Assert.AreEqual<objnull>(

‎tests/Cosmos.Quotations.Tests/FSharp.Azure.Cosmos.Quotations.Tests.fsproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
<Compile Include="NormalizerSpellingTests.fs" />
3333
<Compile Include="NormalizerTests.fs" />
3434
<Compile Include="NormalizerErrorTests.fs" />
35+
<Compile Include="DynamicStruct.fs" />
3536
<Compile Include="EvaluatorTests.fs" />
37+
<Compile Include="ParameterizerShapeTests.fs" />
38+
<Compile Include="ParameterizerTests.fs" />
3639
</ItemGroup>
3740

3841
<ItemGroup>

‎tests/Cosmos.Quotations.Tests/Model.fs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ type Folder = {
120120
| Some parent -> parent.Depth + 1
121121
| None -> 0
122122

123+
/// A function as a class that code declares; the class of every compiled function is made of the same type.
124+
type TagTest (wanted : string) =
125+
inherit FSharpFunc<string, bool> ()
126+
127+
/// <inheritdoc />
128+
override _.Invoke (tag : string) : bool = tag = wanted
129+
130+
/// A union with a case that is represented by null, as the None of an option is.
131+
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
132+
type Rank =
133+
/// The case that is null.
134+
| Unranked
135+
/// A case with a value.
136+
| Ranked of int
137+
123138
/// A pass for values that counts them: a test tells by it how often a part of a quotation was computed.
124139
type Meter () =
125140

0 commit comments

Comments
 (0)