Skip to content

Commit

Permalink
Merge pull request #1260 from wandernauta/underscore
Browse files Browse the repository at this point in the history
Add stringToName special case for all-underscores identifier strings
  • Loading branch information
superaxander authored Oct 18, 2024
2 parents 3313255 + bb2d0f6 commit 9c739cc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/col/vct/col/origin/Origin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ case class IndirectName(name: Name) extends NameStrategy {
object SourceName {
def stringToName(name: String): Name =
Name.Preferred(
if (name.forall(c => !c.isLetter || c.isUpper))
if (name.matches("[_]+"))
Seq(name)
else if (name.forall(c => !c.isLetter || c.isUpper))
name.split("[_]+").toIndexedSeq
else
name.split("[_]+").toIndexedSeq.flatMap(splitNameRec)
Expand Down
35 changes: 35 additions & 0 deletions test/col/origin/OriginSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package origin

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.prop.TableDrivenPropertyChecks.{Table, forAll}
import vct.col.origin.SourceName

class OriginSpec extends AnyFlatSpec {
behavior of "SourceName.stringToName"

it should "come up with a reasonable name for any identifier" in {
val cases = Table(
("string", "snake", "camel"),
("foo", "foo", "foo"),
("foo_bar", "foo_bar", "fooBar"),
("foo_bar_baz", "foo_bar_baz", "fooBarBaz"),
("montAiguille", "mont_aiguille", "montAiguille"),
("GERVANNE", "gervanne", "gervanne"),
("VeRnAiSoN", "ve_rn_ai_so_n", "veRnAiSoN"),
("_", "_", "_"),
("___", "___", "___"),
("_a", "a", "a"),
("a_", "a", "a"),
("a_b_", "a_b", "aB"),
("_a_b", "a_b", "aB"),
("do__ob", "do_ob", "doOb"),
("do____ob", "do_ob", "doOb"),
)

forAll(cases) { (string, snake, camel) =>
val name = SourceName.stringToName(string)
assert(name.snake == snake)
assert(name.camel == camel)
}
}
}

0 comments on commit 9c739cc

Please sign in to comment.