Skip to content

Commit dd6a4aa

Browse files
committed
add tests
1 parent 1818ffb commit dd6a4aa

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.jbock.processor;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import javax.tools.JavaFileObject;
6+
7+
import static io.jbock.common.truth.Truth.assertAbout;
8+
import static io.jbock.testing.compile.JavaSourcesSubjectFactory.javaSources;
9+
import static java.util.Collections.singletonList;
10+
import static net.jbock.processor.Processor.fromSource;
11+
12+
class SuperCommandTest {
13+
14+
@Test
15+
void cannotCombineCommandAndSuperCommand() {
16+
JavaFileObject javaFile = fromSource(
17+
"@Command",
18+
"@SuperCommand",
19+
"interface Arguments {",
20+
"",
21+
" @Parameter(index = 0)",
22+
" String a();",
23+
"}");
24+
assertAbout(javaSources()).that(singletonList(javaFile))
25+
.processedWith(Processor.testInstance())
26+
.failsToCompile()
27+
.withErrorContaining("not both");
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.jbock.examples;
2+
3+
import net.jbock.Command;
4+
import net.jbock.Option;
5+
import net.jbock.Parameter;
6+
import net.jbock.VarargsParameter;
7+
8+
import java.util.List;
9+
10+
@Command
11+
interface EscapeSequenceCommand {
12+
13+
@Parameter(index = 0)
14+
String command();
15+
16+
@Option(names = "--bare")
17+
boolean bare();
18+
19+
@VarargsParameter
20+
List<String> remainingArgs();
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package net.jbock.examples;
2+
3+
import net.jbock.examples.fixture.ParserTestFixture;
4+
import org.junit.jupiter.api.RepeatedTest;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.List;
8+
import java.util.concurrent.ThreadLocalRandom;
9+
10+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class EscapeSequenceCommandTest {
14+
15+
private final EscapeSequenceCommandParser parser = new EscapeSequenceCommandParser();
16+
17+
private final ParserTestFixture<EscapeSequenceCommand> f =
18+
ParserTestFixture.create(parser::parse);
19+
20+
@RepeatedTest(10)
21+
void randomizedTest() {
22+
String[] randomStrings = randomWords();
23+
String[] args = new String[randomStrings.length + 2];
24+
args[0] = "firstToken";
25+
args[1] = "--"; // escape sequence
26+
System.arraycopy(randomStrings, 0, args, 2, randomStrings.length);
27+
EscapeSequenceCommand result = f.parse(args);
28+
String[] remainingArgs = result.remainingArgs().toArray(new String[0]);
29+
30+
// escape sequence works as intended
31+
assertArrayEquals(randomStrings, remainingArgs);
32+
}
33+
34+
@Test
35+
void testDoubleEscape() {
36+
String[] args = {"add", "--", "--", "a"};
37+
EscapeSequenceCommand result = f.parse(args);
38+
assertEquals(List.of("--", "a"), result.remainingArgs());
39+
}
40+
41+
private String[] randomWords() {
42+
int n = ThreadLocalRandom.current().nextInt(5);
43+
String[] result = new String[n];
44+
for (int i = 0; i < n; i++) {
45+
result[i] = randomWord();
46+
}
47+
return result;
48+
}
49+
50+
private String randomWord() {
51+
StringBuilder sb = new StringBuilder();
52+
for (int i = 0; i < ThreadLocalRandom.current().nextInt(0, 3); i++) {
53+
int randomInt = ThreadLocalRandom.current().nextInt(10);
54+
if (randomInt < 5) {
55+
sb.append("-");
56+
} else if (randomInt < 9) {
57+
sb.append("a");
58+
} else {
59+
return "--help";
60+
}
61+
}
62+
return sb.toString();
63+
}
64+
}

0 commit comments

Comments
 (0)