Skip to content

Added sample for string interpolation matching #1359

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

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ class PatternMatching {
}
}

def stringInterpolationMatching(toMatch: String): String = {
toMatch match {
case s"$firstName.$lastName@$domain.$extension" =>
s"Hey ${firstName.capitalize} ${lastName.capitalize}, $domain.$extension is your email domain"
case s"$day-$month-${year}T$time" => s"$month $day, $year"
case s"$something($parenthesis)${_}" =>
s"String between parenthesis: $parenthesis"
case _ => "unknown pattern"
}
}

def optionsPatternMatching(option: Option[String]): String = {
option match {
case Some(value) => s"I'm not an empty option. Value $value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,46 @@ class PatternMatchingUnitTest {
)
}

@Test
def whenEmailIsPassedWithComDomain_itShouldExtractParts(): Unit = {
val result = new PatternMatching().stringInterpolationMatching(
"[email protected]"
)
assertEquals("Hey James Kirk, starfleet.com is your email domain", result)
}

@Test
def whenEmailIsPassedWithCoInDomain_itShouldExtractParts(): Unit = {
val result = new PatternMatching().stringInterpolationMatching(
"[email protected]"
)
assertEquals("Hey James Kirk, starfleet.co.in is your email domain", result)
}

@Test
def whenDateTimeIsPassed_itShouldExtractDate(): Unit = {
val result = new PatternMatching().stringInterpolationMatching(
"01-April-2024T10:20:30"
)
assertEquals("April 01, 2024", result)
}

@Test
def whenStringIsPassed_itShouldExtractDataBetweenParenthesis(): Unit = {
val result = new PatternMatching().stringInterpolationMatching(
"Here is a (special) string"
)
assertEquals("String between parenthesis: special", result)
}

@Test
def whenUnknownDataIsPassed_itShouldReturnDefaultString(): Unit = {
val result = new PatternMatching().stringInterpolationMatching(
"something-unknown.unmatched"
)
assertEquals("unknown pattern", result)
}

@Test
def whenAFilledOptionIsPassed_ThenItShouldMatchTheSomeClause(): Unit = {
val result =
Expand Down