core, contracts: Add Generator<>.generateList to reduce boilerplate

This commit is contained in:
Andras Slemmer 2016-08-31 13:46:53 +01:00
parent 6faf19cccc
commit f11a587382
2 changed files with 11 additions and 13 deletions

View File

@ -29,7 +29,6 @@ class ContractStateGenerator : Generator<ContractState>(ContractState::class.jav
class MoveGenerator : Generator<Cash.Commands.Move>(Cash.Commands.Move::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): Cash.Commands.Move {
// TODO generate null as well
return Cash.Commands.Move(SecureHashGenerator().generate(random, status))
}
}
@ -63,19 +62,11 @@ class CommandGenerator : Generator<Command>(Command::class.java) {
class WiredTransactionGenerator: Generator<WireTransaction>(WireTransaction::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): WireTransaction {
val inputsGenerator = ArrayListGenerator()
inputsGenerator.addComponentGenerators(listOf(StateRefGenerator()))
val attachmentsGenerator = ArrayListGenerator()
attachmentsGenerator.addComponentGenerators(listOf(SecureHashGenerator()))
val outputsGenerator = ArrayListGenerator()
outputsGenerator.addComponentGenerators(listOf(TransactionStateGenerator(ContractStateGenerator())))
val commandsGenerator = ArrayListGenerator()
commandsGenerator.addComponentGenerators(listOf(CommandGenerator()))
val commands = commandsGenerator.generate(random, status) as ArrayList<Command> + listOf(CommandGenerator().generate(random, status))
val commands = CommandGenerator().generateList(random, status) + listOf(CommandGenerator().generate(random, status))
return WireTransaction(
inputs = inputsGenerator.generate(random, status) as ArrayList<StateRef>,
attachments = attachmentsGenerator.generate(random, status) as ArrayList<SecureHash>,
outputs = outputsGenerator.generate(random, status) as ArrayList<TransactionState<ContractState>>,
inputs = StateRefGenerator().generateList(random, status),
attachments = SecureHashGenerator().generateList(random, status),
outputs = TransactionStateGenerator(ContractStateGenerator()).generateList(random, status),
commands = commands,
notary = PartyGenerator().generate(random, status),
signers = commands.flatMap { it.signers },

View File

@ -25,6 +25,13 @@ import java.util.*
* TODO Split this into several files
*/
fun <A> Generator<A>.generateList(random: SourceOfRandomness, status: GenerationStatus): List<A> {
val arrayGenerator = ArrayListGenerator()
arrayGenerator.addComponentGenerators(listOf(this))
@Suppress("UNCHECKED_CAST")
return arrayGenerator.generate(random, status) as List<A>
}
class PrivateKeyGenerator: Generator<EdDSAPrivateKey>(EdDSAPrivateKey::class.java) {
override fun generate(random: SourceOfRandomness, status: GenerationStatus): EdDSAPrivateKey {
val seed = random.nextBytes(32)