Skip to content

Commit 4fb0fb7

Browse files
Merge pull request #1427 from yadavan88/uniform-case-string
Sampe code for Uniform case string check
2 parents 063f5a2 + 431968e commit 4fb0fb7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.scala.uniformcase
2+
3+
object UniformCaseChecker {
4+
5+
def convertAndCheck(str: String): Boolean = {
6+
str.toUpperCase == str || str.toLowerCase == str
7+
}
8+
9+
def isUpperLowerAndForAll(str: String): Boolean = {
10+
val filteredStr = str.filter(_.isLetter)
11+
filteredStr.forall(_.isUpper) || filteredStr.forall(_.isLower)
12+
}
13+
14+
def regexCheck(str: String): Boolean = {
15+
val filteredStr = str.filter(_.isLetter)
16+
filteredStr.matches("^[A-Z]*$") || filteredStr.matches("^[a-z]*$")
17+
}
18+
19+
def countAndCheck(str: String): Boolean = {
20+
val filteredStr = str.filter(_.isLetter)
21+
filteredStr.count(_.isUpper) == 0 || filteredStr.count(_.isLower) == 0
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.scala.uniformcase
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
import org.scalatest.prop.TableDrivenPropertyChecks
6+
import UniformCaseChecker.*
7+
8+
class UniformCaseCheckerUnitTest
9+
extends AnyFlatSpec
10+
with Matchers
11+
with TableDrivenPropertyChecks {
12+
13+
private val fns =
14+
Seq(convertAndCheck, isUpperLowerAndForAll, regexCheck, countAndCheck)
15+
16+
private val table = Table(
17+
("Input", "Expected Result"),
18+
("BAELDUNG @ 2024", true),
19+
("baeldung @ 2024", true),
20+
("Baeldung @ 2024", false),
21+
("2024 @@@ ", true),
22+
(" ", true)
23+
)
24+
25+
it should "check if all characters are upper or lower" in {
26+
fns foreach { fn =>
27+
forAll(table) { (input, expected) =>
28+
withClue("for string: " + input) {
29+
fn(input) shouldBe expected
30+
}
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)