diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/DartFileBuilder.kt b/src/main/kotlin/net/theevilreaper/dartpoet/DartFileBuilder.kt index 84e41165..4bbb3d6f 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/DartFileBuilder.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/DartFileBuilder.kt @@ -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 @@ -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 } diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/util/Constants.kt b/src/main/kotlin/net/theevilreaper/dartpoet/util/Constants.kt index abd3d5e6..c0367f7b 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/util/Constants.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/util/Constants.kt @@ -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]. @@ -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) -} \ No newline at end of file + 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) +} diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt index f6ae3c09..d813c5a2 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt @@ -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" ) } diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/util/IndentTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/util/IndentTest.kt new file mode 100644 index 00000000..21e29706 --- /dev/null +++ b/src/test/kotlin/net/theevilreaper/dartpoet/util/IndentTest.kt @@ -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) } + } + } +}