Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theEvilReaper committed Apr 28, 2024
1 parent eb139fe commit f07e2cf
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.theevilreaper.dartpoet.code.writer
import com.google.common.truth.Truth.assertThat
import net.theevilreaper.dartpoet.parameter.ParameterSpec
import net.theevilreaper.dartpoet.type.ParameterizedTypeName.Companion.parameterizedBy
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -51,7 +52,7 @@ class ParameterWriterTest {
fun `test invalid parameter definition`() {
assertThrows(
IllegalStateException::class.java,
{ ParameterSpec.builder("").build() },
{ ParameterSpec.builder(EMPTY_STRING).build() },
"The name of a parameter can't be empty"
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.theevilreaper.dartpoet.directive

import net.theevilreaper.dartpoet.util.EMPTY_STRING
import net.theevilreaper.dartpoet.util.SPACE_STRING
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.EnumSource
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream

Expand All @@ -16,16 +19,22 @@ class DirectiveFactoryTest {
@JvmStatic
private fun invalidFactoryUsage() = Stream.of(
Arguments.of(
{ DirectiveFactory.create(DirectiveType.LIBRARY, "") },
{ DirectiveFactory.create(DirectiveType.LIBRARY, EMPTY_STRING) },
"The library directive doesn't support a cast type or import cast. Please use #createLibDirective method instead"
),
Arguments.of(
{ DirectiveFactory.create(DirectiveType.LIBRARY, "", castType = CastType.HIDE) },
{ DirectiveFactory.create(DirectiveType.LIBRARY, EMPTY_STRING, castType = CastType.HIDE) },
"The library directive doesn't support a cast type or import cast. Please use #createLibDirective method instead"
),
)
}

@ParameterizedTest
@EnumSource(value = DirectiveType::class, mode = EnumSource.Mode.MATCH_ALL)
fun `test invalid import mapping`(directiveType: DirectiveType) {
assertThrows<IllegalStateException> { DirectiveFactory.create(directiveType, SPACE_STRING) }
}

@ParameterizedTest
@MethodSource("invalidFactoryUsage")
fun `test invalid factory usage`(current: () -> Directive, expectedMessage: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.theevilreaper.dartpoet.directive

import net.theevilreaper.dartpoet.util.DirectiveOrdering
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
Expand Down Expand Up @@ -82,7 +83,7 @@ class DirectiveSortTest {
return directives.map {
var directive: String = it.asString()
for (placeholder in placeholders) {
directive = directive.replace(placeholder, "")
directive = directive.replace(placeholder, EMPTY_STRING)
}
return@map directive
}.toList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.theevilreaper.dartpoet.directive

import net.theevilreaper.dartpoet.util.EMPTY_STRING
import net.theevilreaper.dartpoet.util.SPACE_STRING
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.EnumSource
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream

Expand All @@ -24,11 +26,11 @@ class DirectiveTest {

@JvmStatic
private fun directivesWhichThrowsException() = Stream.of(
Arguments.of({ DartDirective(" ") }, EMPTY_NAME_MESSAGE),
Arguments.of({ LibraryDirective("") }, EMPTY_NAME_MESSAGE),
Arguments.of({ PartDirective("") }, EMPTY_NAME_MESSAGE),
Arguments.of({ RelativeDirective("") }, EMPTY_NAME_MESSAGE),
Arguments.of({ LibraryDirective("") }, EMPTY_NAME_MESSAGE),
Arguments.of({ DartDirective(SPACE_STRING) }, EMPTY_NAME_MESSAGE),
Arguments.of({ LibraryDirective(EMPTY_STRING) }, EMPTY_NAME_MESSAGE),
Arguments.of({ PartDirective(EMPTY_STRING) }, EMPTY_NAME_MESSAGE),
Arguments.of({ RelativeDirective(EMPTY_STRING) }, EMPTY_NAME_MESSAGE),
Arguments.of({ LibraryDirective(EMPTY_STRING) }, EMPTY_NAME_MESSAGE),
Arguments.of({ DartDirective("flutter/material.dart", CastType.AS, null) }, INVALID_CAST_USAGE),
Arguments.of({ DartDirective("flutter/material.dart", null, "test") }, INVALID_CAST_USAGE),
Arguments.of({ RelativeDirective("flutter/material.dart", CastType.AS, null) }, INVALID_CAST_USAGE),
Expand Down Expand Up @@ -134,21 +136,14 @@ class DirectiveTest {
assertEquals(expected, current.asString())
}

@Test
fun `test cast import with empty cast`() {
assertThrows(
IllegalStateException::class.java,
{ DirectiveFactory.create(DirectiveType.IMPORT, "flutter/material.dart", CastType.AS, " ") },
"The importCast can't be empty"
)
assertThrows(
IllegalStateException::class.java,
{ DirectiveFactory.create(DirectiveType.IMPORT, "flutter/material.dart", CastType.AS, "") },
"The importCast can't be empty"
)
@ParameterizedTest
@EnumSource(value = CastType::class, mode = EnumSource.Mode.MATCH_ALL)
fun `test invalid cast import mapping`(castType: CastType) {
assertThrows<IllegalStateException> {
DirectiveFactory.create(DirectiveType.IMPORT, "flutter/material.dart", castType, SPACE_STRING)
}
}


@Test
fun `test package import`() {
val import = DirectiveFactory.create(DirectiveType.IMPORT, "flutter/material.dart")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class ExportDirectiveTest {

@JvmStatic
private fun invalidExportDirectives(): Stream<Arguments> = Stream.of(
Arguments.of(
{ DirectiveFactory.create(DirectiveType.EXPORT, "") },
"The path of an directive can't be empty"
),
Arguments.of(
{ DirectiveFactory.create(DirectiveType.EXPORT, "dart:math", CastType.DEFERRED, "math") },
"The following cast types are not allowed for an export directive: [DEFERRED, AS]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,15 @@ package net.theevilreaper.dartpoet.directive
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
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

@DisplayName("Test part directive creation")
class PartDirectiveTest {

private val expectedImport = "part 'item_model.freezed.dart';"

companion object {
@JvmStatic
private fun invalidPartDirectives(): Stream<Arguments> = Stream.of(
Arguments.of(
{ DirectiveFactory.create(DirectiveType.IMPORT, " ") },
"The path of an Import can't be empty"
),
Arguments.of(
{ DirectiveFactory.create(DirectiveType.RELATIVE, " ") },
"The path of an Import can't be empty"
)
)
}

@ParameterizedTest(name = "Test invalid directive creation over the DirectiveFactory")
@MethodSource("invalidPartDirectives")
fun `test import with empty path`(current: () -> Directive, expectedMessage: String) {
assertThrows<IllegalStateException>(expectedMessage) { current() }
}

@Test
fun `create part import`() {
val partImport = DirectiveFactory.create(DirectiveType.PART, "item_model.freezed.dart")
assertEquals(expectedImport, partImport.asString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.extension

import net.theevilreaper.dartpoet.type.ClassName
import net.theevilreaper.dartpoet.type.ParameterizedTypeName.Companion.parameterizedBy
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -19,12 +20,12 @@ class ExtensionSpecTest {
private fun invalidExtensionSpecs() = Stream.of(
Arguments.of(
IllegalStateException::class.java,
{ ExtensionSpec.builder("", String::class).build() },
{ ExtensionSpec.builder(EMPTY_STRING, String::class).build() },
"The name of a extension can't be empty"
),
Arguments.of(
IllegalArgumentException::class.java,
{ ExtensionSpec.builder("StringExt", "").build() },
{ ExtensionSpec.builder("StringExt", EMPTY_STRING).build() },
"The name of a ClassName can't be empty (includes only spaces)"
),
Arguments.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,15 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
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

@DisplayName("Tests for the factory spec object")
class FactorySpecTest {

companion object {

@JvmStatic
private fun invalidFactoryCalls(): Stream<Arguments> = Stream.of(
Arguments.of(
{ FactorySpec.constBuilder(Int::class.asTypeName()).build() },
"The initializer block must not be empty"
),
)
}

@ParameterizedTest
@MethodSource("invalidFactoryCalls")
fun `test invalid factory calls`(factory: () -> FactorySpec, message: String) {
assertThrows(message, IllegalStateException::class.java) {
factory.invoke()
@Test
fun `test invalid factory creation`() {
assertThrows("The initializer block must not be empty", IllegalStateException::class.java) {
FactorySpec.constBuilder(Int::class.asTypeName())
.build()
}
}

Expand All @@ -51,4 +36,4 @@ class FactorySpecTest {
assertTrue(specAsBuilder.const)
assertEquals(1, specAsBuilder.parameters.size)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.theevilreaper.dartpoet.function.typedef

import net.theevilreaper.dartpoet.parameter.ParameterSpec
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -19,14 +20,14 @@ class TypeDefSpecTest {
private fun invalidTypeDefs(): Stream<Arguments> = Stream.of(
Arguments.of(
"Empty name",
{ TypeDefSpec.builder("").build() },
{ TypeDefSpec.builder(EMPTY_STRING).build() },
"The name of a typedef can't be empty"
),
Arguments.of(
"Empty function name",
{
TypeDefSpec.builder("Test", Int::class)
.name("")
.name(EMPTY_STRING)
.returns(String::class).build()
},
"The function name of a typedef can't be empty"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.theevilreaper.dartpoet.property

import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -20,7 +21,7 @@ class ConstantPropertySpecTest {
private fun invalidFileConstantSpec() = Stream.of(
Arguments.of(
"The name of a file constant can't be empty",
{ ConstantPropertySpec.classConst("", String::class).build() }
{ ConstantPropertySpec.classConst(EMPTY_STRING, String::class).build() }
),
Arguments.of(
"The initializer can't be empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.type.asTypeName
import net.theevilreaper.dartpoet.util.ALLOWED_CONST_MODIFIERS
import net.theevilreaper.dartpoet.util.ALLOWED_PROPERTY_MODIFIERS
import net.theevilreaper.dartpoet.util.EMPTY_STRING
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -57,7 +58,7 @@ class PropertySpecTest {
"These modifiers [REQUIRED] are not allowed in a property context. Allowed modifiers: $ALLOWED_PROPERTY_MODIFIERS"
),
Arguments.of(
{ PropertySpec.builder("", String::class).build() },
{ PropertySpec.builder(EMPTY_STRING, String::class).build() },
"The name of a property can't be empty"
),
Arguments.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.theevilreaper.dartpoet.type

import net.theevilreaper.dartpoet.util.EMPTY_STRING
import net.theevilreaper.dartpoet.util.SPACE_STRING
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand All @@ -16,8 +18,8 @@ class TypeNameExceptionsTest {

@JvmStatic
private fun testTypeNameConditions() = Stream.of(
Arguments.of({ ClassName("") }, "The name of a ClassName can't be empty (includes only spaces)"),
Arguments.of({ ClassName(" ") }, "The name of a ClassName can't be empty (includes only spaces)"),
Arguments.of({ ClassName(EMPTY_STRING) }, "The name of a ClassName can't be empty (includes only spaces)"),
Arguments.of({ ClassName(SPACE_STRING) }, "The name of a ClassName can't be empty (includes only spaces)"),
Arguments.of(
{
ParameterizedTypeName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FileConventionNamingTest {
}

@ParameterizedTest(name = "Test invalid name pattern: {arguments}")
@ValueSource(strings = ["hello__world.dart", "_hello__world_.dart", "_test", "model_", "", "Dart_FILE"])
@ValueSource(strings = ["hello__world.dart", "_hello__world_.dart", "_test", "model_", EMPTY_STRING, "Dart_FILE"])
fun `test invalid file patterns`(pattern: String) {
assertFalse { isDartConventionFileName(pattern) }
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/net/theevilreaper/dartpoet/util/IndentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import kotlin.test.assertTrue
class IndentTest {

@ParameterizedTest(name = "Test valid indent value: {arguments}")
@ValueSource(strings = [" ", " "])
@ValueSource(strings = [SPACE_STRING, " "])
fun `test valid indent pattern`(indent: String) {
assertTrue { isIndent(indent) }
}

@ParameterizedTest(name = "Test invalid indent value: {arguments}")
@ValueSource(strings = ["", " a", "123"])
@ValueSource(strings = [EMPTY_STRING, " a", "123"])
fun `test invalid indent patterns`(indent: String) {
assertFalse { isIndent(indent) }
}
Expand Down

0 comments on commit f07e2cf

Please sign in to comment.