|
| 1 | +package com.fasterxml.jackson.module.kotlin |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator |
| 4 | +import org.junit.Ignore |
| 5 | +import org.junit.experimental.runners.Enclosed |
| 6 | +import org.junit.runner.RunWith |
| 7 | +import kotlin.reflect.KFunction |
| 8 | +import kotlin.reflect.full.functions |
| 9 | +import kotlin.reflect.full.hasAnnotation |
| 10 | +import kotlin.test.Test |
| 11 | +import kotlin.test.assertEquals |
| 12 | +import kotlin.test.assertFalse |
| 13 | +import kotlin.test.assertTrue |
| 14 | + |
| 15 | + |
| 16 | +@RunWith(Enclosed::class) |
| 17 | +class ArgumentBucketTest { |
| 18 | + class Normal { |
| 19 | + @Ignore |
| 20 | + data class Constructor(val foo: String, val bar: String) |
| 21 | + |
| 22 | + @Test |
| 23 | + fun constructorTest() { |
| 24 | + val function: KFunction<*> = ::Constructor |
| 25 | + val params = function.parameters |
| 26 | + val generator = BucketGenerator.forConstructor(params.size) |
| 27 | + val bucket = generator.generate() |
| 28 | + |
| 29 | + assertTrue(bucket.isEmpty()) |
| 30 | + assertEquals(0, bucket.size) |
| 31 | + assertFalse(bucket.isFullInitialized) |
| 32 | + |
| 33 | + bucket[params[0]] = "foo" |
| 34 | + |
| 35 | + assertFalse(bucket.isEmpty()) |
| 36 | + assertEquals(1, bucket.size) |
| 37 | + assertFalse(bucket.isFullInitialized) |
| 38 | + assertEquals("foo", bucket[params[0]]) |
| 39 | + |
| 40 | + bucket[params[1]] = "bar" |
| 41 | + |
| 42 | + assertFalse(bucket.isEmpty()) |
| 43 | + assertEquals(2, bucket.size) |
| 44 | + assertTrue(bucket.isFullInitialized) |
| 45 | + assertEquals("bar", bucket[params[1]]) |
| 46 | + } |
| 47 | + |
| 48 | + @Ignore |
| 49 | + data class Method(val foo: String, val bar: String) { |
| 50 | + companion object { |
| 51 | + @JvmStatic |
| 52 | + @JsonCreator |
| 53 | + fun of(foo: String, bar: String): Method = Method(foo, bar) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun methodTest() { |
| 59 | + val function: KFunction<*> = Method.Companion::class.functions.first { it.hasAnnotation<JsonCreator>() } |
| 60 | + val params = function.parameters |
| 61 | + val generator = BucketGenerator.forMethod(params.size, params[0], Method) |
| 62 | + val bucket = generator.generate() |
| 63 | + |
| 64 | + assertFalse(bucket.isEmpty()) |
| 65 | + assertEquals(1, bucket.size) |
| 66 | + assertEquals(Method.Companion, bucket[params[0]]) |
| 67 | + assertFalse(bucket.isFullInitialized) |
| 68 | + |
| 69 | + bucket[params[1]] = "foo" |
| 70 | + |
| 71 | + assertFalse(bucket.isEmpty()) |
| 72 | + assertEquals(2, bucket.size) |
| 73 | + assertFalse(bucket.isFullInitialized) |
| 74 | + assertEquals("foo", bucket[params[1]]) |
| 75 | + |
| 76 | + bucket[params[2]] = "bar" |
| 77 | + |
| 78 | + assertFalse(bucket.isEmpty()) |
| 79 | + assertEquals(3, bucket.size) |
| 80 | + assertTrue(bucket.isFullInitialized) |
| 81 | + assertEquals("bar", bucket[params[2]]) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // After support, add a case with a value class. |
| 86 | +} |
0 commit comments