Skip to content

Commit

Permalink
CargoArgs#fromAllArguments(String...) instead of String[]
Browse files Browse the repository at this point in the history
Simplify tests based on that.
Other cleanups to tests while at them.
  • Loading branch information
akurtakov committed Oct 28, 2021
1 parent c8f97f5 commit 43bc156
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2019 Fraunhofer FOKUS and others.
* Copyright (c) 2019, 2021 Fraunhofer FOKUS and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -21,64 +21,60 @@

import org.junit.jupiter.api.Test;

public class CargoArgsTest {
class CargoArgsTest {

private static final String SEPARATOR = "--";

@Test
public void testOnlyCommand() {
void testOnlyCommand() {
String command = "fmt";
String[] input = { command };
CargoArgs result = CargoArgs.fromAllArguments(input);
CargoArgs result = CargoArgs.fromAllArguments(command);
assertEquals(command, result.command);
assertTrue(result.arguments.isEmpty());
assertTrue(result.options.isEmpty());
}

@Test
public void testOnlyCommandAndSeparator() {
void testOnlyCommandAndSeparator() {
String command = "fmt";
String[] input = { command, SEPARATOR };
CargoArgs result = CargoArgs.fromAllArguments(input);
CargoArgs result = CargoArgs.fromAllArguments(command, SEPARATOR);
assertEquals(command, result.command);
assertTrue(result.arguments.isEmpty());
assertTrue(result.options.isEmpty());
}

@Test
public void testOnlyCommandAndOptions() {
void testOnlyCommandAndOptions() {
String command = "build";
String opt1 = "--all";
String opt2 = "--release";
String[] input = { command, opt1, opt2 };
CargoArgs result = CargoArgs.fromAllArguments(input);
CargoArgs result = CargoArgs.fromAllArguments(command, opt1, opt2);
assertEquals(command, result.command);
assertEquals(Arrays.asList(opt1, opt2), result.options);
assertTrue(result.arguments.isEmpty());
}

@Test
public void testOnlyCommandAndOptionsAndSeparator() {
void testOnlyCommandAndOptionsAndSeparator() {
String command = "build";
String opt1 = "--all";
String opt2 = "--release";
String[] input = { command, opt1, opt2, SEPARATOR };
CargoArgs result = CargoArgs.fromAllArguments(input);
CargoArgs result = CargoArgs.fromAllArguments(command, opt1, opt2, SEPARATOR);
assertEquals(command, result.command);
assertEquals(Arrays.asList(opt1, opt2), result.options);
assertTrue(result.arguments.isEmpty());
}

@Test
public void testOnlyCommandAndArguments() {
void testOnlyCommandAndArguments() {
String command = "build";
List<String> arguments = Arrays.asList("--nocapture", "my_test");
List<String> arguments = List.of("--nocapture", "my_test");

List<String> all_arguments = new ArrayList<>();
all_arguments.add(command);
all_arguments.add(SEPARATOR);
all_arguments.addAll(arguments);
String[] input = all_arguments.toArray(new String[] {});
String[] input = all_arguments.toArray(String[]::new);

CargoArgs result = CargoArgs.fromAllArguments(input);

Expand All @@ -88,17 +84,17 @@ public void testOnlyCommandAndArguments() {
}

@Test
public void testOnlyCommandAndOptionsAndArguments() {
void testOnlyCommandAndOptionsAndArguments() {
String command = "build";
List<String> options = Arrays.asList("--all", "--release");
List<String> arguments = Arrays.asList("--nocapture", "my_test");
List<String> options = List.of("--all", "--release");
List<String> arguments = List.of("--nocapture", "my_test");

List<String> all_arguments = new ArrayList<>();
all_arguments.add(command);
all_arguments.addAll(options);
all_arguments.add(SEPARATOR);
all_arguments.addAll(arguments);
String[] input = all_arguments.toArray(new String[] {});
String[] input = all_arguments.toArray(String[]::new);

CargoArgs result = CargoArgs.fromAllArguments(input);

Expand All @@ -108,17 +104,17 @@ public void testOnlyCommandAndOptionsAndArguments() {
}

@Test
public void testOnlyCommandAndOptionsAndArgumentsIncludingSeparator() {
void testOnlyCommandAndOptionsAndArgumentsIncludingSeparator() {
String command = "build";
List<String> options = Arrays.asList("--all", "--release");
List<String> arguments = Arrays.asList("--nocapture", SEPARATOR, "my_test");
List<String> options = List.of("--all", "--release");
List<String> arguments = List.of("--nocapture", SEPARATOR, "my_test");

List<String> all_arguments = new ArrayList<>();
all_arguments.add(command);
all_arguments.addAll(options);
all_arguments.add(SEPARATOR);
all_arguments.addAll(arguments);
String[] input = all_arguments.toArray(new String[] {});
String[] input = all_arguments.toArray(String[]::new);

CargoArgs result = CargoArgs.fromAllArguments(input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class RLSRunCommandTest {
class RLSRunCommandTest {

private static final String TITLE = "Run test";
private static final String COMMAND_ID = "rls.run";
Expand All @@ -53,21 +53,21 @@ static Stream<Arguments> parameters() {

@ParameterizedTest
@MethodSource("parameters")
public void testMapEntryArgument(Map<String, Object> argument) {
void testMapEntryArgument(Map<String, Object> argument) {
Command command = new Command(TITLE, COMMAND_ID, Arrays.asList(argument));
Optional<RLSRunCommand> lspCommand = RLSRunCommand.fromLSPCommand(command);
assertFalse(lspCommand.isPresent());
}

@Test
public void testNoArgument() {
void testNoArgument() {
Command command = new Command(TITLE, COMMAND_ID, Arrays.asList());
Optional<RLSRunCommand> lspCommand = RLSRunCommand.fromLSPCommand(command);
assertFalse(lspCommand.isPresent());
}

@Test
public void testArgumentListNull() {
void testArgumentListNull() {
Command command = new Command(TITLE, COMMAND_ID, null);
Optional<RLSRunCommand> lspCommand = RLSRunCommand.fromLSPCommand(command);
assertFalse(lspCommand.isPresent());
Expand All @@ -81,7 +81,7 @@ public void testArgumentListWrongEntry() {
}

@Test
public void testEmptyArguments() {
void testEmptyArguments() {
Command command = new Command(TITLE, COMMAND_ID,
Arrays.asList(createArgument(VALID_BINARY, EMPTY_ARGS, EMPTY_ENV)));
Optional<RLSRunCommand> lspCommand = RLSRunCommand.fromLSPCommand(command);
Expand All @@ -93,7 +93,7 @@ public void testEmptyArguments() {
}

@Test
public void testValidArguments() {
void testValidArguments() {
Command command = new Command(TITLE, COMMAND_ID,
Arrays.asList(createArgument(VALID_BINARY, VALID_ARGS, VALID_ENV)));
Optional<RLSRunCommand> lspCommand = RLSRunCommand.fromLSPCommand(command);
Expand All @@ -105,7 +105,7 @@ public void testValidArguments() {
}

@Test
public void testInvalidEnvEntry() {
void testInvalidEnvEntry() {
Map<String, String> env = new HashMap<>();
env.put("INVALID", null);
env.put("RUST_BACKTRACE", "short");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2019 Fraunhofer FOKUS and others.
* Copyright (c) 2019, 2021 Fraunhofer FOKUS and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -21,7 +21,7 @@
* "test" or "run"), the options (flags for the command) and arguments (to be
* passed to the executable called by cargo). Instances of this class can be
* created via the static factory method
* {@link CargoArgs#fromAllArguments(String[])}.
* {@link CargoArgs#fromAllArguments(String...)}.
*/
class CargoArgs {
public final List<String> options;
Expand Down Expand Up @@ -53,7 +53,7 @@ private CargoArgs(List<String> options, List<String> arguments, String command)
* @param args all arguments after a sub-command (e.g. )
* @return instance of {@code CargoArgs} holding command, options and arguments
*/
public static CargoArgs fromAllArguments(String[] args) {
public static CargoArgs fromAllArguments(String... args) {

String resultCommand = ""; //$NON-NLS-1$
List<String> resultOptions = Collections.emptyList();
Expand Down Expand Up @@ -84,5 +84,4 @@ public static CargoArgs fromAllArguments(String[] args) {
}
return new CargoArgs(resultOptions, resultArguments, resultCommand);
}
// foo
}

0 comments on commit 43bc156

Please sign in to comment.