diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt b/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt index 657c769e..9138a787 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt @@ -5,7 +5,11 @@ import net.theevilreaper.dartpoet.clazz.ClassSpec import net.theevilreaper.dartpoet.code.CodeWriter import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.DartFileWriter -import net.theevilreaper.dartpoet.directive.* +import net.theevilreaper.dartpoet.directive.DartDirective +import net.theevilreaper.dartpoet.directive.ExportDirective +import net.theevilreaper.dartpoet.directive.LibraryDirective +import net.theevilreaper.dartpoet.directive.PartDirective +import net.theevilreaper.dartpoet.directive.RelativeDirective import net.theevilreaper.dartpoet.extension.ExtensionSpec import net.theevilreaper.dartpoet.function.FunctionSpec import net.theevilreaper.dartpoet.util.* @@ -32,12 +36,16 @@ class DartFile internal constructor( private val directives = builder.directives.toImmutableList() - internal val dartImports = DirectiveOrdering.sortDirectives(DartDirective::class, directives) { it.contains("dart:") } - internal val packageImports = DirectiveOrdering.sortDirectives(DartDirective::class, directives) { it.contains("package:")} + internal val dartImports = + DirectiveOrdering.sortDirectives(DartDirective::class, directives) { it.contains("dart:") } + internal val packageImports = + DirectiveOrdering.sortDirectives(DartDirective::class, directives) { it.contains("package:") } internal val partImports = DirectiveOrdering.sortDirectives(PartDirective::class, directives) internal val libImport = DirectiveOrdering.sortDirectives(LibraryDirective::class, directives) - internal val exportDirectives = DirectiveOrdering.sortDirectives(ExportDirective::class, directives) - internal val relativeImports = DirectiveOrdering.sortDirectives(RelativeDirective::class, directives) + internal val exportDirectives = + DirectiveOrdering.sortDirectives(ExportDirective::class, directives) + internal val relativeImports = + DirectiveOrdering.sortDirectives(RelativeDirective::class, directives) internal val typeDefs = builder.typeDefs.toImmutableList() internal val hasTypeDefs = typeDefs.isNotEmpty() diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/DartDirective.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/DartDirective.kt index b7a898f9..d138b6b9 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/directive/DartDirective.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/DartDirective.kt @@ -15,7 +15,7 @@ import net.theevilreaper.dartpoet.util.SEMICOLON * * @constructor Creates a Dart import directive with the specified path as [String], a cast type as [CastType], and a importCast a [String]. */ -class DartDirective( +class DartDirective internal constructor( private val path: String, private val castType: CastType? = null, private val importCast: String? = null, diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactory.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactory.kt new file mode 100644 index 00000000..54e901a9 --- /dev/null +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactory.kt @@ -0,0 +1,85 @@ +package net.theevilreaper.dartpoet.directive + +/** + * The [DirectiveFactory] should be used to create a new instance of different [Directive] implementations. + * It's required to use this factory to create a new instance because it will check if the given [DirectiveType]. + * The usage of the constructor of each implementation is not possible. + * @author theEvilReaper + * @version 1.0.0 + * @since 1.0.0 + **/ +object DirectiveFactory { + + /** + * Creates a new instance from a [Directive] implementation depends on the given [DirectiveType]. + * @param directive the type of the directive + * @param path the path to the file + * @return the created [Directive] instance + */ + @Throws(IllegalStateException::class) + fun create( + directive: DirectiveType, + path: String, + ): Directive { + check(directive != DirectiveType.LIBRARY) { + "The library directive doesn't support a cast type or import cast. Please use #createLibDirective method instead" + } + return create(directive, path, false, null, null) + } + + /** + * Creates a new instance from a [Directive] implementation depends on the given [DirectiveType]. + * If the [Directive] implementation doesn't support the given [CastType] or [importCast] option. + * It will throw an [IllegalStateException]. + * @param directive the type of the directive + * @param path the path to the file + * @param castType the [CastType] to use + * @param importCast the import cast to use + * @return the created [Directive] instance + */ + @Throws(IllegalStateException::class) + fun create( + directive: DirectiveType, + path: String, + castType: CastType? = null, + importCast: String? = null, + ): Directive { + check(directive != DirectiveType.LIBRARY) { + "The library directive doesn't support a cast type or import cast. Please use #createLibDirective method instead" + } + return create(directive, path, false, castType, importCast) + } + + @Throws(IllegalStateException::class) + fun createLib( + path: String, + partOf: Boolean = false, + ) = create(DirectiveType.LIBRARY, path, partOf, null, null) + + /** + * Creates a new instance from a [Directive] implementation depends on the given [DirectiveType]. + * If the [Directive] implementation doesn't support the given [CastType] or [importCast] option. + * It will throw an [IllegalStateException]. + * @param directive the type of the directive + * @param path the path to the file + * @param castType the [CastType] to use + * @param importCast the import cast to use + * @return the created [Directive] instance + */ + @Throws(IllegalStateException::class) + private fun create( + directive: DirectiveType, + path: String, + partOf: Boolean = false, + castType: CastType? = null, + importCast: String? = null, + ): Directive { + return when (directive) { + DirectiveType.IMPORT -> DartDirective(path, castType, importCast) + DirectiveType.RELATIVE -> RelativeDirective(path, castType, importCast) + DirectiveType.PART -> PartDirective(path) + DirectiveType.LIBRARY -> LibraryDirective(path, partOf) + DirectiveType.EXPORT -> ExportDirective(path, castType, importCast) + } + } +} diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveType.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveType.kt new file mode 100644 index 00000000..a6c9e9a4 --- /dev/null +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveType.kt @@ -0,0 +1,16 @@ +package net.theevilreaper.dartpoet.directive + + +/** + * The [DirectiveType] enum represents the different types of directives. + * @since 1.0.0 + * @version 1.0.0 + * @author theEvilReaper + */ +enum class DirectiveType { + IMPORT, + RELATIVE, + PART, + LIBRARY, + EXPORT +} \ No newline at end of file diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/ExportDirective.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/ExportDirective.kt index 405736d3..28717200 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/directive/ExportDirective.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/ExportDirective.kt @@ -12,7 +12,7 @@ import net.theevilreaper.dartpoet.util.SEMICOLON * @since 1.0.0 * @author theEvilReaper */ -class ExportDirective( +class ExportDirective internal constructor( private val path: String, private val castType: CastType? = null, private val importCast: String? = null, diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/LibraryDirective.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/LibraryDirective.kt index 575427e7..4d45f5d1 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/directive/LibraryDirective.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/LibraryDirective.kt @@ -9,7 +9,7 @@ import net.theevilreaper.dartpoet.util.SEMICOLON * @since 1.0.0 * @author theEvilReaper */ -class LibraryDirective( +class LibraryDirective internal constructor( private val path: String, private val asPartOf: Boolean = false ) : BaseDirective(path) { diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/PartDirective.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/PartDirective.kt index f8f20a32..a8412ecc 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/directive/PartDirective.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/PartDirective.kt @@ -1,6 +1,7 @@ package net.theevilreaper.dartpoet.directive import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.directive.BaseDirective import net.theevilreaper.dartpoet.util.SEMICOLON /** @@ -9,7 +10,7 @@ import net.theevilreaper.dartpoet.util.SEMICOLON * @since 1.0.0 * @author theEvilReaper */ -class PartDirective( +class PartDirective internal constructor( private val path: String ) : BaseDirective(path) { diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/directive/RelativeDirective.kt b/src/main/kotlin/net/theevilreaper/dartpoet/directive/RelativeDirective.kt index 10a7fc60..631cb94c 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/directive/RelativeDirective.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/directive/RelativeDirective.kt @@ -1,6 +1,8 @@ package net.theevilreaper.dartpoet.directive import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.directive.BaseDirective +import net.theevilreaper.dartpoet.directive.CastType import net.theevilreaper.dartpoet.util.SEMICOLON /** @@ -9,7 +11,7 @@ import net.theevilreaper.dartpoet.util.SEMICOLON * @since 1.0.0 * @author theEvilReaper */ -class RelativeDirective( +class RelativeDirective internal constructor( private val path: String, private val castType: CastType? = null, private val importCast: String? = null, diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/DartFileImportTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/DartFileImportTest.kt index 3dec2029..d301d867 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/DartFileImportTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/DartFileImportTest.kt @@ -1,13 +1,12 @@ package net.theevilreaper.dartpoet -import com.google.common.truth.Truth.* +import com.google.common.truth.Truth.assertThat import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.clazz.ClassSpec import net.theevilreaper.dartpoet.code.buildCodeBlock import net.theevilreaper.dartpoet.directive.CastType -import net.theevilreaper.dartpoet.directive.DartDirective -import net.theevilreaper.dartpoet.directive.ExportDirective -import net.theevilreaper.dartpoet.directive.PartDirective +import net.theevilreaper.dartpoet.directive.DirectiveFactory +import net.theevilreaper.dartpoet.directive.DirectiveType import net.theevilreaper.dartpoet.function.FunctionSpec import net.theevilreaper.dartpoet.type.ClassName import net.theevilreaper.dartpoet.type.ParameterizedTypeName.Companion.parameterizedBy @@ -22,10 +21,10 @@ class DartFileImportTest { val reduxAction = ClassName("ReduxAction").parameterizedBy(appState) val dartFile = DartFile.builder("Test") .directives( - DartDirective("dart:io"), - DartDirective("dart:math"), - DartDirective("async_redux/async_redux.dart"), - DartDirective("model/${model.name}.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:io"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:math"), + DirectiveFactory.create(DirectiveType.IMPORT, "async_redux/async_redux.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "model/${model.name}.dart"), ) .type( ClassSpec.builder("TestAction") @@ -74,10 +73,10 @@ class DartFileImportTest { fun `test directives with an export directive`() { val classFile = DartFile.builder("House") .directives( - DartDirective("dart:io"), - DartDirective("door"), - PartDirective("house_part.dart"), - ExportDirective("garden.dart", CastType.SHOW, "garden"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:io"), + DirectiveFactory.create(DirectiveType.IMPORT, "door"), + DirectiveFactory.create(DirectiveType.PART, "house_part.dart"), + DirectiveFactory.create(DirectiveType.EXPORT, "garden.dart", CastType.SHOW, "garden"), ) .type( ClassSpec.builder("House") @@ -102,6 +101,7 @@ class DartFileImportTest { @immutable class House {} - """.trimIndent()) + """.trimIndent() + ) } } diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt index 56e68e8b..fd86b864 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/DartFileTest.kt @@ -1,25 +1,25 @@ package net.theevilreaper.dartpoet -import com.google.common.truth.Truth.* +import com.google.common.truth.Truth.assertThat import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.clazz.ClassSpec import net.theevilreaper.dartpoet.code.buildCodeBlock +import net.theevilreaper.dartpoet.directive.CastType +import net.theevilreaper.dartpoet.directive.DirectiveFactory +import net.theevilreaper.dartpoet.directive.DirectiveType import net.theevilreaper.dartpoet.enum.EnumPropertySpec import net.theevilreaper.dartpoet.function.FunctionSpec import net.theevilreaper.dartpoet.function.constructor.ConstructorSpec -import net.theevilreaper.dartpoet.directive.DartDirective -import net.theevilreaper.dartpoet.directive.CastType -import net.theevilreaper.dartpoet.directive.LibraryDirective -import net.theevilreaper.dartpoet.directive.PartDirective import net.theevilreaper.dartpoet.function.typedef.TypeDefSpec -import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec import net.theevilreaper.dartpoet.parameter.ParameterSpec import net.theevilreaper.dartpoet.property.PropertySpec +import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec import net.theevilreaper.dartpoet.type.ClassName import net.theevilreaper.dartpoet.type.DYNAMIC import net.theevilreaper.dartpoet.type.ParameterizedTypeName.Companion.parameterizedBy import net.theevilreaper.dartpoet.type.asTypeName -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test import kotlin.test.assertContentEquals @@ -89,9 +89,9 @@ class DartFileTest { } val versionFile = DartFile.builder("version.dart") .directives( - DartDirective("freezed_annotation/freezed_annotation.dart"), - PartDirective("version.freezed.dart"), - PartDirective("version.g.dart") + DirectiveFactory.create(DirectiveType.IMPORT, "freezed_annotation/freezed_annotation.dart"), + DirectiveFactory.create(DirectiveType.PART, "version.freezed.dart"), + DirectiveFactory.create(DirectiveType.PART, "version.g.dart") ) .type( versionFreezedClass @@ -133,9 +133,9 @@ class DartFileTest { .build() ) .directives( - DartDirective("dart:html"), - LibraryDirective("testLib"), - DartDirective("dart:math", CastType.AS, "math"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:html"), + DirectiveFactory.createLib("testLib"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:math", CastType.AS, "math"), ) .build() assertThat(libClass.toString()).isEqualTo( @@ -251,7 +251,7 @@ class DartFileTest { .build() val file = DartFile.builder("${className}Handler") - .directive(LibraryDirective("testLibrary", true)) + .directive(DirectiveFactory.createLib("testLibrary", true)) .type(handlerApiClass) .build() assertThat(file.toString()).isEqualTo( @@ -336,7 +336,7 @@ class DartFileTest { fun `test class write with constant values`() { val name = "environment" val classFile = DartFile.builder(name) - .directive(DartDirective("dart:html")) + .directive(DirectiveFactory.create(DirectiveType.IMPORT, "dart:html")) .constants( ConstantPropertySpec.fileConst("typeLive").initWith("1").build(), ConstantPropertySpec.fileConst("typeTest").initWith("10").build(), diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactoryTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactoryTest.kt new file mode 100644 index 00000000..806589eb --- /dev/null +++ b/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactoryTest.kt @@ -0,0 +1,34 @@ +package net.theevilreaper.dartpoet.directive + +import org.junit.jupiter.api.Assertions.* +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 + +class DirectiveFactoryTest { + + companion object { + + @JvmStatic + private fun invalidFactoryUsage() = Stream.of( + Arguments.of( + { DirectiveFactory.create(DirectiveType.LIBRARY, "") }, + "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) }, + "The library directive doesn't support a cast type or import cast. Please use #createLibDirective method instead" + ), + ) + } + + @ParameterizedTest + @MethodSource("invalidFactoryUsage") + fun `test invalid factory usage`(current: () -> Directive, expectedMessage: String) { + val exception = assertThrows { current() } + assertEquals(IllegalStateException::class.java, exception.javaClass) + assertEquals(expectedMessage, exception.message) + } +} diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveSortTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveSortTest.kt index 675bdfa9..3bcc66a6 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveSortTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveSortTest.kt @@ -17,9 +17,9 @@ class DirectiveSortTest { private fun dartDirectiveArguments(): Stream = Stream.of( Arguments.of( listOf( - DartDirective("dart:io"), - DartDirective("dart:math"), - PartDirective("test.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:io"), + DirectiveFactory.create(DirectiveType.IMPORT, "dart:math"), + DirectiveFactory.create(DirectiveType.PART, "test.dart"), ), listOf( "dart:io", @@ -32,10 +32,10 @@ class DirectiveSortTest { private fun directiveSortArguments(): Stream = Stream.of( Arguments.of( listOf( - DartDirective("testD.dart"), - DartDirective("testA.dart"), - DartDirective("testB.dart"), - DartDirective("testC.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "testD.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "testA.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "testB.dart"), + DirectiveFactory.create(DirectiveType.IMPORT, "testC.dart"), ), listOf( "testA.dart", @@ -73,7 +73,10 @@ class DirectiveSortTest { assertEquals(emptyList(), DirectiveOrdering.sortDirectives(DartDirective::class, listOf())) } - private inline fun formatImports(directives: List, vararg placeholders: String): List { + private inline fun formatImports( + directives: List, + vararg placeholders: String, + ): List { return directives.map { var directive: String = it.asString() for (placeholder in placeholders) { diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveTest.kt index 9b184373..a788ad1c 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveTest.kt @@ -1,6 +1,7 @@ package net.theevilreaper.dartpoet.directive import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import org.junit.jupiter.params.ParameterizedTest @@ -36,14 +37,23 @@ class DirectiveTest { @JvmStatic private fun libDirectives() = Stream.of( - Arguments.of(LibraryDirective("testLib"), "library testLib;"), - Arguments.of(LibraryDirective("testLib", true), "part of testLib;") + Arguments.of( + DirectiveFactory.createLib("testLib"), + "library testLib;" + ), + Arguments.of( + DirectiveFactory.createLib("testLib", true), + "part of testLib;" + ), ) @JvmStatic private fun dartDirectives() = Stream.of( - Arguments.of(DartDirective("dart:html"), "import 'dart:html';"), - Arguments.of(DartDirective("dart:http", CastType.AS, "http"), "import 'dart:http' as http;") + Arguments.of(DirectiveFactory.create(DirectiveType.IMPORT, "dart:html"), "import 'dart:html';"), + Arguments.of( + DirectiveFactory.create(DirectiveType.IMPORT, "dart:http", CastType.AS, "http"), + "import 'dart:http' as http;" + ) ) @JvmStatic @@ -62,15 +72,31 @@ class DirectiveTest { "import '../../model/item_model.dart' hide item;" ), Arguments.of( - RelativeDirective(TEST_IMPORT, CastType.SHOW, CAST_VALUE), + DirectiveFactory.create(DirectiveType.RELATIVE, TEST_IMPORT), + "import '../../model/item_model.dart';" + ), + Arguments.of( + DirectiveFactory.create(DirectiveType.RELATIVE, TEST_IMPORT, CastType.AS, CAST_VALUE), + "import '../../model/item_model.dart' as item;" + ), + Arguments.of( + DirectiveFactory.create(DirectiveType.RELATIVE, TEST_IMPORT, CastType.DEFERRED, CAST_VALUE), + "import '../../model/item_model.dart' deferred as item;" + ), + Arguments.of( + DirectiveFactory.create(DirectiveType.RELATIVE, TEST_IMPORT, CastType.HIDE, CAST_VALUE), + "import '../../model/item_model.dart' hide item;" + ), + Arguments.of( + DirectiveFactory.create(DirectiveType.RELATIVE, TEST_IMPORT, CastType.SHOW, CAST_VALUE), "import '../../model/item_model.dart' show item;" ) ) @JvmStatic private fun exportDirectives() = Stream.of( - Arguments.of(ExportDirective("test.dart"), "export 'test.dart';"), - Arguments.of(ExportDirective("new_lib"), "export 'new_lib.dart';") + Arguments.of(DirectiveFactory.create(DirectiveType.EXPORT, "test.dart"), "export 'test.dart';"), + Arguments.of(DirectiveFactory.create(DirectiveType.EXPORT, "new_lib"), "export 'new_lib.dart';") ) } @@ -106,9 +132,24 @@ 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" + ) + } + + @Test fun `test package import`() { - val import = DartDirective("flutter/material.dart") + val import = DirectiveFactory.create(DirectiveType.IMPORT, "flutter/material.dart") assertEquals(packageImport, import.asString()) } } diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/directive/ExportDirectiveTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/directive/ExportDirectiveTest.kt index f081b32a..cfa91fb1 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/directive/ExportDirectiveTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/directive/ExportDirectiveTest.kt @@ -1,6 +1,6 @@ package net.theevilreaper.dartpoet.directive -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.assertThrows import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments @@ -14,25 +14,31 @@ class ExportDirectiveTest { @JvmStatic private fun invalidExportDirectives(): Stream = Stream.of( Arguments.of( - { ExportDirective("") }, + { DirectiveFactory.create(DirectiveType.EXPORT, "") }, "The path of an directive can't be empty" ), Arguments.of( - { ExportDirective("dart:math", CastType.DEFERRED, "math") }, + { DirectiveFactory.create(DirectiveType.EXPORT, "dart:math", CastType.DEFERRED, "math") }, "The following cast types are not allowed for an export directive: [DEFERRED, AS]" ), Arguments.of( - { ExportDirective("dart:math", CastType.AS, "math") }, + { DirectiveFactory.create(DirectiveType.EXPORT, "dart:math", CastType.AS, "math") }, "The following cast types are not allowed for an export directive: [DEFERRED, AS]" ) ) @JvmStatic private fun exportDirectives(): Stream = Stream.of( - Arguments.of(ExportDirective("test.dart"), "export 'test.dart';"), - Arguments.of(ExportDirective("new_lib"), "export 'new_lib.dart';"), - Arguments.of(ExportDirective("new_lib", CastType.SHOW, "lib"), "export 'new_lib.dart' show lib;"), - Arguments.of(ExportDirective("new_lib", CastType.HIDE, "lib"), "export 'new_lib.dart' hide lib;") + Arguments.of(DirectiveFactory.create(DirectiveType.EXPORT, "test.dart"), "export 'test.dart';"), + Arguments.of(DirectiveFactory.create(DirectiveType.EXPORT, "new_lib"), "export 'new_lib.dart';"), + Arguments.of( + DirectiveFactory.create(DirectiveType.EXPORT, "new_lib", CastType.SHOW, "lib"), + "export 'new_lib.dart' show lib;" + ), + Arguments.of( + DirectiveFactory.create(DirectiveType.EXPORT, "new_lib", CastType.HIDE, "lib"), + "export 'new_lib.dart' hide lib;" + ) ) } diff --git a/src/test/kotlin/net/theevilreaper/dartpoet/directive/PartDirectiveTest.kt b/src/test/kotlin/net/theevilreaper/dartpoet/directive/PartDirectiveTest.kt index bd2b6edd..f202233e 100644 --- a/src/test/kotlin/net/theevilreaper/dartpoet/directive/PartDirectiveTest.kt +++ b/src/test/kotlin/net/theevilreaper/dartpoet/directive/PartDirectiveTest.kt @@ -12,19 +12,19 @@ class PartDirectiveTest { fun `test import with empty path`() { Assertions.assertThrows( IllegalStateException::class.java, - { PartDirective(" ") }, + { DirectiveFactory.create(DirectiveType.IMPORT, " ") }, "The path of an Import can't be empty" ) Assertions.assertThrows( IllegalStateException::class.java, - { PartDirective("") }, + { DirectiveFactory.create(DirectiveType.RELATIVE, " ") }, "The path of an Import can't be empty" ) } @Test fun `create part import`() { - val partImport = PartDirective("item_model.freezed.dart") + val partImport = DirectiveFactory.create(DirectiveType.PART, "item_model.freezed.dart") assertEquals(expectedImport, partImport.asString()) } } \ No newline at end of file