namespace LibraryTests module PropertyTests = open FsCheck.Xunit open System open System.Drawing open SmoulderingBeachBall.Domain.DomainTypes open System.Reflection open SmoulderingBeachBall.Services open System.IO open System.Threading open FsCheck (* '3000' is an arbitary value. It is used to keep the testing times sane. This function is used to randomly generate image sizes but you do not need to use it. The project can handle larger numbers than '3000' but processing takes longer. If you decide to change it, please keep an eye on the saving time. When a image is too big, the project cannot write it to disc fast enough -- sometimes it is because of IO constraints. This means the tests checking for this fail/error will think the image does not exist. To resolved this, add a Thread.Sleep call to the affected tests. Just be aware, the length of sleep-time depends on the size of the image. Also, problems tends to arise around the 15,000 mark. *) let randomInt () = Random().Next(3000) let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea/" let allColours = let properties = typeof.GetProperties(BindingFlags.Public ||| BindingFlags.Static) let colours = seq { for prop in properties -> prop} |> Seq.toArray colours let randomColour () = let item = allColours.[Random().Next(allColours.Length)] item.GetValue(null, null) let fileSaved width height = saveLocation + width + "x" + height + ".png" |> File.Exists // To manually clear out the SavingTestArea folder, use this function in script.fsx. let resetSavingTestArea () = let files = Directory.GetFileSystemEntries(saveLocation) match files.Length with | 0 -> () | _ -> files |> Array.iter (fun f -> File.Delete(f)) let buildBorderOverlay () = Thread.Sleep 100 // Helps generate better colour variation. { colour = randomColour () :?> Brush overlayType = Border } let buildFullOverlay () = Thread.Sleep 100 // Helps generate better colour variation. { colour = randomColour () :?> Brush overlayType = Full } [] let ``Can create an image with no overlay`` () = resetSavingTestArea () let spec = { // See note accompanying 'randomInt' function for constraints information. width = randomInt () height = randomInt () colour = randomColour () :?> Brush filePath = saveLocation overlay = None } makeImage spec |> Async.RunSynchronously fileSaved (spec.width.ToString()) (spec.height.ToString()) = true [] let ``Can create an image with border`` () = resetSavingTestArea () let spec = { // See note accompanying 'randomInt' function for constraints information. width = randomInt () height = randomInt () colour = randomColour () :?> Brush filePath = saveLocation overlay = Some (buildBorderOverlay ()) } makeImage spec |> Async.RunSynchronously fileSaved (spec.width.ToString()) (spec.height.ToString()) = true [] let ``Can create an image with a full overlay`` () = resetSavingTestArea () let spec = { // See note accompanying 'randomInt' function for constraints information. width = randomInt () height = randomInt () colour = randomColour () :?> Brush filePath = saveLocation overlay = Some (buildFullOverlay ()) } makeImage spec |> Async.RunSynchronously fileSaved (spec.width.ToString()) (spec.height.ToString()) = true