Skip to content
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

Add stringToName special case for all-underscores identifier strings #1260

Merged
merged 1 commit into from
Oct 18, 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
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)
}
}
}
Loading