Death Socket consists of three projects. They are a .Net Standard 2.0 library, a console program and a Test Centre. The purpose of this repository is to provide a way for people to add grids to images.
https://www.craigoates.net/Software/project/13
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
5.5 KiB
136 lines
5.5 KiB
namespace LibraryTests |
|
|
|
(* Initial Setup -- Populating the Test Folders. |
|
=========================================================================== |
|
If you have just cloned this repository or have not run any of the tests |
|
before, please head over to script.fs (in Test Centre) and populate the |
|
LoadingTestArea and SavingTestArea folders. More information will be |
|
provided there about what the scripts do. It is worth pointing out here the |
|
.gitignore file ignores any .png files in them. This is why you must |
|
populate them before running any tests. *) |
|
|
|
module TestingHelpers = |
|
|
|
(* When you are writing tests, please keep all helper functions in this |
|
module. If this module grows to a point where it hinders the actual |
|
testing modules, consider moving it then. If you do decide to move them |
|
out, pay attention to ConsoleTests.fs and its helper functions. *) |
|
|
|
open System |
|
open System.Drawing |
|
open System.Reflection |
|
open System.IO |
|
|
|
let rand = Random () |
|
|
|
|
|
(* These are duplicates from ConsoleTests.fs (both of them). See point |
|
about helpers. Tests for checking these locations can be found in |
|
ConsoleTests.fs. *) |
|
let loadLocation = __SOURCE_DIRECTORY__ + "/LoadingTestArea" |
|
let saveLocation = __SOURCE_DIRECTORY__ + "/SavingTestArea" |
|
|
|
let allColours = |
|
let properties = |
|
typeof<Brushes>.GetProperties |
|
(BindingFlags.Public ||| BindingFlags.Static) |
|
seq { for prop in properties -> prop} |
|
|> Seq.toArray |
|
|
|
let randomBrush () = |
|
let item = allColours.[rand.Next(allColours.Length)] |
|
item.GetValue(null, null) |
|
|
|
(* Max. value is arbitrary (change for performance). |
|
Min. value is to stop divide by zero exceptions. |
|
Intended for horizontal and vertical line tests. *) |
|
let newNum () = rand.Next(1, 1000) |
|
|
|
let imagesInLoadingTestArea = |
|
Directory.GetFileSystemEntries (loadLocation, "*.png") |
|
|
|
let generateLoadPath () = |
|
let files = imagesInLoadingTestArea |
|
files.[rand.Next(files.Length)] |
|
|
|
let generateSavePath originalFilePath = |
|
let fileName = Path.GetFileName originalFilePath |
|
saveLocation + "/" + fileName |
|
|
|
(* To "manually" clear out the SavingTestArea folder, use this function |
|
in script.fsx. More information can be found there, also. *) |
|
let resetSavingTestArea () = |
|
let files = Directory.GetFileSystemEntries(saveLocation, "*.png") |
|
match files.Length with |
|
| 0 -> () |
|
| _ -> |
|
files |
|
|> Array.iter (fun f -> File.Delete(f)) |
|
|
|
module PropertyTests = |
|
|
|
open FsCheck.Xunit |
|
open DeathSocket |
|
open System.Drawing |
|
open DeathSocket.GridPainter |
|
open TestingHelpers |
|
open System.IO |
|
open System |
|
|
|
[<Property>] |
|
let ``Can apply grid to image and save it`` () = |
|
(* You should end up with one image left over in SavingTestArea. |
|
Comment out the "reset" function to see all the images produced, |
|
by this test. This will mean you will need to manually delete the |
|
images yourself if you do. *) |
|
resetSavingTestArea () |
|
let oPath = generateLoadPath () |
|
let sPath = generateSavePath oPath |
|
let (spec: BrushSpec) = |
|
{ originalPath = oPath |
|
savePath = sPath |
|
colour = randomBrush () :?> Brush |
|
penWidth = float32 1 |
|
rows = 10 |
|
columns = 10 } |
|
applyBrushSpecGridAsync spec |
|
|> Async.RunSynchronously |
|
(File.Exists sPath) = true |
|
|
|
[<Property>] |
|
let ``Can return a collection of points which represent a grids horizontal lines`` () = |
|
let result = determineHorizontalLines (newNum()) (newNum()) (newNum()) |
|
result.Length > 0 |
|
|
|
[<Property>] |
|
let ``Can return a collection of points which represent a grids vertical lines`` () = |
|
let result = determineVerticalLines (newNum()) (newNum()) (newNum()) |
|
result.Length > 0 |
|
|
|
module UnitTests = |
|
|
|
open TestingHelpers |
|
open Xunit |
|
open DeathSocket |
|
open System |
|
|
|
(* This test is a precaution (a test for the tests if you will...). |
|
It is here to make sure the property test has what it needs to run. |
|
If the property test fails, here is a good place to start. |
|
See script.fs (in Test Centre) for information on populating the |
|
LoadingTestArea folder. *) |
|
[<Fact>] |
|
let ``LoadingTestArea contains at least 100 test images`` () = |
|
let length = imagesInLoadingTestArea.Length |
|
let imagesAreThere = if length < 100 then false else true |
|
Assert.True imagesAreThere |
|
|
|
[<Fact>] |
|
let ``Divide By Zero Exception is thrown when 0 rows is used when determining horizontal lines`` () = |
|
let result () = GridPainter.determineHorizontalLines 100 100 0 |
|
Assert.Throws<DivideByZeroException>(fun () -> result () |> ignore) |
|
|
|
[<Fact>] |
|
let ``Divide By Zero Exception is thrown when 0 columns is used when determining vertical lines`` () = |
|
let result () = GridPainter.determineVerticalLines 100 100 0 |
|
Assert.Throws<DivideByZeroException>(fun () -> result () |> ignore) |