-
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 1 commit
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
135 changes: 135 additions & 0 deletions
135
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,135 @@ | ||
| 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.assertIsNot | ||
| import kotlin.test.assertTrue | ||
|
|
||
| abstract class UnresolvableBase | ||
|
|
||
| class ConcreteUnresolvableBase : UnresolvableBase() | ||
|
|
||
| class ClassResolverTest { | ||
| private val resolver = ClassResolver(random = Random.Default) | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves data class with primary constructor`() { | ||
| val result = resolver.resolve(typeOf<SimpleData>(), defaultTestChain) | ||
| assertTrue(result is SimpleData) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves class with secondary constructor only`() { | ||
| val result = resolver.resolve(typeOf<SecondaryOnlyClass>(), defaultTestChain) | ||
| assertTrue(result is SecondaryOnlyClass) | ||
| } | ||
|
|
||
| @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<SimpleData>())) | ||
| } | ||
|
|
||
| @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 String type`() { | ||
| assertFalse(resolver.canResolve(typeOf<String>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects Int type`() { | ||
| assertFalse(resolver.canResolve(typeOf<Int>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects List type`() { | ||
| assertFalse(resolver.canResolve(typeOf<List<String>>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects Set type`() { | ||
| assertFalse(resolver.canResolve(typeOf<Set<String>>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects Map type`() { | ||
| assertFalse(resolver.canResolve(typeOf<Map<String, Int>>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects abstract class`() { | ||
| assertFalse(resolver.canResolve(typeOf<UnresolvableBase>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver canResolve rejects sealed class`() { | ||
| assertFalse(resolver.canResolve(typeOf<TestSealed>())) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver preserves primary constructor success path for data classes`() { | ||
| val result = some<SimpleData>() | ||
| assertTrue(result.name.isNotEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ClassResolver resolves class with private constructor via reflection`() { | ||
| val result = resolver.resolve(typeOf<PrivateConstructorClass>(), defaultTestChain) | ||
| assertTrue(result is PrivateConstructorClass) | ||
| } | ||
| } | ||
|
|
||
| data class SimpleData(val name: String) | ||
|
|
||
| class SecondaryOnlyClass private constructor(val value: String) { | ||
| constructor(chars: List<Char>) : this(chars.joinToString("")) | ||
| } | ||
|
|
||
| class FallbackClass private constructor(val name: String, val extra: UnresolvableBase) { | ||
| constructor(name: String) : this(name, ConcreteUnresolvableBase()) | ||
| } | ||
|
|
||
| class EmptyConstructorClass { | ||
| val greeting: String = "hello" | ||
| } | ||
|
|
||
| class UnresolvableParamClass(val param: UnresolvableBase) | ||
|
|
||
| class PrivateConstructorClass private constructor(val secret: String) | ||
|
|
||
| sealed class TestSealed { | ||
| data class Sub(val x: Int) : TestSealed() | ||
| } |
Oops, something went wrong.
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