Skip to content

Commit

Permalink
Verify class names, package names, and module names
Browse files Browse the repository at this point in the history
  • Loading branch information
knokko committed Sep 7, 2024
1 parent 3682433 commit c1f2c9b
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/main/kotlin/fixie/generator/module/FixieModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,19 @@ class FixieModule(

for (className in allClassNames) {
if (allClassNames.count { it == className } > 1) {
throw IllegalArgumentException("Duplicate class name $className")
throw InvalidConfigException("Duplicate class name $className")
}
if (!className.matches(Regex("[A-Z_](\$[A-Z_]|[\\w_])*"))) {
throw InvalidConfigException("Invalid class name $className")
}
}

if (!packageName.matches(Regex("^[a-z][a-z0-9_]*(\\.[a-z0-9A-Z_]+)*[a-z0-9A-Z_]*\$"))) {
throw InvalidConfigException("Invalid package name $packageName")
}

if (!moduleName.matches(Regex("[a-z0-9A-Z_-]+"))) {
throw InvalidConfigException("Invalid module name $moduleName")
}
}
}
132 changes: 131 additions & 1 deletion src/test/kotlin/fixie/generator/parser/TestParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class TestParser {
val modules = parseModule(
"""{
packageName = 'kit.%TYPE%.%OVERFLOW%',
moduleName = 'module.kit.%TYPE%.%OVERFLOW%',
moduleName = 'module-kit-%TYPE%-%OVERFLOW%',
numbers = [{
className = 'Hello',
internalType = '%TYPE%',
Expand Down Expand Up @@ -318,4 +318,134 @@ class TestParser {
"Expected $error to contain 'you must use all variations in the moduleName to avoid this'"
)
}

@Test
fun testDuplicateClassName() {
assertEquals(
"Duplicate class name Hello",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = 'hello',
moduleName = 'hello',
numbers = [{
className = 'Hello',
internalType = 'Short',
oneValue = 50,
checkOverflow = false
}],
displacements = [{
className = 'Hello',
number = 'Hello',
oneUnit = 'Meter',
displayUnit = 'Meter',
createNumberExtensions = false
}]
}"""
)
}.message
)
}

@Test
fun testInvalidModuleName() {
assertEquals(
"Invalid module name hel lo",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = 'hello',
moduleName = 'hel lo'
}"""
)
}.message
)
}

@Test
fun testInvalidPackageName() {
assertEquals(
"Invalid package name hel lo",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = 'hel lo',
moduleName = 'hello'
}"""
)
}.message
)
}

@Test
fun testInvalidClassName() {
assertEquals(
"Invalid class name Hel lo",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = 'hello',
moduleName = 'hello',
numbers = [{
className = 'Hel lo',
internalType = 'Short',
oneValue = 50,
checkOverflow = false
}],
}"""
)
}.message
)
}

@Test
fun testEmptyModuleName() {
assertEquals(
"Invalid module name ",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = 'hello',
moduleName = ''
}"""
)
}.message
)
}

@Test
fun testEmptyPackageName() {
assertEquals(
"Invalid package name ",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = '',
moduleName = 'hello'
}"""
)
}.message
)
}

@Test
fun testEmptyClassName() {
assertEquals(
"Invalid class name ",
assertThrows<InvalidConfigException> {
parseModule(
"""{
packageName = 'hello',
moduleName = 'hello',
numbers = [{
className = '',
internalType = 'Short',
oneValue = 50,
checkOverflow = false
}],
}"""
)
}.message
)
}
}

0 comments on commit c1f2c9b

Please sign in to comment.