CommandLineInterfaceTest.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package de.jplag.cli;
  2. import static de.jplag.CommandLineArgument.ROOT_DIRECTORY;
  3. import static java.util.stream.Collectors.toSet;
  4. import java.util.Arrays;
  5. import net.sourceforge.argparse4j.inf.Namespace;
  6. import de.jplag.CLI;
  7. import de.jplag.CommandLineArgument;
  8. import de.jplag.options.JPlagOptions;
  9. /**
  10. * Test base for tests regarding the {@link CLI} and any {@link CommandLineArgument}. Solely tests if the arguments set
  11. * via the command line interface are propagated correctly into options. JPlag is not executed for the different command
  12. * line arguments, thus these tests do not test the functionality of the options during the comparison.
  13. * @author Timur Saglam
  14. */
  15. public abstract class CommandLineInterfaceTest {
  16. protected static final String CURRENT_DIRECTORY = ".";
  17. protected static final double DELTA = 1E-5;
  18. protected CLI cli;
  19. protected Namespace namespace;
  20. protected JPlagOptions options;
  21. /**
  22. * Creates a string for all arguments and their values that have been succesfully parsed.
  23. */
  24. private String parsedKeys(String... arguments) {
  25. var keys = namespace.getAttrs().keySet().stream()
  26. .filter(key -> key.equals(ROOT_DIRECTORY.flag()) || Arrays.stream(arguments).anyMatch(arg -> arg.contains("-" + key)));
  27. return keys.map(it -> it.toString() + "=" + namespace.get(it)).collect(toSet()).toString();
  28. }
  29. /**
  30. * Builds a CLI string for a CLI argument and a value.
  31. * @param argument is the CLI argument.
  32. * @param value is the value for that argument.
  33. * @return <code>"flag value"</code>
  34. */
  35. protected String buildArgument(CommandLineArgument argument, String value) {
  36. return argument.flag() + "=" + value;
  37. }
  38. /**
  39. * Builds {@link JPlagOptions} via the command line interface. Sets {@link CommandLineInterfaceTest#cli},
  40. * {@link CommandLineInterfaceTest#namespace}, and {@link CommandLineInterfaceTest#options}.
  41. * @param arguments are the command line interface arguments.
  42. */
  43. protected void buildOptionsFromCLI(String... arguments) {
  44. cli = new CLI();
  45. namespace = cli.parseArguments(arguments);
  46. System.out.println("Parsed arguments: " + parsedKeys(arguments));
  47. options = cli.buildOptionsFromArguments(namespace);
  48. }
  49. }