Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ SOFTWARE.
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.7.6</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.lombrozo.jsmith.commandline;

import com.github.lombrozo.jsmith.RandomJavaClass;
import picocli.CommandLine;

/**
* @todo #18: Add more console commands and also write tests for them.
*/

@CommandLine.Command(name = "randomjavacode", description = "Generates a random Java code", mixinStandardHelpOptions = true)
public class RandomJavaCodeCommand implements Runnable{

@CommandLine.Option(names = {"-s", "--seed"}, description = "seed for random generation", defaultValue = "-1")
long seed;

@Override
public void run() {
RandomJavaClass clazz;
if (seed == -1) {
clazz = new RandomJavaClass();
} else {
clazz = new RandomJavaClass(seed);
}
System.out.print(clazz.src());
}
}
52 changes: 52 additions & 0 deletions src/test/java/com/github/lombrozo/jsmith/it/ConsoleCommandIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.lombrozo.jsmith.it;

import com.github.lombrozo.jsmith.commandline.RandomJavaCodeCommand;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.RepeatedTest;
import picocli.CommandLine;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Random;
import java.util.logging.Logger;

/**
* This test checks how CLI works
**/
public class ConsoleCommandIT {
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private static final Logger logger = Logger.getLogger(ConsoleCommandIT.class.getName());

@RepeatedTest(10)
public void generatesJavaCode() {
CommandLine cmd = new CommandLine(new RandomJavaCodeCommand());
System.setOut(new PrintStream(outputStream));
String randomExecutionArgs = getRandomExecuteArgs();
int exitCode;
if (randomExecutionArgs != null) {
exitCode = cmd.execute(randomExecutionArgs);
} else {
exitCode = cmd.execute();
}
System.setOut(originalOut);
String executionResult = outputStream.toString();

Assertions.assertEquals(0, exitCode, "We expect the command to complete correctly.");
Assertions.assertFalse(executionResult.isEmpty(), "We expect the output to be non-empty.");
logger.info("Generated code: " + "\n-----\n" + executionResult + "\n-----\n");
}

private String getRandomExecuteArgs() {
Random random = new Random();
boolean needArgs = random.nextBoolean();
long seed;
if (needArgs) {
seed = random.nextLong();
logger.info("Random seed: " + seed);
return "-s=" + seed;
} else {
return null;
}
}
}