-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement factory to create directive objects (#80)
* Add factory class to create directives * Add directive type enum * Update constructor definition from the directive implementations * Add test for the new factory * Update directive usage * Remove wildcard import * Fix test class
- Loading branch information
1 parent
ea94c85
commit 48f3255
Showing
15 changed files
with
261 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/kotlin/net/theevilreaper/dartpoet/directive/DirectiveFactoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IllegalStateException> { current() } | ||
assertEquals(IllegalStateException::class.java, exception.javaClass) | ||
assertEquals(expectedMessage, exception.message) | ||
} | ||
} |
Oops, something went wrong.