All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jio.test.pbt.PropertyCommand Maven / Gradle / Ivy

Go to download

JIO test library based on Property Based Testing and Java Flight Recording Debuggers

There is a newer version: 3.0.0-RC2
Show newest version
package jio.test.pbt;

import jio.IO;
import jio.console.Command;
import jio.console.State;
import jsonvalues.JsObj;

import java.util.Arrays;
import java.util.function.Function;
import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;

/**
 * Command to execute {@link Property properties} with the command:
 * 
 *     prop name
 * 
*

* Properties can also be executed an arbitrary number of time either in parallel or sequentially: * *

 *     prop name par 3
 *     prop name seq 5
 * 
*/ class PropertyCommand extends Command { static final Pattern parPattern = Pattern.compile("prop \\w+ par \\d+"); static final Pattern seqPattern = Pattern.compile("prop \\w+ seq \\d+"); private static final String PREFIX_COMMAND = "prop"; private final Property prop; /** * Creates a PropertyCommand from a property. * * @param prop The property to execute. */ private PropertyCommand(final Property prop) { super(String.format("%s %s", PREFIX_COMMAND, requireNonNull(prop).name ), prop.description, tokens -> tokens[0].equalsIgnoreCase(PREFIX_COMMAND) && tokens[1].equalsIgnoreCase(prop.name) ); this.prop = requireNonNull(prop); } /** * Creates a PropertyCommand from a property. * * @param prop The property to execute. */ public static PropertyCommand of(final Property prop) { return new PropertyCommand(prop); } @Override public Function> apply(final JsObj conf, final State state ) { return tokens -> { String command = String.join(" ", Arrays.stream(tokens) .toList()); if (parPattern.matcher(command) .matches()) { int n = Integer.parseInt(tokens[3]); return IO.succeed(prop.repeatPar(n) .createTask(conf) .join() .toString()); } if (seqPattern.matcher(command) .matches()) { int n = Integer.parseInt(tokens[3]); return IO.succeed(prop.repeatPar(n) .createTask(conf) .join() .toString()); } return prop.createTask(conf) .map(Report::toString); }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy