<commit_before>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package forms.validation

import forms.FormValidation
import forms.FormValidation.ErrorCode
import forms.vatFinancials.vatBankAccount.SortCode
import models.DateModel
import org.scalatest.{Inside, Inspectors}
import play.api.data.validation.{Constraint, Invalid, Valid}
import uk.gov.hmrc.play.test.UnitSpec


class FormValidationSpec extends UnitSpec with Inside with Inspectors {

  "mandatoryText" must {

    val constraint = FormValidation.mandatoryText()("errorCode")

    "accept a non-blank string as Valid" in {
      constraint("non-blank string") shouldBe Valid
    }

    "reject blank string" in {
      forAll(Seq("", "  ", "    \t   "))(constraint(_) shouldBe Invalid("validation.errorCode.missing"))
    }
  }


  "mandatoryNumericText" must {

    val constraint = FormValidation.mandatoryNumericText()("errorCode")

    "accept a numeric string integer" in {
      constraint("1234") shouldBe Valid
    }

    "reject blank string" in {
      forAll(Seq("", "  ", "    \t   "))(constraint(_) shouldBe Invalid("validation.errorCode.missing"))
    }

    "reject non-numeric string" in {
      constraint("1e10") shouldBe Invalid("validation.numeric")
    }
  }


  "nonEmptyValidText" should {
    val regex = """^[A-Za-z]{1,10}$""".r

    "return valid when string matches regex" in {
      val constraint = FormValidation.nonEmptyValidText(regex)("fieldName")
      constraint("abcdef") shouldBe Valid
    }

    "return invalid when string does not match regex" in {
      val constraint = FormValidation.nonEmptyValidText(regex)("fieldName")
      constraint("a123") shouldBe Invalid("validation.fieldName.invalid")
    }

    "return invalid when string is empty" in {
      val constraint = FormValidation.nonEmptyValidText(regex)("fieldName")
      constraint("") shouldBe Invalid("validation.fieldName.missing")
    }
  }

  "maxLenText" must {
    val constraint = FormValidation.maxLenText(20)("errorCode")

    "accept an empty string as Valid" in {
      constraint("") shouldBe Valid
    }

    "accept a short string as Valid" in {
      constraint("short string") shouldBe Valid
    }

    "reject long string" in {
      constraint("long long long long string") shouldBe Invalid("validation.errorCode.maxlen")
    }
  }

  "taxEstimateTextToLong" must {
    "return MinValue when input converts to value less than zero" in {
      FormValidation.taxEstimateTextToLong("-1") shouldBe Long.MinValue
    }

    "return MaxValue when input converts to value greater than 1e15" in {
      FormValidation.taxEstimateTextToLong("100000000000000000") shouldBe Long.MaxValue
    }

    "return value when input converts to value between 0 and 1e15" in {
      FormValidation.taxEstimateTextToLong("1000") shouldBe 1000<commit_msg>improve unit test<commit_after>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package forms.validation

import forms.FormValidation
import forms.FormValidation.ErrorCode
import forms.vatFinancials.vatBankAccount.SortCode
import models.DateModel
import org.scalatest.{Inside, Inspectors}
import play.api.data.validation.{Constraint, Invalid, Valid}
import uk.gov.hmrc.play.test.UnitSpec


class FormValidationSpec extends UnitSpec with Inside with Inspectors {

  "mandatoryText" must {

    val constraint = FormValidation.mandatoryText()("errorCode")

    "accept a non-blank string as Valid" in {
      constraint("non-blank string") shouldBe Valid
    }

    "reject blank string" in {
      forAll(Seq("", "  ", "    \t   "))(constraint(_) shouldBe Invalid("validation.errorCode.missing"))
    }
  }


  "mandatoryNumericText" must {

    val constraint = FormValidation.mandatoryNumericText()("errorCode")

    "accept a numeric string integer" in {
      constraint("1234") shouldBe Valid
    }

    "reject blank string" in {
      forAll(Seq("", "  ", "    \t   "))(constraint(_) shouldBe Invalid("validation.errorCode.missing"))
    }

    "reject non-numeric string" in {
      constraint("1e10") shouldBe Invalid("validation.numeric")
    }
  }


  "nonEmptyValidText" should {
    val regex = """^[A-Za-z]{1,10}$""".r

    "return valid when string matches regex" in {
      val constraint = FormValidation.nonEmptyValidText(regex)("fieldName")
      constraint("abcdef") shouldBe Valid
    }

    "return invalid when string does not match regex" in {
      val constraint = FormValidation.nonEmptyValidText(regex)("fieldName")
      constraint("a123") shouldBe Invalid("validation.fieldName.invalid")
    }

    "return invalid when string is empty" in {
      val constraint = FormValidation.nonEmptyValidText(regex)("fieldName")
      constraint("") shouldBe Invalid("validation.fieldName.missing")
    }
  }

  "maxLenText" must {
    val maxlen = 70
    val constraint = FormValidation.maxLenText(maxlen)("errorCode")

    "accept an empty string as Valid" in {
      constraint("") shouldBe Valid
    }

    "accept maxLen string as Valid" in {
      constraint("c" * maxlen) shouldBe Valid
    }

    "reject maxLen+1 string" in {
      constraint("c" * (maxlen + 1)) shouldBe Invalid("validation.errorCode.maxlen")
    }

    "reject long string" in {
      constraint("long " * maxlen) shouldBe Invalid("validation.errorCode.maxlen")
    }
  }

  "taxEstimateTextToLong" must {
    "return MinValue when input converts to value less than zero" in {
      FormValidation.taxEstimateTextToLong("-1") shouldBe Long.MinValue
    }

    "return MaxValue when input converts to value greater than 1e15" in {
      FormValidation.taxEstimateTextToLong("100000000000000000") shouldBe Long.MaxValue
    }

    "return value when input converts to value between 0 and 1e15" in {
      FormValidation.taxEstimateTextToLong("1000") shouldBe 1000
{'fraction_non_alphanumeric': 0.10105996805575722, 'fraction_numerical': 0.01684332800929287, 'mean_word_length': 3.641509433962264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4144909', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1879, 'n_words': 597}