Skip to content

Commit 7adafb7

Browse files
committed
Use reified type parameter
1 parent 141a784 commit 7adafb7

File tree

15 files changed

+20
-23
lines changed

15 files changed

+20
-23
lines changed

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/TestHelpers.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fasterxml.jackson.module.kotlin.test
22

3-
import kotlin.reflect.KClass
43
import kotlin.test.fail
54

65
/**
@@ -13,18 +12,18 @@ import kotlin.test.fail
1312
* should make it clear what has happened (i.e. that some previously broken functionality
1413
* has been fixed).
1514
*
16-
* @param throwableType The expected throwable
15+
* @param T The expected throwable
1716
* @param fixMessage The message to print when the block succeeds
1817
* @param block The block to execute
1918
*/
20-
fun <T : Throwable> expectFailure(
21-
throwableType: KClass<T>,
19+
inline fun <reified T : Throwable> expectFailure(
2220
fixMessage: String,
2321
block: () -> Unit
2422
) {
2523
try {
2624
block()
2725
} catch (e: Throwable) {
26+
val throwableType = T::class
2827
if (throwableType.isInstance(e)) {
2928
// Expected exception, do nothing
3029
} else {

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/TestHelpersTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import kotlin.test.assertEquals
77
class TestHelpersTest {
88
@Test
99
fun expectFailure_ExpectedExceptionThrown() {
10-
expectFailure(AssertionError::class, "This will not be printed") {
10+
expectFailure<AssertionError>("This will not be printed") {
1111
throw AssertionError("This is expected")
1212
}
1313
}
1414

1515
@Test
1616
fun expectFailure_AnotherExceptionThrown() {
1717
val throwable = assertThrows(AssertionError::class.java) {
18-
expectFailure(AssertionError::class, "This will not be printed") {
18+
expectFailure<AssertionError>("This will not be printed") {
1919
throw Exception("This is not expected")
2020
}
2121
}
@@ -27,7 +27,7 @@ class TestHelpersTest {
2727
fun expectFailure_NoExceptionThrown() {
2828
val fixMessage = "Test will fail with this message"
2929
val throwable = assertThrows(AssertionError::class.java) {
30-
expectFailure(AssertionError::class, fixMessage) {
30+
expectFailure<AssertionError>(fixMessage) {
3131
// Do nothing
3232
}
3333
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github138.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TestGithub138 {
2626
fun testDeserProblem() {
2727
val xml = """<sms Phone="435242423412" Id="43234324">Lorem ipsum</sms>"""
2828
val xmlMapper = XmlMapper().registerKotlinModule()
29-
expectFailure(InvalidDefinitionException::class, "GitHub #138 has been fixed!") {
29+
expectFailure<InvalidDefinitionException>("GitHub #138 has been fixed!") {
3030
val jsms = xmlMapper.readValue<Sms>(xml)
3131
}
3232
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github160DisableAnnotations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestGithub160 {
1313
@Test
1414
fun dataClass() {
1515
val mapper = jacksonObjectMapper().configure(MapperFeature.USE_ANNOTATIONS, false)
16-
expectFailure(MismatchedInputException::class, "GitHub #160 has been fixed!") {
16+
expectFailure<MismatchedInputException>("GitHub #160 has been fixed!") {
1717
mapper.readValue<DataClass>("""{"blah":"blah"}""")
1818
}
1919
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github271AlphaSortProperties.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TestGithub271 {
1717
val mapper = jacksonObjectMapper()
1818

1919
val json = mapper.writeValueAsString(Foo("a", "c"))
20-
expectFailure(AssertionError::class, "GitHub #271 has been fixed!") {
20+
expectFailure<AssertionError>("GitHub #271 has been fixed!") {
2121
assertEquals("""{"a":"a","b":"b","c":"c"}""", json)
2222
}
2323
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github335.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Github335Test {
3030
val json = mapper.writeValueAsString(oldEntity)
3131
val newEntity = mapper.readValue<MyEntity>(json)
3232

33-
expectFailure(AssertionError::class, "GitHub #335 has been fixed!") {
33+
expectFailure<AssertionError>("GitHub #335 has been fixed!") {
3434
// newEntity.type is the string "null" instead of the null value
3535
assertNull(newEntity.type)
3636
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github340.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class OwnerRequestTest {
2121

2222
@Test
2323
fun testDeserHit340() {
24-
expectFailure(UnrecognizedPropertyException::class, "GitHub #340 has been fixed!") {
24+
expectFailure<UnrecognizedPropertyException>("GitHub #340 has been fixed!") {
2525
val value: IsField = jackson.readValue(json)
2626
assertEquals("Got a foo", value.foo)
2727
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github396.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.fasterxml.jackson.module.kotlin.registerKotlinModule
66
import com.fasterxml.jackson.module.kotlin.test.expectFailure
77
import kotlin.test.Test
88
import kotlin.test.assertEquals
9-
import kotlin.test.fail
109

1110
class TestGithub396 {
1211
/**
@@ -18,7 +17,7 @@ class TestGithub396 {
1817
val mapper = XmlMapper().registerKotlinModule()
1918

2019
val xml = "<product><stuff></stuff></product>"
21-
expectFailure(MismatchedInputException::class, "GitHub #396 has been fixed!") {
20+
expectFailure<MismatchedInputException>("GitHub #396 has been fixed!") {
2221
val product: Product = mapper.readValue(xml, Product::class.java)
2322

2423
assertEquals(Product(null), product)

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github50.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TestGithub50 {
1818
@Test
1919
fun testGithub50UnwrappedError() {
2020
val json = """{"firstName":"John","lastName":"Smith","position":"Manager"}"""
21-
expectFailure(InvalidDefinitionException::class, "GitHub #50 has been fixed!") {
21+
expectFailure<InvalidDefinitionException>("GitHub #50 has been fixed!") {
2222
val obj: Employee = jacksonObjectMapper().readValue(json)
2323
}
2424
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github54.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
77
import com.fasterxml.jackson.module.kotlin.readValue
88
import com.fasterxml.jackson.module.kotlin.test.expectFailure
99
import org.junit.Test
10-
import kotlin.test.fail
1110

1211
class TestGithub54 {
1312
@Test
@@ -22,7 +21,7 @@ class TestGithub54 {
2221
entity1.entity2 = entity2
2322

2423
val json = mapper.writeValueAsString(entity1)
25-
expectFailure(UnresolvedForwardReference::class, "GitHub #54 has been fixed!") {
24+
expectFailure<UnresolvedForwardReference>("GitHub #54 has been fixed!") {
2625
mapper.readValue<Entity1>(json)
2726
}
2827

0 commit comments

Comments
 (0)