-
Notifications
You must be signed in to change notification settings - Fork 0
DataClassResolver→ClassResolver; 192 tests pass #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/dev/appoutlet/some/exception/SomeInstantiationException.kt
This file contains hidden or 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,10 @@ | ||
| package dev.appoutlet.some.exception | ||
|
|
||
| import kotlin.reflect.KClass | ||
|
|
||
| class SomeInstantiationException( | ||
| kClass: KClass<*>, | ||
| failures: List<String>, | ||
| ) : Exception( | ||
| "Failed to instantiate ${kClass.simpleName}. All constructors failed:\n${failures.joinToString("\n")}" | ||
| ) |
This file contains hidden or 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
129 changes: 129 additions & 0 deletions
129
src/test/kotlin/dev/appoutlet/some/resolver/ClassResolverTest.kt
This file contains hidden or 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,129 @@ | ||
| package dev.appoutlet.some.resolver | ||
|
|
||
| import dev.appoutlet.some.exception.SomeInstantiationException | ||
| import dev.appoutlet.some.some | ||
| import dev.appoutlet.some.test.defaultTestChain | ||
| import kotlin.random.Random | ||
| import kotlin.reflect.typeOf | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFailsWith | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertTrue | ||
|
|
||
| abstract class AbstractClass | ||
|
|
||
| class ConcreteClass : AbstractClass() | ||
|
|
||
| data class SimpleDataClass(val name: String) | ||
|
|
||
| data class Wrapper<T>(val value: T) | ||
|
|
||
| data class SimpleDataClassWithGenerics(val names: Wrapper<String>) | ||
|
|
||
| class SecondaryOnlyClass private constructor(val value: String) { | ||
| constructor(chars: List<Char>) : this(chars.joinToString("")) | ||
| } | ||
|
|
||
| class FallbackClass private constructor(val name: String, val extra: AbstractClass) { | ||
| constructor(name: String) : this(name, ConcreteClass()) | ||
| } | ||
|
|
||
| class EmptyConstructorClass { | ||
| val greeting: String = "hello" | ||
| } | ||
|
|
||
| class UnresolvableParamClass(val param: AbstractClass) | ||
|
|
||
| class PrivateConstructorClass private constructor(val secret: String) | ||
|
|
||
| sealed class TestSealed { | ||
| data class Sub(val x: Int) : TestSealed() | ||
| } | ||
|
|
||
| class ClassResolverTest { | ||
| private val resolver = ClassResolver(random = Random.Default) | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves data class with primary constructor`() { | ||
| val result = resolver.resolve(typeOf<SimpleDataClass>(), defaultTestChain) | ||
| assertTrue(result is SimpleDataClass) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves data class with primary constructor with generics`() { | ||
| val result = resolver.resolve(typeOf<SimpleDataClassWithGenerics>(), defaultTestChain) | ||
| assertTrue(result is SimpleDataClassWithGenerics) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves class with secondary constructor only`() { | ||
| val result = resolver.resolve(typeOf<SecondaryOnlyClass>(), defaultTestChain) | ||
| assertTrue(result is SecondaryOnlyClass) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves external kotlin class`() { | ||
| val result = resolver.resolve(typeOf<Pair<SimpleDataClass, FallbackClass>>(), defaultTestChain) | ||
| val castedResult = result as Pair<SimpleDataClass, FallbackClass> | ||
| assertTrue(castedResult.first is SimpleDataClass) | ||
| assertTrue(castedResult.second is FallbackClass) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves external java class`() { | ||
| val date = resolver.resolve(typeOf<java.util.Date>(), defaultTestChain) | ||
| assertNotNull(date) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver falls back to secondary constructor when primary fails`() { | ||
| val result = resolver.resolve(typeOf<FallbackClass>(), defaultTestChain) | ||
| assertTrue(result is FallbackClass) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves class with empty constructor`() { | ||
| val result = resolver.resolve(typeOf<EmptyConstructorClass>(), defaultTestChain) | ||
| assertTrue(result is EmptyConstructorClass) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver throws SomeInstantiationException when all constructors fail`() { | ||
| assertFailsWith<SomeInstantiationException> { | ||
| resolver.resolve(typeOf<UnresolvableParamClass>(), defaultTestChain) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve accepts class with constructor`() { | ||
| assertTrue(resolver.canResolve(typeOf<SimpleDataClass>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve accepts class with secondary constructor only`() { | ||
| assertTrue(resolver.canResolve(typeOf<SecondaryOnlyClass>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve accepts class with empty constructor`() { | ||
| assertTrue(resolver.canResolve(typeOf<EmptyConstructorClass>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects well known types`() { | ||
| assertFalse(resolver.canResolve(typeOf<String>())) | ||
| assertFalse(resolver.canResolve(typeOf<Int>())) | ||
| assertFalse(resolver.canResolve(typeOf<List<String>>())) | ||
| assertFalse(resolver.canResolve(typeOf<Set<String>>())) | ||
| assertFalse(resolver.canResolve(typeOf<Map<String, Int>>())) | ||
| assertFalse(resolver.canResolve(typeOf<AbstractClass>())) | ||
| assertFalse(resolver.canResolve(typeOf<TestSealed>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves class with private constructor via reflection`() { | ||
| val result = resolver.resolve(typeOf<PrivateConstructorClass>(), defaultTestChain) | ||
| assertTrue(result is PrivateConstructorClass) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/oc add a trailing line