Skip to content

Commit

Permalink
GFORMS-2973 - Return English if Welsh is blank (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmanson authored Oct 30, 2024
1 parent fc7b3da commit 90a94ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/uk/gov/hmrc/gform/sharedmodel/LocalisedString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import play.api.libs.functional.syntax._
import uk.gov.hmrc.gform.sharedmodel.formtemplate.JsonUtils

case class LocalisedString(m: Map[LangADT, String]) {
def value(implicit l: LangADT): String = m.getOrElse(l, m.getOrElse(LangADT.En, ""))
def value(implicit l: LangADT): String = {
val check: String = m.getOrElse(l, "")
if (check.isBlank) m.getOrElse(LangADT.En, "") else check
}

def replace(toReplace: String, replaceWith: String): LocalisedString =
copy(m = (m.map { case (lang, message) => (lang, message.replace(toReplace, replaceWith)) }))
def transform(fEn: String => String, fCy: String => String): LocalisedString =
Expand Down
21 changes: 21 additions & 0 deletions test/uk/gov/hmrc/gform/sharedmodel/SmartStringSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import uk.gov.hmrc.gform.sharedmodel.formtemplate.generators.{ ExprGen, Primitiv

class SmartStringSpec extends Spec {

def welsh: String = "Welsh"
def english: String = "English"

"JSON" should "round trip" in {
forAll(Gen.asciiStr, Gen.asciiStr, PrimitiveGen.zeroOrMoreGen(ExprGen.exprGen())) { (english, welsh, exprs) =>
val cEnglish = condition(english)
Expand All @@ -35,6 +38,24 @@ class SmartStringSpec extends Spec {
}
}

"SmartString when Welsh requested" should "return English if Welsh not defined" in {
implicit val l: LangADT = LangADT.Cy
val smartString = SmartString(LocalisedString(Map(LangADT.En -> english)), Nil)
smartString.rawDefaultValue shouldBe english
}

it should "return English if Welsh is defined and blank" in {
implicit val l: LangADT = LangADT.Cy
val smartString = SmartString(LocalisedString(Map(LangADT.En -> english, LangADT.Cy -> "")), Nil)
smartString.rawDefaultValue shouldBe english
}

it should "return Welsh if Welsh is defined and not blank" in {
implicit val l: LangADT = LangADT.Cy
val smartString = SmartString(LocalisedString(Map(LangADT.En -> english, LangADT.Cy -> welsh)), Nil)
smartString.rawDefaultValue shouldBe welsh
}

private def condition(s: String): String =
s.flatMap { c =>
if (c >= 32 && c <= 127 && c != '"' && c != '\\') Seq(c)
Expand Down

0 comments on commit 90a94ef

Please sign in to comment.