From 862df84a54890480f3d56d7cf0ddafb92189517d Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Tue, 4 Sep 2018 20:29:35 +0100 Subject: [PATCH 01/10] add Test Centre. Add FsCheck and Nunit packages, wire them up and set-up the first FsCheck test. Display tests in Test Explorer (in Visual Studio). --- SmoulderingBeachBall.sln | 6 ++ SmoulderingBeachBallCLI/Validation.fs | 2 +- TestCentre/AssemblyInfo.fs | 41 ++++++++++ TestCentre/ConsoleTests.fs | 16 ++++ TestCentre/Script.fsx | 8 ++ TestCentre/TestCentre.fsproj | 112 ++++++++++++++++++++++++++ TestCentre/packages.config | 15 ++++ 7 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 TestCentre/AssemblyInfo.fs create mode 100644 TestCentre/ConsoleTests.fs create mode 100644 TestCentre/Script.fsx create mode 100644 TestCentre/TestCentre.fsproj create mode 100644 TestCentre/packages.config diff --git a/SmoulderingBeachBall.sln b/SmoulderingBeachBall.sln index 8328c68..0e40a48 100644 --- a/SmoulderingBeachBall.sln +++ b/SmoulderingBeachBall.sln @@ -7,6 +7,8 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "SmoulderingBeachBall", "Smo EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "SmoulderingBeachBallCLI", "SmoulderingBeachBallCLI\SmoulderingBeachBallCLI.fsproj", "{5C00D583-EF09-4FE6-A3FE-EB01454C0607}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TestCentre", "TestCentre\TestCentre.fsproj", "{9EAEE0E6-1E90-4C44-AD22-E8CA727B3392}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {5C00D583-EF09-4FE6-A3FE-EB01454C0607}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C00D583-EF09-4FE6-A3FE-EB01454C0607}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C00D583-EF09-4FE6-A3FE-EB01454C0607}.Release|Any CPU.Build.0 = Release|Any CPU + {9EAEE0E6-1E90-4C44-AD22-E8CA727B3392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9EAEE0E6-1E90-4C44-AD22-E8CA727B3392}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9EAEE0E6-1E90-4C44-AD22-E8CA727B3392}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9EAEE0E6-1E90-4C44-AD22-E8CA727B3392}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SmoulderingBeachBallCLI/Validation.fs b/SmoulderingBeachBallCLI/Validation.fs index 3820173..ef7473e 100644 --- a/SmoulderingBeachBallCLI/Validation.fs +++ b/SmoulderingBeachBallCLI/Validation.fs @@ -1,4 +1,4 @@ -module internal Validation +module Validation open System.Drawing open SmoulderingBeachBall.Domain.DomainTypes diff --git a/TestCentre/AssemblyInfo.fs b/TestCentre/AssemblyInfo.fs new file mode 100644 index 0000000..d8dda7b --- /dev/null +++ b/TestCentre/AssemblyInfo.fs @@ -0,0 +1,41 @@ +namespace TestCentre.AssemblyInfo + +open System.Reflection +open System.Runtime.CompilerServices +open System.Runtime.InteropServices + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[] +[] +[] +[] +[] +[] +[] +[] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [] +[] +[] + +do + () \ No newline at end of file diff --git a/TestCentre/ConsoleTests.fs b/TestCentre/ConsoleTests.fs new file mode 100644 index 0000000..690021b --- /dev/null +++ b/TestCentre/ConsoleTests.fs @@ -0,0 +1,16 @@ +namespace ConsoleTests + + module PropertyTests = + + open Xunit + open FsCheck + open FsCheck.Xunit + open Validation + + let config = { FsCheck.Config.Default with MaxTest = 10 } + + [] + let ``colour map is not empty`` () = + (Map.isEmpty colourList) = false + + Check.Quick (``colour map is not empty`` ()) \ No newline at end of file diff --git a/TestCentre/Script.fsx b/TestCentre/Script.fsx new file mode 100644 index 0000000..1845605 --- /dev/null +++ b/TestCentre/Script.fsx @@ -0,0 +1,8 @@ +// Learn more about F# at http://fsharp.org +// See the 'F# Tutorial' project for more help. + +#load "Library1.fs" +open TestCentre + +// Define your library scripting code here + diff --git a/TestCentre/TestCentre.fsproj b/TestCentre/TestCentre.fsproj new file mode 100644 index 0000000..74e7271 --- /dev/null +++ b/TestCentre/TestCentre.fsproj @@ -0,0 +1,112 @@ + + + + + + + Debug + AnyCPU + 2.0 + 9eaee0e6-1e90-4c44-ad22-e8ca727b3392 + Library + TestCentre + TestCentre + true + v4.7.1 + true + TestCentre + + + + + true + full + false + false + bin\$(Configuration)\ + DEBUG;TRACE + 3 + bin\$(Configuration)\$(AssemblyName).XML + + + pdbonly + true + true + bin\$(Configuration)\ + TRACE + 3 + bin\$(Configuration)\$(AssemblyName).XML + + + 11 + + + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets + + + + + + + + + + + ..\packages\FsCheck.2.11.0\lib\net452\FsCheck.dll + + + ..\packages\FsCheck.Xunit.2.11.0\lib\net452\FsCheck.Xunit.dll + + + ..\packages\FSharp.Core.4.5.2\lib\net45\FSharp.Core.dll + + + + + + + + True + + + ..\packages\xunit.abstractions.2.0.3\lib\net35\xunit.abstractions.dll + + + ..\packages\xunit.assert.2.4.0\lib\netstandard2.0\xunit.assert.dll + + + ..\packages\xunit.extensibility.core.2.4.0\lib\net452\xunit.core.dll + + + ..\packages\xunit.extensibility.execution.2.4.0\lib\net452\xunit.execution.desktop.dll + + + + + SmoulderingBeachBallCLI + {5c00d583-ef09-4fe6-a3fe-eb01454c0607} + True + + + SmoulderingBeachBall + {dfc3cbca-3da7-4cf4-a8bc-bccb740fa6cd} + True + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + \ No newline at end of file diff --git a/TestCentre/packages.config b/TestCentre/packages.config new file mode 100644 index 0000000..0e1b818 --- /dev/null +++ b/TestCentre/packages.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file From d05e56c91377115281452e56e3b0d85adbd8af1b Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 17:37:00 +0100 Subject: [PATCH 02/10] add UnitTests module and initial tests for console project. --- SmoulderingBeachBallCLI/Validation.fs | 12 ++++---- TestCentre/ConsoleTests.fs | 41 +++++++++++++++++++++++---- TestCentre/Script.fsx | 5 ++-- TestCentre/TestCentre.fsproj | 3 ++ TestCentre/packages.config | 1 + 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/SmoulderingBeachBallCLI/Validation.fs b/SmoulderingBeachBallCLI/Validation.fs index ef7473e..774e384 100644 --- a/SmoulderingBeachBallCLI/Validation.fs +++ b/SmoulderingBeachBallCLI/Validation.fs @@ -16,11 +16,11 @@ "yellow", Brushes.Yellow;] |> Map.ofList - let isColourValid (colour: string) = + let private isColourValid (colour: string) = colourList |> Map.containsKey (colour.ToLower()) - let parseColour colour = + let private parseColour colour = match (isColourValid colour) with | true -> colourList @@ -33,7 +33,7 @@ let getDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) - let parsePath (path: string) = + let private parsePath (path: string) = match path with | path when ((path.ToLower()).Equals "desktop") -> getDesktopPath | path when ((path.ToLower()).Equals "d") -> getDesktopPath @@ -52,7 +52,7 @@ } spec - let parseOverlay (oType: string) = + let private parseOverlay (oType: string) = match oType.ToLower() with | "border" -> Border | "b" -> Border @@ -60,13 +60,13 @@ | "f" -> Full | _ -> invalidArg "Overlay Type" "The overlay type must be either 'border' or 'full'." - let buildOverlaySpec oColour (oType: string) = + let private buildOverlaySpec oColour (oType: string) = let oSpec = { colour = parseColour oColour; overlayType = parseOverlay oType } oSpec - let buildMainSpec iWidth iHeight mainColour path oSpec = + let private buildMainSpec iWidth iHeight mainColour path oSpec = let spec = { width = iWidth; height = iHeight; diff --git a/TestCentre/ConsoleTests.fs b/TestCentre/ConsoleTests.fs index 690021b..5bd7e96 100644 --- a/TestCentre/ConsoleTests.fs +++ b/TestCentre/ConsoleTests.fs @@ -2,15 +2,44 @@ module PropertyTests = - open Xunit open FsCheck open FsCheck.Xunit open Validation - - let config = { FsCheck.Config.Default with MaxTest = 10 } + open System + open SmoulderingBeachBall.Domain.DomainTypes + open System.Drawing [] - let ``colour map is not empty`` () = - (Map.isEmpty colourList) = false + let ``buildDefaultSpec creates intended type`` () = + let width = Random().Next() + let height = Random().Next() + let oSpec = + { colour = Brushes.Black + overlayType = Full } + let defaultSpec = + { width = width + height = height + colour = Brushes.AntiqueWhite + filePath = getDesktopPath + overlay = Some oSpec + } + let spec = buildDefaultSpec width height + defaultSpec = spec - Check.Quick (``colour map is not empty`` ()) \ No newline at end of file + Check.Quick (``buildDefaultSpec creates intended type`` ()) + + module UnitTests = + + open Xunit + open Validation + open System + + [] + let ``Colour map is not empty`` () = + Assert.NotEmpty colourList + + [] + let ``getDesktopPath returns desktop path`` () = + let expectedPath = + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + Assert.Equal (expectedPath, getDesktopPath) \ No newline at end of file diff --git a/TestCentre/Script.fsx b/TestCentre/Script.fsx index 1845605..c3b5e6c 100644 --- a/TestCentre/Script.fsx +++ b/TestCentre/Script.fsx @@ -1,8 +1,9 @@ // Learn more about F# at http://fsharp.org // See the 'F# Tutorial' project for more help. -#load "Library1.fs" -open TestCentre +#load "ConsoleTests.fs" + +open ConsoleTests // Define your library scripting code here diff --git a/TestCentre/TestCentre.fsproj b/TestCentre/TestCentre.fsproj index 74e7271..b72f52d 100644 --- a/TestCentre/TestCentre.fsproj +++ b/TestCentre/TestCentre.fsproj @@ -64,6 +64,9 @@ + + ..\packages\System.Drawing.Common.4.5.0\lib\net461\System.Drawing.Common.dll + True diff --git a/TestCentre/packages.config b/TestCentre/packages.config index 0e1b818..6de53e1 100644 --- a/TestCentre/packages.config +++ b/TestCentre/packages.config @@ -3,6 +3,7 @@ + From 466a833dfbb30484af1866dc7b48c786641a2339 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 19:01:26 +0100 Subject: [PATCH 03/10] add property tests and helper functions to console tests. Add a comment about the setup of the tests in console tests. --- SmoulderingBeachBallCLI/Validation.fs | 6 +-- TestCentre/ConsoleTests.fs | 65 ++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/SmoulderingBeachBallCLI/Validation.fs b/SmoulderingBeachBallCLI/Validation.fs index 774e384..47460e6 100644 --- a/SmoulderingBeachBallCLI/Validation.fs +++ b/SmoulderingBeachBallCLI/Validation.fs @@ -62,14 +62,14 @@ let private buildOverlaySpec oColour (oType: string) = let oSpec = - { colour = parseColour oColour; + { colour = parseColour oColour overlayType = parseOverlay oType } oSpec let private buildMainSpec iWidth iHeight mainColour path oSpec = let spec = - { width = iWidth; - height = iHeight; + { width = iWidth + height = iHeight colour = parseColour mainColour filePath = parsePath path overlay = oSpec } diff --git a/TestCentre/ConsoleTests.fs b/TestCentre/ConsoleTests.fs index 5bd7e96..95b44a6 100644 --- a/TestCentre/ConsoleTests.fs +++ b/TestCentre/ConsoleTests.fs @@ -2,17 +2,38 @@ module PropertyTests = - open FsCheck open FsCheck.Xunit open Validation open System open SmoulderingBeachBall.Domain.DomainTypes open System.Drawing + (* The functions without the "Property" attribute are not for testing. + They generate the test data used in the (property) tests. + Please be careful when removing any of these types of functions. + They are the ones not marked with the "Property" attribute. + Also, the intention is to keep these functions seperate from the tests. + Until it is not practical to keep them in this module, the default place to keep them is above the tests. With that said, it is just a guide. + Use your best judgement when adding new "helper" functions. + This applies to the UnitTests module, thanks. *) + + let randomInt () = Random().Next() + + let randomColour () = + colourList + |> Map.toList + |> List.item (Random().Next(colourList.Count)) + + let overlays = [|"none"; "n"; "full"; "f"|] + + let noOverlay () = overlays.[Random().Next(0, 1)] + + let fullOverlay () = overlays.[Random().Next(2, 3)] + [] let ``buildDefaultSpec creates intended type`` () = - let width = Random().Next() - let height = Random().Next() + let width = randomInt () + let height = randomInt () let oSpec = { colour = Brushes.Black overlayType = Full } @@ -25,8 +46,42 @@ } let spec = buildDefaultSpec width height defaultSpec = spec - - Check.Quick (``buildDefaultSpec creates intended type`` ()) + + [] + let ``buildSpec creates intended type with no overlay`` () = + let width = randomInt () + let height = randomInt () + let colour = randomColour () + let overlay = noOverlay () + let expectedSpec = + { width = width + height = height + colour = snd colour + filePath = getDesktopPath + overlay = option.None } + let spec = + buildSpec width height (fst colour) (fst colour) overlay getDesktopPath + expectedSpec = spec + + [] + let ``buildSpec creates intended type with full overlay`` () = + let width = randomInt () + let height = randomInt () + let mainColour = randomColour () + let overlayColour = randomColour () + let overlay = fullOverlay () + let oSpec = + { colour = snd overlayColour + overlayType = Full } + let expectedSpec = + { width = width + height = height + colour = snd mainColour + filePath = getDesktopPath + overlay = Some oSpec } + let spec = + buildSpec width height (fst mainColour) (fst overlayColour) overlay getDesktopPath + expectedSpec = spec module UnitTests = From e898b404175aba32cc82565e5f2e97ad9c68b96b Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 19:12:29 +0100 Subject: [PATCH 04/10] add property test (CLI) -- build spec with border. --- TestCentre/ConsoleTests.fs | 39 ++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/TestCentre/ConsoleTests.fs b/TestCentre/ConsoleTests.fs index 95b44a6..d0ee47a 100644 --- a/TestCentre/ConsoleTests.fs +++ b/TestCentre/ConsoleTests.fs @@ -24,11 +24,13 @@ |> Map.toList |> List.item (Random().Next(colourList.Count)) - let overlays = [|"none"; "n"; "full"; "f"|] + let overlays = [|"none"; "n"; "b"; "border"; "full"; "f"|] let noOverlay () = overlays.[Random().Next(0, 1)] - let fullOverlay () = overlays.[Random().Next(2, 3)] + let borderOverlay () = overlays.[Random().Next(2, 3)] + + let fullOverlay () = overlays.[Random().Next(4, 5)] [] let ``buildDefaultSpec creates intended type`` () = @@ -48,19 +50,24 @@ defaultSpec = spec [] - let ``buildSpec creates intended type with no overlay`` () = + let ``buildSpec creates intended type with border`` () = let width = randomInt () let height = randomInt () - let colour = randomColour () - let overlay = noOverlay () + let mainColour = randomColour () + let overlayColour = randomColour () + let overlay = borderOverlay () + let oSpec = + { colour = (snd) overlayColour + overlayType = Border } let expectedSpec = { width = width height = height - colour = snd colour + colour = (snd) mainColour filePath = getDesktopPath - overlay = option.None } + overlay = Some oSpec + } let spec = - buildSpec width height (fst colour) (fst colour) overlay getDesktopPath + buildSpec width height (fst mainColour) (fst overlayColour) overlay getDesktopPath expectedSpec = spec [] @@ -83,6 +90,22 @@ buildSpec width height (fst mainColour) (fst overlayColour) overlay getDesktopPath expectedSpec = spec + [] + let ``buildSpec creates intended type with no overlay`` () = + let width = randomInt () + let height = randomInt () + let colour = randomColour () + let overlay = noOverlay () + let expectedSpec = + { width = width + height = height + colour = snd colour + filePath = getDesktopPath + overlay = option.None } + let spec = + buildSpec width height (fst colour) (fst colour) overlay getDesktopPath + expectedSpec = spec + module UnitTests = open Xunit From e6890045b08642020c8f901e4fc905378213dd16 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 22:19:26 +0100 Subject: [PATCH 05/10] add property tests for library project. Add rough code for generating a collection of brushes in script.fsx in Test Centre. Make minor changes to Assembly Info and fsproj (Test Centre). --- SmoulderingBeachBall/InternalServices.fs | 2 - TestCentre/AssemblyInfo.fs | 8 ++-- TestCentre/LibraryTests.fs | 54 ++++++++++++++++++++++++ TestCentre/Script.fsx | 28 ++++++++++++ TestCentre/TestCentre.fsproj | 1 + 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 TestCentre/LibraryTests.fs diff --git a/SmoulderingBeachBall/InternalServices.fs b/SmoulderingBeachBall/InternalServices.fs index 260e608..7402b30 100644 --- a/SmoulderingBeachBall/InternalServices.fs +++ b/SmoulderingBeachBall/InternalServices.fs @@ -44,7 +44,6 @@ let penPath = createBorderPath pen.Width spec graphics.DrawLines (pen, penPath) - let drawFullOverlay (graphics: Graphics) (pen: Pen) spec = drawBorder graphics pen spec printfn "[INFO.] Adding full overlay to image..." @@ -70,7 +69,6 @@ sb.Append ".png" |> ignore (sb.ToString ()) - let drawImage spec = let bitmap = new Bitmap (spec.width, spec.height) let graphics = Graphics.FromImage (bitmap) diff --git a/TestCentre/AssemblyInfo.fs b/TestCentre/AssemblyInfo.fs index d8dda7b..bfea358 100644 --- a/TestCentre/AssemblyInfo.fs +++ b/TestCentre/AssemblyInfo.fs @@ -7,8 +7,8 @@ open System.Runtime.InteropServices // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[] -[] +[] +[] [] [] [] @@ -34,8 +34,8 @@ open System.Runtime.InteropServices // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [] -[] -[] +[] +[] do () \ No newline at end of file diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs new file mode 100644 index 0000000..3ace00c --- /dev/null +++ b/TestCentre/LibraryTests.fs @@ -0,0 +1,54 @@ +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 + + let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea/" + + (* '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 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 = + let path = saveLocation + width + "x" + height + ".png" + File.Exists path + + [] + let ``can create an image with no overlay`` () = + // See note accompanying 'randomInt' function for constraints information. + let spec = + { width = randomInt () + height = randomInt () + colour = randomColour () :?> Brush + filePath = saveLocation + overlay = None } + makeImage spec |> Async.RunSynchronously + fileSaved (spec.width.ToString()) (spec.height.ToString()) = true diff --git a/TestCentre/Script.fsx b/TestCentre/Script.fsx index c3b5e6c..f7a3229 100644 --- a/TestCentre/Script.fsx +++ b/TestCentre/Script.fsx @@ -2,8 +2,36 @@ // See the 'F# Tutorial' project for more help. #load "ConsoleTests.fs" +#load "LibraryTests.fs" open ConsoleTests +open LibraryTests +open System.Drawing +open System.Reflection +open System // Define your library scripting code here +let allBrushColours = + let properties = + Color().GetType().GetProperties() + let names = + seq { for prop in properties do + yield prop.Name } + |> Seq.toList + names + +let allBrushes = + let properties = + typeof.GetProperties(BindingFlags.Public|||BindingFlags.Static) + let colours = + seq { for prop in properties -> prop} + |> Seq.toArray + colours + +let randomColour () = + let item = allBrushes.[Random().Next (allBrushes.Length)] + item.GetValue(null, null) + +printfn "%A" allBrushes +printfn "%A" (randomColour ()) \ No newline at end of file diff --git a/TestCentre/TestCentre.fsproj b/TestCentre/TestCentre.fsproj index b72f52d..01d14c5 100644 --- a/TestCentre/TestCentre.fsproj +++ b/TestCentre/TestCentre.fsproj @@ -47,6 +47,7 @@ + From 29e8f46cb24ab887848c4b82ea01ed732d28ae40 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 23:32:34 +0100 Subject: [PATCH 06/10] add logic to clean TestSaveAea in tests and script. --- SmoulderingBeachBall/ScratchPad.fsx | 2 +- TestCentre/LibraryTests.fs | 23 +++++++++++----- TestCentre/Script.fsx | 42 +++++++---------------------- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/SmoulderingBeachBall/ScratchPad.fsx b/SmoulderingBeachBall/ScratchPad.fsx index 544dcb6..6d870b3 100644 --- a/SmoulderingBeachBall/ScratchPad.fsx +++ b/SmoulderingBeachBall/ScratchPad.fsx @@ -34,7 +34,7 @@ let imageSpec = { width = 500; height = 500; colour = Brushes.Yellow; - filePath = "C:/users/craig/desktop/test.png"; + filePath = "C:/users/craig/desktop/"; // Change this to flip between border/full overlay or None. overlay = Some fullOverlay } diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 3ace00c..6c8feca 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -2,6 +2,7 @@ module PropertyTests = + open Xunit open FsCheck.Xunit open System open System.Drawing @@ -10,8 +11,6 @@ open SmoulderingBeachBall.Services open System.IO - let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea/" - (* '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. @@ -25,6 +24,8 @@ *) let randomInt () = Random().Next(3000) + let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea/" + let allColours = let properties = typeof.GetProperties(BindingFlags.Public ||| BindingFlags.Static) @@ -38,14 +39,24 @@ item.GetValue(null, null) let fileSaved width height = - let path = saveLocation + width + "x" + height + ".png" - File.Exists path + 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 ``can create an image with no overlay`` () = - // See note accompanying 'randomInt' function for constraints information. + resetSavingTestArea () let spec = - { width = randomInt () + { // See note accompanying 'randomInt' function for constraints information. + width = randomInt () height = randomInt () colour = randomColour () :?> Brush filePath = saveLocation diff --git a/TestCentre/Script.fsx b/TestCentre/Script.fsx index f7a3229..526b141 100644 --- a/TestCentre/Script.fsx +++ b/TestCentre/Script.fsx @@ -1,37 +1,15 @@ // Learn more about F# at http://fsharp.org // See the 'F# Tutorial' project for more help. +open System.IO -#load "ConsoleTests.fs" -#load "LibraryTests.fs" +let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea/" -open ConsoleTests -open LibraryTests -open System.Drawing -open System.Reflection -open System +let resetSavingTestArea () = + let files = Directory.GetFileSystemEntries(saveLocation) + match files.Length with + | 0 -> () + | _ -> + files + |> Array.iter (fun f -> File.Delete(f)) -// Define your library scripting code here - -let allBrushColours = - let properties = - Color().GetType().GetProperties() - let names = - seq { for prop in properties do - yield prop.Name } - |> Seq.toList - names - -let allBrushes = - let properties = - typeof.GetProperties(BindingFlags.Public|||BindingFlags.Static) - let colours = - seq { for prop in properties -> prop} - |> Seq.toArray - colours - -let randomColour () = - let item = allBrushes.[Random().Next (allBrushes.Length)] - item.GetValue(null, null) - -printfn "%A" allBrushes -printfn "%A" (randomColour ()) \ No newline at end of file +resetSavingTestArea () \ No newline at end of file From fc0d8babf2a0da1d0522b7b9362efbc9c3577346 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 23:49:07 +0100 Subject: [PATCH 07/10] add a sleep period when creating an overlay in library tests. --- TestCentre/LibraryTests.fs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 6c8feca..0ade21a 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -2,7 +2,6 @@ module PropertyTests = - open Xunit open FsCheck.Xunit open System open System.Drawing @@ -10,6 +9,7 @@ open System.Reflection open SmoulderingBeachBall.Services open System.IO + open System.Threading (* '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. @@ -51,8 +51,18 @@ 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`` () = + let ``Can create an image with no overlay`` () = resetSavingTestArea () let spec = { // See note accompanying 'randomInt' function for constraints information. @@ -63,3 +73,16 @@ overlay = None } makeImage spec |> Async.RunSynchronously fileSaved (spec.width.ToString()) (spec.height.ToString()) = true + + [] + let ``Can create 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 From 5fb703fb3be66a44770bdb22be09c624ba601d3e Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Fri, 7 Sep 2018 23:53:31 +0100 Subject: [PATCH 08/10] add full overlay image test to library tests. --- TestCentre/LibraryTests.fs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 0ade21a..5173ae9 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -10,6 +10,7 @@ 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. @@ -75,7 +76,7 @@ fileSaved (spec.width.ToString()) (spec.height.ToString()) = true [] - let ``Can create image with border`` () = + let ``Can create an image with border`` () = resetSavingTestArea () let spec = { // See note accompanying 'randomInt' function for constraints information. @@ -86,3 +87,16 @@ 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 From b27acd31f21dfe6640bff193e973968612ca42a0 Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Sat, 8 Sep 2018 00:04:00 +0100 Subject: [PATCH 09/10] Rename console tests and update the git ignore file. Ignore all files in SavingTestArea. --- .gitignore | 3 +++ TestCentre/ConsoleTests.fs | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 3e759b7..cb05819 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +# Project Specific Items +SavingTestArea/ + # User-specific files *.suo *.user diff --git a/TestCentre/ConsoleTests.fs b/TestCentre/ConsoleTests.fs index d0ee47a..a1705c3 100644 --- a/TestCentre/ConsoleTests.fs +++ b/TestCentre/ConsoleTests.fs @@ -33,7 +33,7 @@ let fullOverlay () = overlays.[Random().Next(4, 5)] [] - let ``buildDefaultSpec creates intended type`` () = + let ``Can create an intended default image spec`` () = let width = randomInt () let height = randomInt () let oSpec = @@ -50,7 +50,7 @@ defaultSpec = spec [] - let ``buildSpec creates intended type with border`` () = + let ``Can create an image spec with border`` () = let width = randomInt () let height = randomInt () let mainColour = randomColour () @@ -71,7 +71,7 @@ expectedSpec = spec [] - let ``buildSpec creates intended type with full overlay`` () = + let ``Can create an image spec with full overlay`` () = let width = randomInt () let height = randomInt () let mainColour = randomColour () @@ -91,7 +91,7 @@ expectedSpec = spec [] - let ``buildSpec creates intended type with no overlay`` () = + let ``Can create an image spec with no overlay`` () = let width = randomInt () let height = randomInt () let colour = randomColour () @@ -117,7 +117,7 @@ Assert.NotEmpty colourList [] - let ``getDesktopPath returns desktop path`` () = + let ``Can retrieve the path to users desktop`` () = let expectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) Assert.Equal (expectedPath, getDesktopPath) \ No newline at end of file From f0e7e7d8703c7d7ae75157c7e28764fec35955fd Mon Sep 17 00:00:00 2001 From: "OPTIMUS-PRIME\\craig" Date: Sat, 8 Sep 2018 00:07:07 +0100 Subject: [PATCH 10/10] leave comment about function arrangement in LibraryTests.fs. --- TestCentre/LibraryTests.fs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TestCentre/LibraryTests.fs b/TestCentre/LibraryTests.fs index 5173ae9..78c7b9a 100644 --- a/TestCentre/LibraryTests.fs +++ b/TestCentre/LibraryTests.fs @@ -62,6 +62,9 @@ { colour = randomColour () :?> Brush overlayType = Full } + (* Please non-test functions above the test functions. + See ConsoleTests.fs for the extended comment -- regarding function placement. *) + [] let ``Can create an image with no overlay`` () = resetSavingTestArea ()