Skip to content

Commit 1a3ae02

Browse files
maciegKevinGilmore
authored andcommitted
SCALA-38 (eugenp#8548)
1 parent 7451392 commit 1a3ae02

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.scala
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class RegexUnitTest {
7+
private val polishPostalCode = "([0-9]{2})\\-([0-9]{3})".r
8+
private val timestamp = "([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{3})".r
9+
private val timestampUnanchored = timestamp.unanchored
10+
11+
@Test
12+
def givenRegularExpression_whenCallingFindFirstIn_thenShouldFindCorrectMatches(): Unit = {
13+
val postCode = polishPostalCode.findFirstIn("Warsaw 01-011, Jerusalem Avenue")
14+
assertEquals(Some("01-011"), postCode)
15+
}
16+
17+
@Test
18+
def givenRegularExpression_whenCallingFindFirstMatchIn_thenShouldFindCorrectMatches(): Unit = {
19+
val postCodes = polishPostalCode.findFirstMatchIn("Warsaw 01-011, Jerusalem Avenue")
20+
assertEquals(Some("011"), for (m <- postCodes) yield m.group(2))
21+
}
22+
23+
@Test
24+
def givenRegularExpression_whenCallingFindAllIn_thenShouldFindCorrectMatches(): Unit = {
25+
val postCodes = polishPostalCode.findAllIn("Warsaw 01-011, Jerusalem Avenue, Cracow 30-059, Mickiewicza Avenue")
26+
.toList
27+
assertEquals(List("01-011", "30-059"), postCodes)
28+
29+
polishPostalCode.findAllIn("Warsaw 01-011, Jerusalem Avenue, Cracow 30-059, Mickiewicza Avenue")
30+
}
31+
32+
@Test
33+
def givenRegularExpression_whenCallingFindAlMatchlIn_thenShouldFindCorrectMatches(): Unit = {
34+
val postCodes = polishPostalCode.findAllMatchIn("Warsaw 01-011, Jerusalem Avenue, Cracow 30-059, Mickiewicza Avenue")
35+
.toList
36+
val postalDistricts = for (m <- postCodes) yield m.group(1)
37+
assertEquals(List("01", "30"), postalDistricts)
38+
}
39+
40+
@Test
41+
def givenRegularExpression_whenExtractingValues_thenShouldExtractCorrectValues(): Unit = {
42+
val description = "11:34:01.411" match {
43+
case timestamp(hour, minutes, _, _) => s"It's $minutes minutes after $hour"
44+
}
45+
46+
assertEquals("It's 34 minutes after 11", description)
47+
}
48+
49+
@Test
50+
def givenUnanchoredRegularExpression_whenExtractingValues_thenShouldExtractCorrectValues(): Unit = {
51+
val description = "Timestamp: 11:34:01.411 error appeared" match {
52+
case timestampUnanchored(hour, minutes, _, _) => s"It's $minutes minutes after $hour"
53+
}
54+
55+
assertEquals("It's 34 minutes after 11", description)
56+
}
57+
58+
@Test
59+
def givenRegularExpression_whenCallingReplaceAllIn_thenShouldReplaceText(): Unit = {
60+
val minutes = timestamp.replaceAllIn("11:34:01.311", m => m.group(2))
61+
62+
assertEquals("34", minutes)
63+
}
64+
65+
@Test
66+
def givenRegularExpression_whenCallingReplaceAllInWithMatcher_thenShouldReplaceText(): Unit = {
67+
val secondsThatDayInTotal = timestamp.replaceAllIn("11:34:01.311", _ match {
68+
case timestamp(hours, minutes, seconds, _) => s"$hours-$minutes"
69+
})
70+
71+
assertEquals("11-34", secondsThatDayInTotal)
72+
}
73+
}

0 commit comments

Comments
 (0)