Skip to content

Commit

Permalink
resolved merge conflicts again
Browse files Browse the repository at this point in the history
  • Loading branch information
mycp98 committed May 13, 2024
2 parents d7c4710 + 930ad8e commit 42e2456
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
5 changes: 3 additions & 2 deletions app/forms/NirmsNumberFormProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
package forms

import javax.inject.Inject

import forms.mappings.Mappings
import forms.mappings.helpers.RemoveWhitespace.removeWhitespace
import play.api.data.Form

class NirmsNumberFormProvider @Inject() extends Mappings {

def apply(): Form[String] =
Form(
"value" -> text("nirmsNumber.error.required")
.verifying(maxLength(100, "nirmsNumber.error.length"))
.transform(removeWhitespace, identity[String])
.verifying(regexp("RMS-?(GB|NI)-?[0-9]{6}", "nirmsNumber.error.invalidFormat"))
)
}
2 changes: 1 addition & 1 deletion test/controllers/NirmsNumberControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class NirmsNumberControllerSpec extends SpecBase with MockitoSugar {
running(application) {
val request =
FakeRequest(POST, nirmsNumberRoute)
.withFormUrlEncodedBody(("value", "answer"))
.withFormUrlEncodedBody(("value", "RMS-GB-123456"))

val result = route(application, request).value

Expand Down
58 changes: 35 additions & 23 deletions test/forms/NirmsNumberFormProviderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,49 @@
package forms

import forms.behaviours.StringFieldBehaviours
import play.api.data.FormError
import org.scalacheck.Gen
import play.api.data.{Form, FormError}

class NirmsNumberFormProviderSpec extends StringFieldBehaviours {
private val formProvider = new NirmsNumberFormProvider()
private val form: Form[String] = formProvider()
private val requiredKey = "nirmsNumber.error.required"
private val invalidKey = "nirmsNumber.error.invalidFormat"

val requiredKey = "nirmsNumber.error.required"
val lengthKey = "nirmsNumber.error.length"
val maxLength = 100
val nirmsNumberGenerator: Gen[String] = {
val regionGen = Gen.oneOf("GB", "NI")
val digitsGen = Gen.listOfN(6, Gen.numChar).map(_.mkString)
val hyphenGen = Gen.oneOf("", "-")

val form = new NirmsNumberFormProvider()()
for {
region <- regionGen
digits <- digitsGen
hyphen1 <- hyphenGen
hyphen2 <- hyphenGen
} yield s"RMS$hyphen1$region$hyphen2$digits"
}

val nonNirmsNumberGenerator: Gen[String] = {
val invalidRegionGen = Gen.alphaStr.suchThat(s => s != "GB" && s != "NI" && s.nonEmpty)
val invalidDigitsGen = for {
length <- Gen.choose(1, 10)
digits <- Gen.listOfN(length, Gen.oneOf(Gen.alphaChar, Gen.numChar))
} yield digits.mkString

Gen.oneOf(
invalidRegionGen.map(region => s"RMS-$region-123456"),
invalidDigitsGen.map(digits => s"RMS-GB-$digits")
)
}

".value" - {
".nirmsNumber" - {

val fieldName = "value"

behave like fieldThatBindsValidData(
form,
fieldName,
stringsWithMaxLength(maxLength)
)
behave like mandatoryField(form, fieldName, requiredError = FormError(fieldName, requiredKey))

behave like fieldWithMaxLength(
form,
fieldName,
maxLength = maxLength,
lengthError = FormError(fieldName, lengthKey, Seq(maxLength))
)
behave like fieldThatBindsValidData(form, fieldName, nirmsNumberGenerator)

behave like mandatoryField(
form,
fieldName,
requiredError = FormError(fieldName, requiredKey)
)
behave like fieldThatErrorsOnInvalidData(form, fieldName, nonNirmsNumberGenerator, FormError(fieldName, invalidKey))
}
}
}

0 comments on commit 42e2456

Please sign in to comment.