mirror of
https://github.com/corda/corda.git
synced 2025-04-07 19:34:41 +00:00
Minor: add a unit test for the StringToMethodParser code as used with c'tors.
This commit is contained in:
parent
e67521796f
commit
3636f137b0
@ -193,8 +193,7 @@ open class StringToMethodCallParser<in T : Any> @JvmOverloads constructor(
|
||||
val parameterString = "{ $args }"
|
||||
val tree: JsonNode = om.readTree(parameterString) ?: throw UnparseableCallException(args)
|
||||
if (tree.size() > parameters.size) throw UnparseableCallException.TooManyParameters(methodNameHint, args)
|
||||
val inOrderParams: List<Any?> = parameters.mapIndexed { _, param ->
|
||||
val (argName, argType) = param
|
||||
val inOrderParams: List<Any?> = parameters.mapIndexed { _, (argName, argType) ->
|
||||
val entry = tree[argName] ?: throw UnparseableCallException.MissingParameter(methodNameHint, argName, args)
|
||||
try {
|
||||
om.readValue(entry.traverse(om), argType)
|
||||
|
@ -1,14 +1,16 @@
|
||||
package net.corda.jackson
|
||||
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Test
|
||||
import kotlin.reflect.full.primaryConstructor
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class StringToMethodCallParserTest {
|
||||
@Suppress("UNUSED")
|
||||
class Target {
|
||||
fun simple() = "simple"
|
||||
fun string(note: String) = note
|
||||
fun string(noteTextWord: String) = noteTextWord
|
||||
fun twoStrings(a: String, b: String) = a + b
|
||||
fun simpleObject(hash: SecureHash.SHA256) = hash.toString()
|
||||
fun complexObject(pair: Pair<Int, String>) = pair
|
||||
@ -20,7 +22,7 @@ class StringToMethodCallParserTest {
|
||||
val randomHash = "361170110f61086f77ff2c5b7ab36513705da1a3ebabf14dbe5cc9c982c45401"
|
||||
val tests = mapOf(
|
||||
"simple" to "simple",
|
||||
"string note: A test of barewords" to "A test of barewords",
|
||||
"string noteTextWord: A test of barewords" to "A test of barewords",
|
||||
"twoStrings a: Some words, b: ' and some words, like, Kirk, would, speak'" to "Some words and some words, like, Kirk, would, speak",
|
||||
"simpleObject hash: $randomHash" to randomHash.toUpperCase(),
|
||||
"complexObject pair: { first: 12, second: Word up brother }" to Pair(12, "Word up brother"),
|
||||
@ -36,4 +38,31 @@ class StringToMethodCallParserTest {
|
||||
assertEquals(output, parser.parse(target, input).invoke())
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNUSED")
|
||||
class ConstructorTarget(val someWord: String, val aDifferentThing: Int) {
|
||||
constructor(alternativeWord: String) : this(alternativeWord, 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ctor1() {
|
||||
val clazz = ConstructorTarget::class.java
|
||||
val parser = StringToMethodCallParser(clazz)
|
||||
val ctor = clazz.constructors.single { it.parameterCount == 2 }
|
||||
val names: List<String> = parser.paramNamesFromConstructor(ctor)
|
||||
assertEquals(listOf("someWord", "aDifferentThing"), names)
|
||||
val args: Array<Any?> = parser.parseArguments(clazz.name, names.zip(ctor.parameterTypes), "someWord: Blah blah blah, aDifferentThing: 12")
|
||||
assertArrayEquals(args, arrayOf<Any?>("Blah blah blah", 12))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ctor2() {
|
||||
val clazz = ConstructorTarget::class.java
|
||||
val parser = StringToMethodCallParser(clazz)
|
||||
val ctor = clazz.constructors.single { it.parameterCount == 1 }
|
||||
val names: List<String> = parser.paramNamesFromConstructor(ctor)
|
||||
assertEquals(listOf("alternativeWord"), names)
|
||||
val args: Array<Any?> = parser.parseArguments(clazz.name, names.zip(ctor.parameterTypes), "alternativeWord: Foo bar!")
|
||||
assertArrayEquals(args, arrayOf<Any?>("Foo bar!"))
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user