Skip to content

Commit 47f7ebe

Browse files
authored
feat: jdoc-cucumber (#9)
1 parent 701a6b4 commit 47f7ebe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+932
-242
lines changed

buildSrc/src/main/groovy/java-conventions.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ spotbugs {
6969

7070
repositories {
7171
mavenCentral()
72+
maven { url "https://jitpack.io" }
7273
}
7374

7475
configurations {
@@ -85,5 +86,6 @@ dependencies {
8586

8687
testImplementation "org.assertj:assertj-core"
8788
testImplementation "org.junit.jupiter:junit-jupiter"
89+
testImplementation "org.junit.platform:junit-platform-suite"
8890
testImplementation "org.mockito:mockito-junit-jupiter"
8991
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id "java-library-conventions"
3+
}
4+
5+
tasks.withType(JavaCompile) {
6+
options.compilerArgs << "-parameters"
7+
}
8+
9+
afterEvaluate {
10+
test {
11+
systemProperties = [
12+
"jdoc.spock.test-dirs" : sourceSets.main.java.srcDirs.join(","),
13+
"jdoc.spock.classpath" : configurations.testCompileClasspath.files.join(",")
14+
]
15+
}
16+
}

gradle/config/checkstyle/checkstyle.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@
159159
<!--<module name="JavadocMethod"/>-->
160160
<!--<module name="JavadocPackage"/>-->
161161
<!--<module name="JavadocParagraph"/>-->
162-
<module name="JavadocStyle"/>
162+
<module name="JavadocStyle">
163+
<property name="severity" value="warning"/>
164+
</module>
163165
<!--<module name="JavadocTagContinuationIndentation"/>-->
164166
<!--<module name="JavadocType"/>-->
165167
<!--<module name="JavadocVariable"/>-->

jdoc-core/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id "java-library-jdoc-spock-conventions"
3+
}
4+
5+
dependencies {
6+
api "org.junit.platform:junit-platform-engine"
7+
api "com.github.javaparser:javaparser-core"
8+
9+
implementation "org.apache.commons:commons-lang3"
10+
implementation "org.jsoup:jsoup"
11+
12+
testRuntimeOnly "com.github.boolivar:jdoc-test"
13+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.bool.jdoc.core;
2+
3+
import lombok.AllArgsConstructor;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.junit.platform.engine.ConfigurationParameters;
6+
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
@AllArgsConstructor
13+
public abstract class ConfigParam<T> {
14+
15+
protected final String key;
16+
17+
public String getKey() {
18+
return key;
19+
}
20+
21+
public abstract Optional<T> maybeGet(ConfigurationParameters parameters);
22+
23+
public static class StringConfigParam extends ConfigParam<String> {
24+
25+
public StringConfigParam(String key) {
26+
super(key);
27+
}
28+
29+
@Override
30+
public Optional<String> maybeGet(ConfigurationParameters config) {
31+
return config.get(key);
32+
}
33+
}
34+
35+
public static class StringListConfigParam extends ConfigParam<List<String>> {
36+
37+
public StringListConfigParam(String key) {
38+
super(key);
39+
}
40+
41+
public List<String> get(ConfigurationParameters config) {
42+
return maybeGet(config).orElse(Collections.emptyList());
43+
}
44+
45+
@Override
46+
public Optional<List<String>> maybeGet(ConfigurationParameters config) {
47+
return config.get(key).map(value -> Arrays.asList(StringUtils.split(value, ',')));
48+
}
49+
}
50+
}

jdoc-spock/src/main/java/org/bool/jdoc/spock/DiscoveryRequest.java renamed to jdoc-core/src/main/java/org/bool/jdoc/core/DiscoveryRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.bool.jdoc.spock;
1+
package org.bool.jdoc.core;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Builder;
@@ -25,7 +25,7 @@ public class DiscoveryRequest implements EngineDiscoveryRequest {
2525

2626
@NonNull
2727
@Builder.Default
28-
private final List<DiscoveryFilter<?>> filters = Collections.emptyList();
28+
private final List<? super DiscoveryFilter<?>> filters = Collections.emptyList();
2929

3030
@NonNull
3131
@Builder.Default

jdoc-spock/src/main/java/org/bool/jdoc/spock/JavaFileParser.java renamed to jdoc-core/src/main/java/org/bool/jdoc/core/JavaFileParser.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.bool.jdoc.spock;
1+
package org.bool.jdoc.core;
22

3-
import org.bool.jdoc.spock.exception.SpockEngineException;
3+
import org.bool.jdoc.core.exception.JdocException;
44

55
import com.github.javaparser.JavaParser;
66
import com.github.javaparser.ParseResult;
@@ -22,10 +22,10 @@ public class JavaFileParser {
2222

2323
private final JavaParser javaParser;
2424

25-
private final CodeBlockParser codeBlockParser;
25+
private final JdocParser jdocParser;
2626

27-
public JavaFileParser() {
28-
this(new JavaParser(), new CodeBlockParser());
27+
public JavaFileParser(String lang) {
28+
this(new JavaParser(), new JdocParser(lang));
2929
}
3030

3131
/**
@@ -34,13 +34,13 @@ public JavaFileParser() {
3434
* <pre><code lang="spock">
3535
* def "Parsing result has code from <code></code> blocks"() {
3636
* given:
37-
* def parser = new JavaFileParser()
37+
* def parser = new JavaFileParser("spock")
3838
* when:
39-
* def result = parser.parse(java.nio.file.Paths.get("src/main/java/org/bool/jdoc/spock/JavaFileParser.java"))
39+
* def result = parser.parse(java.nio.file.Paths.get("src/main/java/org/bool/jdoc/core/JavaFileParser.java"))
4040
* then:
4141
* result.codeBlocks.size() == 1
4242
* result.codeBlocks[0].startsWith('\n def "Parsing result has code from <code></code> blocks"() {')
43-
* result.codeBlocks[0].contains('parser.parse(java.nio.file.Paths.get("src/main/java/org/bool/jdoc/spock/JavaFileParser.java"))')
43+
* result.codeBlocks[0].contains('parser.parse(java.nio.file.Paths.get("src/main/java/org/bool/jdoc/core/JavaFileParser.java"))')
4444
* result.codeBlocks[0].contains('infinite recursion ]8D')
4545
* }
4646
* </code></pre>
@@ -49,7 +49,7 @@ public JavaFileParser() {
4949
public SpecSource parse(Path file) {
5050
ParseResult<CompilationUnit> result = javaParser.parse(file);
5151
if (!result.isSuccessful()) {
52-
throw new SpockEngineException("Error parse file " + file + ": " + result.getProblems());
52+
throw new JdocException("Error parse file " + file + ": " + result.getProblems());
5353
}
5454
return new SpecSource(result.getResult().get(),
5555
result.getCommentsCollection().map(this::parseComments).orElse(Collections.emptyList()));
@@ -58,7 +58,7 @@ public SpecSource parse(Path file) {
5858
private List<String> parseComments(CommentsCollection comments) {
5959
return comments.getComments().stream()
6060
.filter(comment -> JavadocComment.class.isInstance(comment) || BlockComment.class.isInstance(comment))
61-
.flatMap(comment -> codeBlockParser.parse(comment.getContent()).stream())
61+
.flatMap(comment -> jdocParser.parse(comment.getContent()).stream())
6262
.filter(StringUtils::isNotBlank)
6363
.collect(Collectors.toList());
6464
}

jdoc-spock/src/main/java/org/bool/jdoc/spock/CodeBlockParser.java renamed to jdoc-core/src/main/java/org/bool/jdoc/core/JdocParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.bool.jdoc.spock;
1+
package org.bool.jdoc.core;
22

33
import lombok.AllArgsConstructor;
44
import lombok.SneakyThrows;
@@ -16,17 +16,17 @@
1616
import java.util.stream.Stream;
1717

1818
@AllArgsConstructor
19-
public class CodeBlockParser {
19+
public class JdocParser {
2020

2121
private final Evaluator codeTag;
2222

2323
private final Function<String, Document> parser;
2424

25-
public CodeBlockParser() {
26-
this(and(new Evaluator.Tag("code"), new Evaluator.AttributeWithValue("lang", "spock")));
25+
public JdocParser(String lang) {
26+
this(and(new Evaluator.Tag("code"), new Evaluator.AttributeWithValue("lang", lang)));
2727
}
2828

29-
public CodeBlockParser(Evaluator codeTag) {
29+
public JdocParser(Evaluator codeTag) {
3030
this(codeTag, content -> Parser.xmlParser().parseInput(content, ""));
3131
}
3232

@@ -36,7 +36,7 @@ public CodeBlockParser(Evaluator codeTag) {
3636
* <pre><code lang="spock">
3737
* def "parse text and remove asterisks"() {
3838
* given:
39-
* def parser = new CodeBlockParser()
39+
* def parser = new JdocParser("spock")
4040
* when:
4141
* def result = parser.parse('<code lang="spock"> * some code</code>')
4242
* then:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.bool.jdoc.core;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.SneakyThrows;
5+
import org.junit.platform.engine.DiscoverySelector;
6+
import org.junit.platform.engine.discovery.DirectorySelector;
7+
import org.junit.platform.engine.discovery.FileSelector;
8+
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.List;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
14+
15+
@AllArgsConstructor
16+
public class JdocSpecReader {
17+
18+
private final JavaFileParser parser;
19+
20+
/**
21+
* Collect the specs from selected java files.
22+
*
23+
* <pre><code lang="spock">
24+
* def "Parse file specified by FileSelector"() {
25+
* given:
26+
* def spec = new SpecSource()
27+
* parser.parse(java.nio.file.Paths.get("File.java")) >> spec
28+
* when:
29+
* def specs = $target.readSpecs([org.junit.platform.engine.discovery.DiscoverySelectors.selectFile("File.java")])
30+
* then:
31+
* specs == [spec]
32+
* }
33+
* </code></pre>
34+
*/
35+
public JdocSpecReader(String lang) {
36+
this(new JavaFileParser(lang));
37+
}
38+
39+
public List<SpecSource> readSpecs(List<DiscoverySelector> selectors) {
40+
return selectors.stream()
41+
.flatMap(this::streamFiles).distinct()
42+
.map(parser::parse).collect(Collectors.toList());
43+
}
44+
45+
@SneakyThrows
46+
private Stream<Path> streamFiles(DiscoverySelector selector) {
47+
if (selector instanceof DirectorySelector) {
48+
return Files.walk(((DirectorySelector) selector).getPath())
49+
.filter(path -> path.getFileName().toString().endsWith(".java"));
50+
}
51+
if (selector instanceof FileSelector) {
52+
return Stream.of(((FileSelector) selector).getPath());
53+
}
54+
return Stream.empty();
55+
}
56+
}

jdoc-spock/src/main/java/org/bool/jdoc/spock/SpecSource.java renamed to jdoc-core/src/main/java/org/bool/jdoc/core/SpecSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.bool.jdoc.spock;
1+
package org.bool.jdoc.core;
22

33
import com.github.javaparser.ast.CompilationUnit;
44
import lombok.AllArgsConstructor;

0 commit comments

Comments
 (0)