Skip to content

Commit

Permalink
Add indent check to prevent invalid indent usage (#35)
Browse files Browse the repository at this point in the history
* Add method to check if an indent string contains only spaces

* Update indent check

* Update exception type in the assertThrows method calls

* Add a test for the indent pattern
  • Loading branch information
theEvilReaper authored Jun 20, 2023
1 parent a81e3f1 commit 48648e6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/net/theevilreaper/dartpoet/DartFileBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.theevilreaper.dartpoet.extension.ExtensionSpec
import net.theevilreaper.dartpoet.directive.Directive
import net.theevilreaper.dartpoet.property.DartPropertySpec
import net.theevilreaper.dartpoet.util.DEFAULT_INDENT
import java.lang.IllegalArgumentException
import net.theevilreaper.dartpoet.util.isIndent

class DartFileBuilder(
val name: String
Expand Down Expand Up @@ -50,9 +50,7 @@ class DartFileBuilder(
}

fun indent(indent: String) = apply {
if (indent.trim().isEmpty()) {
throw IllegalArgumentException("The indent can't be empty")
}
check(isIndent(indent)) { "An indent can only contains only spaces" }
this.indent = indent
}

Expand Down
26 changes: 24 additions & 2 deletions src/main/kotlin/net/theevilreaper/dartpoet/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ internal val ALLOWED_FUNCTION_MODIFIERS = setOf(DartModifier.PUBLIC, DartModifie
internal val ALLOWED_PROPERTY_MODIFIERS = setOf(DartModifier.PRIVATE, DartModifier.FINAL, DartModifier.LATE, DartModifier.STATIC, DartModifier.CONST)
internal val ALLOWED_CLASS_CONST_MODIFIERS = setOf(DartModifier.STATIC, DartModifier.CONST)
internal val ALLOWED_CONST_MODIFIERS = setOf(DartModifier.CONST)

//RegEx
private val namePattern: Regex = Regex("[a-z]+|([a-z]+)_+([a-z]+)")
private val lowerCamelCase: Regex = Regex("[a-z]+[A-Z0-9]*[a-z0-9]*[A-Za-z0-9]*")
private val indentPattern: Regex = Regex(" +")

/**
* Checks if a given set of [DartModifier] matches with a given set which contains the allowed [DartModifier].
Expand Down Expand Up @@ -67,5 +70,24 @@ fun isDartConventionFileName(fileName: String): Boolean {
* @return true when the string has the format otherwise false
*/
fun isInLowerCamelCase(input: String): Boolean {
return input.trim().isNotEmpty() && input.matches(lowerCamelCase)
}
return testStringForPattern(input, lowerCamelCase)
}

/**
* Checks if a given [String] can be used as an indent.
* @param input the string to check
* @return true when the string only contains spaces otherwise false
*/
fun isIndent(input: String): Boolean {
return testStringForPattern(input, indentPattern)
}

/**
* Add base method for all the method which tests a string for a pattern.
* @param input the string to check
* @param pattern the pattern for the check
* @return true when the string matches with the pattern otherwise false
*/
private fun testStringForPattern(input: String, pattern: Regex): Boolean {
return input.isNotEmpty() && input.matches(pattern)
}
6 changes: 3 additions & 3 deletions src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class DartFileTest {
@Test
fun `test indent set`() {
assertThrows(
IllegalArgumentException::class.java,
IllegalStateException::class.java,
{ DartFile.builder("Test").indent("") },
"The indent can't be empty"
)
assertThrows(
IllegalArgumentException::class.java,
{ DartFile.builder("Test").indent { "" } },
IllegalStateException::class.java,
{ DartFile.builder("Test").indent { " 123AB" } },
"The indent can't be empty"
)
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/kotlin/net/theevilreaper/dartpoet/util/IndentTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.theevilreaper.dartpoet.util

import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class IndentTest {

companion object {

@JvmStatic
private fun indentTest() = Stream.of(
Arguments.of(" ", true),
Arguments.of(" ", true),
Arguments.of("", false),
Arguments.of(" a", false),
Arguments.of("123", false),
)
}

@ParameterizedTest
@MethodSource("indentTest")
fun `test indent pattern`(indent: String, expected: Boolean) {
if (expected) {
assertTrue { isIndent(indent) }
} else {
assertFalse { isIndent(indent) }
}
}
}

0 comments on commit 48648e6

Please sign in to comment.