Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate.IParamCollection.forAll #88

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
39 changes: 39 additions & 0 deletions src/ARCExpect/ErrorMessage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,45 @@ type ErrorMessage =
str.AppendFormat(" > line '{0}'", line) |> ignore
| None -> ()

match Param.tryGetValueOfCvParamAttr "Position" iParam with
| Some position ->
str.AppendFormat(" > position '{0}'", position) |> ignore
| None -> ()
str.ToString()


static member ofIParamCollection error iParamCollection =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anymore in the current state of the PR is it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


let iParam = Seq.head iParamCollection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not get these functions to be honest. The API naming indicates that it generates error messages from IParam collections, but you only use the first item in the sequence to generate the error message

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, since printing all items would be too much. I mean, this is arbitrary. How many items do we want to display? I chose 1, but we could also do 3 or 5 or any number...
What's your opinion on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, this is arbitrary.

Not really, ideally you want to know all items that do not satisfy the predicate


let str = new StringBuilder()
str.AppendFormat("['{0}', ..] {1}\n", Param.getCvName iParam, error) |> ignore

match Param.tryGetValueOfCvParamAttr "FilePath" iParam with
| Some path ->
str.AppendFormat(" > filePath '{0}'\n", path) |> ignore
| None -> ()

match Param.tryGetValueOfCvParamAttr "Worksheet" iParam with
| Some sheet ->
str.AppendFormat(" > sheet '{0}'", sheet) |> ignore
| None -> ()

match Param.tryGetValueOfCvParamAttr "Row" iParam with
| Some row ->
str.AppendFormat(" > row '{0}'", row) |> ignore
| None -> ()

match Param.tryGetValueOfCvParamAttr "Column" iParam with
| Some column ->
str.AppendFormat(" > column '{0}'", column) |> ignore
| None -> ()

match Param.tryGetValueOfCvParamAttr "Line" iParam with
| Some line ->
str.AppendFormat(" > line '{0}'", line) |> ignore
| None -> ()

match Param.tryGetValueOfCvParamAttr "Position" iParam with
| Some position ->
str.AppendFormat(" > position '{0}'", position) |> ignore
Expand Down
20 changes: 19 additions & 1 deletion src/ARCExpect/ValidationFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
|> ErrorMessage.ofIParam "is empty."
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>

Check warning on line 29 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is invalid: unknown parameter 'expectedValue'

Check warning on line 29 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is incomplete: no documentation for parameter 'targetValue'

Check warning on line 29 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is invalid: unknown parameter 'expectedValue'

Check warning on line 29 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is incomplete: no documentation for parameter 'targetValue'
/// Validates if the value of the given Param is equal to the expected value.
/// </summary>
/// <param name="expectedValue">The expected value to validate against</param>
Expand Down Expand Up @@ -70,7 +70,7 @@
|> ErrorMessage.ofIParam "is invalid."
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>

Check warning on line 73 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is invalid: unknown parameter 'pattern'

Check warning on line 73 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

This XML comment is incomplete: no documentation for parameter 'regex'

Check warning on line 73 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is invalid: unknown parameter 'pattern'

Check warning on line 73 in src/ARCExpect/ValidationFunctions.fs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

This XML comment is incomplete: no documentation for parameter 'regex'
/// Validates if the value of the given Param matches a regex.
/// </summary>
/// <param name="pattern">The regex that the value should match</param>
Expand Down Expand Up @@ -134,7 +134,7 @@
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>
/// Validates if the given Param is contained in the given collection át least once.
/// Validates if the given Param is contained in the given collection at least once.
/// </summary>
/// <param name="expectedParam">the param expected to occur at least once in the given collection</param>
/// <param name="paramCollection">The param collection to validate</param>
Expand All @@ -152,6 +152,24 @@
|> ErrorMessage.ofIParam $"does not exist"
|> Expecto.Tests.failtestNoStackf "%s"

/// <summary>
/// Validates if all elements in the given IParam collection satisfy the predicate function.
/// </summary>
/// <param name="predicate">A function that evaluates to true if the element satisfies the requirements.</param>
/// <param name="paramCollection">The IParam collection to validate.</param>
static member AllItemsSatisfyPredicate (predicate : #IParam -> bool) (paramCollection : #seq<#IParam>) =
use en = paramCollection.GetEnumerator()
let rec loop () =
match en.MoveNext() with
| true ->
if predicate en.Current |> not then
ErrorMessage.ofIParam $"does not satisfy predicate" en.Current
|> Expecto.Tests.failtestNoStackf "%s"
loop ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested wether this continues after 1 failtestNoStackf is executed? If that throws an exception i am quite sure that it wont

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. It is more complicated to do this than I thought...
But I think I'll get a solution.

else loop ()
| false -> ()
loop ()


/// <summary>
/// Validates wether the given Param's value is an email that matches a pre-defined regex pattern ("^[^@\s]+@[^@\s]+\.[^@\s]+$")
Expand Down
1 change: 1 addition & 0 deletions tests/ARCExpect.Tests/ARCExpect.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<EmbeddedResource Remove="CLITests\**" />
<Compile Include="InternalUtilsTests.fs" />
<Compile Include="ReferenceObjects.fs" />
<Compile Include="ErrorMessageTests.fs" />
<Compile Include="ExpectoExtensionsTests.fs" />
<Compile Include="PathConfigTests.fs" />
<Compile Include="DirTokenizerTests.fs" />
Expand Down
21 changes: 21 additions & 0 deletions tests/ARCExpect.Tests/ErrorMessageTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ErrorMessageTests


open Expecto
open ARCExpect
open ControlledVocabulary


let dummyIParam = CvParam("test:0", "testTerm", "test", ParamValue.Value "no val")
let dummyIParamColl = List.init 3 (fun _ -> dummyIParam)


[<Tests>]
let ``ErrorMessage tests`` =
testList "ErrorMessage" [
testList "ofIParamCollection" [
testCase "resolves correctly" <| fun _ ->
let eMsg = ErrorMessage.ofIParamCollection "does not satisfy" dummyIParamColl
Expect.equal eMsg "['testTerm', ..] does not satisfy\n" "resolved incorrectly"
]
]
Loading