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 indent check to prevent invalid indent usage #35

Merged
merged 4 commits into from
Jun 20, 2023
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
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) }
}
}
}