Skip to content

Commit

Permalink
Merge pull request #498 from hmrc/TGP-2684
Browse files Browse the repository at this point in the history
TGP-2684: Allow passing in custom methods to the fieldWithMaxLength for cases where we do not allow spaces to fix flaky test
  • Loading branch information
Reece-Carruthers authored Oct 14, 2024
2 parents 302848b + 246a43e commit 9df5dd2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 9 additions & 0 deletions test-utils/generators/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ trait Generators {
def trimmedAsciiPrintableChars(length: Int): Gen[List[Char]] =
listOfN(length, Gen.asciiPrintableChar).suchThat(_.mkString.trim.length == length)

def noSpacesPrintableChars(length: Int): Gen[List[Char]] =
listOfN(length, Gen.asciiPrintableChar).suchThat(_.mkString.trim.replaceAll("\\s+", " ").length == length)

implicit val dontShrink: Shrink[String] = Shrink.shrinkAny

def genIntersperseString(gen: Gen[String], value: String, frequencyV: Int = 1, frequencyN: Int = 10): Gen[String] = {
Expand Down Expand Up @@ -102,6 +105,12 @@ trait Generators {
chars <- trimmedAsciiPrintableChars(length)
} yield chars.mkString

def noSpaceStringsLongerThan(minLength: Int): Gen[String] = for {
maxLength <- (minLength * 2).max(100)
length <- Gen.chooseNum(minLength + 1, maxLength)
chars <- noSpacesPrintableChars(length)
} yield chars.mkString

def nonEmptyStringWithoutExcludedValues(excluded: Seq[String]): Gen[String] =
nonEmptyString.suchThat(str => str.nonEmpty && !excluded.exists(str.contains))

Expand Down
3 changes: 2 additions & 1 deletion test/forms/NameFormProviderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class NameFormProviderSpec extends StringFieldBehaviours {
form,
fieldName,
maxLength = maxLength,
lengthError = FormError(fieldName, lengthKey, Seq(maxLength))
lengthError = FormError(fieldName, lengthKey, Seq(maxLength)),
generateString = noSpaceStringsLongerThan
)

behave like mandatoryField(
Expand Down
10 changes: 8 additions & 2 deletions test/forms/behaviours/StringFieldBehaviours.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ import play.api.data.{Form, FormError}

trait StringFieldBehaviours extends FieldBehaviours {

def fieldWithMaxLength(form: Form[_], fieldName: String, maxLength: Int, lengthError: FormError): Unit =
def fieldWithMaxLength(
form: Form[_],
fieldName: String,
maxLength: Int,
lengthError: FormError,
generateString: Int => Gen[String] = stringsLongerThan
): Unit =
s"not bind strings longer than $maxLength characters" in {

forAll(stringsLongerThan(maxLength) -> "longString") { string =>
forAll(generateString(maxLength) -> "longString") { string =>
val result = form.bind(Map(fieldName -> string)).apply(fieldName)
result.errors.find(_ == lengthError) mustBe defined
}
Expand Down

0 comments on commit 9df5dd2

Please sign in to comment.