-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #443 - [extras-string] Add case conversion - string.toPascalCase
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
modules/extras-string/shared/src/main/scala/extras/strings/syntax/cases.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package extras.strings.syntax | ||
|
||
import java.util.Locale | ||
|
||
/** @author Kevin Lee | ||
* @since 2023-10-16 | ||
*/ | ||
trait cases { | ||
implicit def stringCaseOps(s: String): cases.StringCaseOps = new cases.StringCaseOps(s) | ||
} | ||
object cases extends cases { | ||
|
||
final class StringCaseOps(private val s: String) extends AnyVal { | ||
|
||
def toPascalCase: String = | ||
s.headOption.fold("")(_.toUpper.toString) + s.drop(1).toLowerCase(Locale.ENGLISH) | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
modules/extras-string/shared/src/test/scala/extras/strings/syntax/casesSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package extras.strings.syntax | ||
|
||
import extras.strings.syntax.cases._ | ||
import hedgehog._ | ||
import hedgehog.runner._ | ||
|
||
import java.util.Locale | ||
|
||
/** @author Kevin Lee | ||
* @since 2023-10-16 | ||
*/ | ||
object casesSpec extends Properties { | ||
override def tests: List[Test] = StringCaseOpsSpec.tests | ||
|
||
object StringCaseOpsSpec { | ||
private val name = this.getClass.getSimpleName.stripSuffix("$") | ||
def tests: List[Test] = List( | ||
property(s"$name: test String.toPascalCase with already PascalCase", testToPascalCaseWithAlreadyPascalCase), | ||
property(s"$name: test String.toPascalCase with PascalCases", testToPascalCaseWithPascalCases), | ||
property(s"$name: test String.toPascalCase with camelCases", testToPascalCaseWithCamelCases), | ||
property(s"$name: test String.toPascalCase with all UPPERCASE", testToPascalCaseWithUpperCase), | ||
property(s"$name: test String.toPascalCase with all lowercase", testToPascalCaseWithLowerCase), | ||
) | ||
|
||
def testToPascalCaseWithAlreadyPascalCase: Property = for { | ||
expected <- genPascalCase(1, 10).log("expected") | ||
input <- Gen.constant(expected).log("input") | ||
} yield { | ||
val actual = input.toPascalCase | ||
actual ==== expected | ||
} | ||
|
||
def testToPascalCaseWithPascalCases: Property = for { | ||
s1 <- genPascalCase(1, 10).log("s1") | ||
s2 <- genPascalCase(1, 10).list(Range.linear(1, 4)).log("s2") | ||
input <- Gen.constant(s1 + s2.mkString).log("input") | ||
} yield { | ||
val expected = s1 + s2.mkString.toLowerCase(Locale.ENGLISH) | ||
val actual = input.toPascalCase | ||
println( | ||
s""" input: $input | ||
| actual: $actual | ||
|expected: $expected | ||
|""".stripMargin | ||
) | ||
actual ==== expected | ||
} | ||
|
||
def testToPascalCaseWithCamelCases: Property = for { | ||
s1 <- genPascalCase(1, 10).log("s1") | ||
s2 <- genPascalCase(1, 10).list(Range.linear(1, 4)).log("s2") | ||
input <- Gen.constant(s1.updated(0, s1.charAt(0).toLower) + s2.mkString).log("input") | ||
} yield { | ||
val expected = s1 + s2.mkString.toLowerCase(Locale.ENGLISH) | ||
val actual = input.toPascalCase | ||
println( | ||
s""" input: $input | ||
| actual: $actual | ||
|expected: $expected | ||
|""".stripMargin | ||
) | ||
actual ==== expected | ||
} | ||
|
||
def testToPascalCaseWithUpperCase: Property = for { | ||
expected <- genPascalCase(1, 10).log("expected") | ||
input <- Gen.constant(expected.toUpperCase(Locale.ENGLISH)).log("input") | ||
} yield { | ||
val actual = input.toPascalCase | ||
actual ==== expected | ||
} | ||
|
||
def testToPascalCaseWithLowerCase: Property = for { | ||
expected <- genPascalCase(1, 10).log("expected") | ||
input <- Gen.constant(expected.toLowerCase(Locale.ENGLISH)).log("input") | ||
} yield { | ||
val actual = input.toPascalCase | ||
actual ==== expected | ||
} | ||
|
||
} | ||
|
||
def genPascalCase(min: Int, max: Int): Gen[String] = | ||
for { | ||
head <- Gen.upper | ||
tail <- Gen.string(Gen.lower, Range.linear(min - 1, max - 1)) | ||
} yield head +: tail | ||
|
||
def genCamelCaseList(range: Range[Int]): Gen[List[String]] = | ||
Gen.string(Gen.lower, Range.linear(1, 5)).list(range) | ||
|
||
} |