|
| 1 | +namespace FSharp.Azure.Cosmos.Quotations.Tests |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Collections.Generic |
| 5 | +open System.Collections.Immutable |
| 6 | +open Microsoft.VisualStudio.TestTools.UnitTesting |
| 7 | +open FSharp.Quotations |
| 8 | +open FSharp.Quotations.Patterns |
| 9 | + |
| 10 | +open FSharp.Azure.Cosmos |
| 11 | +open FSharp.Azure.Cosmos.Quotations |
| 12 | + |
| 13 | +/// The values that the functions of the tests capture. A test writes its functions over two sets of them. |
| 14 | +type CapturedValues = { |
| 15 | + Limit : int |
| 16 | + Name : string |
| 17 | + Filter : string option |
| 18 | + Flag : bool |
| 19 | + Nick : string option |
| 20 | + Tags : string list |
| 21 | + Range : AgeRange |
| 22 | + Comparison : StringComparison |
| 23 | +} |
| 24 | + |
| 25 | +/// <summary> |
| 26 | +/// A function of a person as a quotation, with the check that another quotation computes what its compiled code does. |
| 27 | +/// </summary> |
| 28 | +type PersonFunction = { |
| 29 | + /// What the function is made of; the name of a test result starts with it. |
| 30 | + Name : string |
| 31 | + /// The quotation of the function as it is written. |
| 32 | + Quotation : Expr |
| 33 | + /// Fails unless the function that a quotation holds computes what the compiled code of this function does. |
| 34 | + ComputesTheSame : Expr -> unit |
| 35 | +} with |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// The function <paramref name="code"/>. The compiler passes the code together with its quotation. |
| 39 | + /// </summary> |
| 40 | + static member Of<'Result when 'Result : equality> |
| 41 | + (name : string, [<ReflectedDefinition(true)>] code : Expr<Person -> 'Result>) |
| 42 | + : PersonFunction |
| 43 | + = |
| 44 | + let code = Quoted.Split code |
| 45 | + |
| 46 | + { |
| 47 | + Name = name |
| 48 | + Quotation = code.Quotation |
| 49 | + ComputesTheSame = NormalizerAssert.ComputesTheSame code.Compiled Persons.all |
| 50 | + } |
| 51 | + |
| 52 | +/// The functions that the laws of the parameterizer are checked on. |
| 53 | +[<RequireQualifiedAccess>] |
| 54 | +module PersonFunctions = |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Functions of a person written over <paramref name="captured"/>, every one of a shape of its own: no two of |
| 58 | + /// them may give one query text. Two calls give the same functions in the same order, with other values. |
| 59 | + /// </summary> |
| 60 | + let over (captured : CapturedValues) : ImmutableArray<PersonFunction> = |
| 61 | + // Locals, because a local arrives in a quotation as a value with the name of its variable. |
| 62 | + let limit = captured.Limit |
| 63 | + let name = captured.Name |
| 64 | + let filter = captured.Filter |
| 65 | + let flag = captured.Flag |
| 66 | + let nick = captured.Nick |
| 67 | + let tags = captured.Tags |
| 68 | + let range = captured.Range |
| 69 | + let comparison = captured.Comparison |
| 70 | + |
| 71 | + seq { |
| 72 | + // Literals |
| 73 | + PersonFunction.Of ("a literal", (fun p -> p.Age > 18)) |
| 74 | + PersonFunction.Of ("another literal", (fun p -> p.Age > 21)) |
| 75 | + PersonFunction.Of ("a literal of another type", (fun p -> int64 p.Age > 18L)) |
| 76 | + PersonFunction.Of ("a zero", (fun p -> float p.Age > 0.0)) |
| 77 | + PersonFunction.Of ("a negative zero", (fun p -> float p.Age > -0.0)) |
| 78 | + PersonFunction.Of ("a string", (fun p -> p.Name = "Bob")) |
| 79 | + PersonFunction.Of ("an empty string", (fun p -> p.Name = "")) |
| 80 | + PersonFunction.Of ("null", (fun p -> box p.Name = null)) |
| 81 | + PersonFunction.Of ("a character", (fun p -> p.Name.Contains 'o')) |
| 82 | + PersonFunction.Of ("a value of an enumeration", (fun p -> p.Name.StartsWith ("b", StringComparison.Ordinal))) |
| 83 | + PersonFunction.Of ( |
| 84 | + "another value of the enumeration", |
| 85 | + (fun p -> p.Name.StartsWith ("b", StringComparison.OrdinalIgnoreCase)) |
| 86 | + ) |
| 87 | + |
| 88 | + // Captured values |
| 89 | + PersonFunction.Of ("a captured value", (fun p -> p.Age > limit)) |
| 90 | + PersonFunction.Of ("a captured value on the other side", (fun p -> limit < p.Age)) |
| 91 | + PersonFunction.Of ("a captured value that is used twice", (fun p -> p.Age > limit || fst p.Scores > limit)) |
| 92 | + PersonFunction.Of ("two captured values", (fun p -> p.Name = name || fst p.Scores > limit)) |
| 93 | + PersonFunction.Of ("a captured value of an enumeration", (fun p -> p.Name.StartsWith (name, comparison))) |
| 94 | + PersonFunction.Of ("a captured option", (fun p -> p.Nick = nick)) |
| 95 | + PersonFunction.Of ("a captured list", (fun p -> tags |> List.contains p.Name)) |
| 96 | + PersonFunction.Of ("a captured record", (fun p -> { Low = p.Age; High = fst p.Scores } = range)) |
| 97 | + |
| 98 | + // Parts that are computed |
| 99 | + PersonFunction.Of ("a property of a captured value", (fun p -> p.Age >= range.Low)) |
| 100 | + PersonFunction.Of ("a call on a captured value", (fun p -> p.Name = name.ToUpperInvariant ())) |
| 101 | + PersonFunction.Of ("arithmetic on a captured value", (fun p -> p.Age <= limit + 1)) |
| 102 | + PersonFunction.Of ("a module-level value", (fun p -> p.Age < Conditions.retirementAge)) |
| 103 | + PersonFunction.Of ("a static field", (fun p -> p.Name <> String.Empty)) |
| 104 | + PersonFunction.Of ( |
| 105 | + "an object that a constructor makes", |
| 106 | + (fun p -> DateTime (2000 + p.Age, 1, 1) > DateTime (2000 + limit, 1, 1)) |
| 107 | + ) |
| 108 | + |
| 109 | + // Values that the quotation builds |
| 110 | + PersonFunction.Of ("an option with a captured value", (fun p -> p.Nick = Some name)) |
| 111 | + PersonFunction.Of ("an option with a literal", (fun p -> p.Nick = Some "bobby")) |
| 112 | + PersonFunction.Of ("an option without a value", (fun p -> p.Nick = None)) |
| 113 | + PersonFunction.Of ("a list that is written item by item", (fun p -> [ name; "Dee" ] |> List.contains p.Name)) |
| 114 | + PersonFunction.Of ("an array", (fun p -> [| name; "Dee" |] |> Array.contains p.Name)) |
| 115 | + PersonFunction.Of ("a longer array", (fun p -> [| name; "Dee"; "Cy" |] |> Array.contains p.Name)) |
| 116 | + PersonFunction.Of ("a tuple", (fun p -> p.Scores = (limit, 1))) |
| 117 | + PersonFunction.Of ("a struct tuple", (fun p -> struct (fst p.Scores, snd p.Scores) = struct (limit, 1))) |
| 118 | + PersonFunction.Of ("a record", (fun p -> { Low = p.Age; High = limit } = { Low = 30; High = limit })) |
| 119 | + PersonFunction.Of ("an anonymous record", (fun p -> {| Low = p.Age |} = {| Low = limit |})) |
| 120 | + PersonFunction.Of ("a default value", (fun p -> DateTime (2000 + p.Age, 1, 1) > DateTime ())) |
| 121 | + |
| 122 | + // Choices |
| 123 | + PersonFunction.Of ("a choice by a captured value", (fun p -> flag || p.Active)) |
| 124 | + PersonFunction.Of ("a choice that guards a computed part", (fun p -> filter.IsNone || p.Name = filter.Value.Trim ())) |
| 125 | + PersonFunction.Of ( |
| 126 | + "a choice with a computed part in its other branch", |
| 127 | + (fun p -> if flag then p.Age > limit else p.Name = filter.Value) |
| 128 | + ) |
| 129 | + PersonFunction.Of ("a choice by the document", (fun p -> if p.Active then p.Age > limit + 1 else p.Age < limit + 2)) |
| 130 | + |
| 131 | + // Other operations on the document |
| 132 | + PersonFunction.Of ("an item of a tuple", (fun p -> fst p.Scores > limit)) |
| 133 | + PersonFunction.Of ("another item of a tuple", (fun p -> snd p.Scores > limit)) |
| 134 | + PersonFunction.Of ( |
| 135 | + "a test for a union case", |
| 136 | + (fun p -> |
| 137 | + match p.Nick with |
| 138 | + | Some _ -> p.Active |
| 139 | + | None -> p.Age > limit |
| 140 | + ) |
| 141 | + ) |
| 142 | + PersonFunction.Of ("a test for a type", (fun p -> box p.Age :? int)) |
| 143 | + PersonFunction.Of ("a value seen as a value of another type", (fun p -> (p.Name :> obj) = (name :> obj))) |
| 144 | + PersonFunction.Of ("an inner function", (fun p -> p.Tags |> List.exists (fun tag -> tag = name))) |
| 145 | + PersonFunction.Of ( |
| 146 | + "an inner function that reads the document", |
| 147 | + (fun p -> p.Tags |> List.exists (fun tag -> tag = p.Name)) |
| 148 | + ) |
| 149 | + } |
| 150 | + |> ImmutableArray.CreateRange |
| 151 | + |
| 152 | + /// The functions over values with which every choice takes its first branch. |
| 153 | + let first = |
| 154 | + over { |
| 155 | + Limit = 18 |
| 156 | + Name = "bob" |
| 157 | + Filter = None |
| 158 | + Flag = true |
| 159 | + Nick = None |
| 160 | + Tags = [] |
| 161 | + Range = { Low = 10; High = 9 } |
| 162 | + Comparison = StringComparison.Ordinal |
| 163 | + } |
| 164 | + |
| 165 | + /// The same functions over other values, with which every choice takes its other branch. |
| 166 | + let second = |
| 167 | + over { |
| 168 | + Limit = 30 |
| 169 | + Name = "Bob" |
| 170 | + Filter = Some " Bob " |
| 171 | + Flag = false |
| 172 | + Nick = Some "bobby" |
| 173 | + Tags = [ "Bob"; "Dee" ] |
| 174 | + Range = { Low = 30; High = 10 } |
| 175 | + Comparison = StringComparison.OrdinalIgnoreCase |
| 176 | + } |
| 177 | + |
| 178 | +/// <summary> |
| 179 | +/// The laws of <see cref="T:FSharp.Azure.Cosmos.Quotations.Parameterizer"/>: the key of a function does not depend on |
| 180 | +/// the values the function captured, two functions that are not alike have different keys, the key is the same whether |
| 181 | +/// or not the template is asked for, and the template with its values computes what the function computes. |
| 182 | +/// </summary> |
| 183 | +[<TestClass; ParameterizerUnitTestCategory>] |
| 184 | +type ParameterizerShapeTests () = |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Every function with both sets of values, as a row of a data-driven test. The name of a row replaces the name of |
| 188 | + /// the test method in the results, so it says what the function is made of and what <paramref name="law"/> the |
| 189 | + /// method asserts. |
| 190 | + /// </summary> |
| 191 | + static member private Rows (law : string) : IEnumerable<TestDataRow<PersonFunction>> = seq { |
| 192 | + for written in PersonFunctions.first do |
| 193 | + TestDataRow<PersonFunction>(written, DisplayName = $"%s{written.Name} %s{law}, with the first values") |
| 194 | + |
| 195 | + for written in PersonFunctions.second do |
| 196 | + TestDataRow<PersonFunction>(written, DisplayName = $"%s{written.Name} %s{law}, with the second values") |
| 197 | + } |
| 198 | + |
| 199 | + /// The rows of the test that compares the keys of one function with two sets of values. |
| 200 | + static member SameKeyRows : IEnumerable<TestDataRow<struct (PersonFunction * PersonFunction)>> = |
| 201 | + Seq.zip PersonFunctions.first PersonFunctions.second |
| 202 | + |> Seq.map (fun (struct (first, second)) -> |
| 203 | + TestDataRow<struct (PersonFunction * PersonFunction)>( |
| 204 | + struct (first, second), |
| 205 | + DisplayName = $"%s{first.Name} must have one key whatever values are captured" |
| 206 | + ) |
| 207 | + ) |
| 208 | + |
| 209 | + /// The rows of the test that compares what is known without the template with what the template holds. |
| 210 | + static member SameShapeRows = ParameterizerShapeTests.Rows "must have the key and the values of its template" |
| 211 | + |
| 212 | + /// The rows of the test that looks at what the template holds at the places of the values. |
| 213 | + static member VariableRows = |
| 214 | + ParameterizerShapeTests.Rows "must have a template with a variable for every slot and no captured value" |
| 215 | + |
| 216 | + /// The rows of the test that compares the template with the compiled code. |
| 217 | + static member SameMeaningRows = |
| 218 | + ParameterizerShapeTests.Rows "must have a template that computes what the compiled code computes" |
| 219 | + |
| 220 | + [<TestMethod; DynamicData(nameof ParameterizerShapeTests.SameKeyRows)>] |
| 221 | + member _.``A function must have one key whatever values are captured`` (first : PersonFunction, second : PersonFunction) = |
| 222 | + let firstKey = (ParameterizerAssert.WantShape first.Quotation).Key |
| 223 | + let secondKey = (ParameterizerAssert.WantShape second.Quotation).Key |
| 224 | + |
| 225 | + Assert.AreEqual<ShapeKey>(firstKey, secondKey, "The values a function captured should not be in its key.") |
| 226 | + |
| 227 | + Assert.AreEqual<int>( |
| 228 | + firstKey.GetHashCode (), |
| 229 | + secondKey.GetHashCode (), |
| 230 | + "Equal keys should have one hash code, or a dictionary does not find the second one." |
| 231 | + ) |
| 232 | + |
| 233 | + [<TestMethod>] |
| 234 | + member _.``Two functions that are not alike must have different keys`` () = |
| 235 | + let keys = |
| 236 | + PersonFunctions.first |
| 237 | + |> Seq.map (fun written -> struct (written.Name, (ParameterizerAssert.WantShape written.Quotation).Key)) |
| 238 | + |> Seq.toArray |
| 239 | + |
| 240 | + let alike = |
| 241 | + seq { |
| 242 | + for index in 0 .. keys.Length - 1 do |
| 243 | + for other in index + 1 .. keys.Length - 1 do |
| 244 | + let struct (name, key) = keys[index] |
| 245 | + let struct (otherName, otherKey) = keys[other] |
| 246 | + |
| 247 | + if key = otherKey then |
| 248 | + $"%s{name} / %s{otherName}" |
| 249 | + } |
| 250 | + |> Seq.toArray |
| 251 | + |
| 252 | + Assert.IsEmpty (alike, "Functions with one key get one query text, so every pair here would share a wrong one.") |
| 253 | + |
| 254 | + [<TestMethod; DynamicData(nameof ParameterizerShapeTests.SameShapeRows)>] |
| 255 | + member _.``A function must have the key and the values of its template`` (written : PersonFunction) = |
| 256 | + let shape = ParameterizerAssert.WantShape written.Quotation |
| 257 | + let template = ParameterizerAssert.WantTemplate written.Quotation |
| 258 | + |
| 259 | + Assert.AreEqual<ShapeKey>( |
| 260 | + template.Key, |
| 261 | + shape.Key, |
| 262 | + "The key that finds a translation should be the key the translation was made under." |
| 263 | + ) |
| 264 | + |
| 265 | + CollectionAssert.AreEqual ( |
| 266 | + template.Values |> Seq.toArray, |
| 267 | + shape.Values |> Seq.toArray, |
| 268 | + "The values should be the same, slot by slot." |
| 269 | + ) |
| 270 | + |
| 271 | + Assert.HasCount (template.Slots.Length, shape.Values, "Every slot should have its place among the values.") |
| 272 | + |
| 273 | + [<TestMethod; DynamicData(nameof ParameterizerShapeTests.VariableRows)>] |
| 274 | + member _.``A function must have a template with a variable for every slot and no captured value`` (written : PersonFunction) = |
| 275 | + let template = ParameterizerAssert.WantTemplate written.Quotation |
| 276 | + |
| 277 | + let captured = |
| 278 | + template.Body |
| 279 | + |> Quotation.filter (fun part -> |
| 280 | + match part with |
| 281 | + | ValueWithName _ -> true |
| 282 | + | _ -> false |
| 283 | + ) |
| 284 | + |
| 285 | + Assert.IsEmpty (captured, "A captured value is a parameter, so the template should hold a variable at its place.") |
| 286 | + |
| 287 | + CollectionAssert.AreEquivalent ( |
| 288 | + template.Slots |> Seq.map _.Variable |> Seq.toArray, |
| 289 | + template.Body.GetFreeVars () |> Seq.toArray, |
| 290 | + "The variables that the template leaves unbound should be the variables of its slots, every one of them." |
| 291 | + ) |
| 292 | + |
| 293 | + [<TestMethod; DynamicData(nameof ParameterizerShapeTests.SameMeaningRows)>] |
| 294 | + member _.``A function must have a template that computes what the compiled code computes`` (written : PersonFunction) = |
| 295 | + written.Quotation |
| 296 | + |> ParameterizerAssert.WantTemplate |
| 297 | + |> Templates.withValues |
| 298 | + |> written.ComputesTheSame |
0 commit comments