Skip to content

Commit

Permalink
feat(#10): add broader test info
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jan 15, 2023
1 parent c83ba24 commit f0fe681
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class UserTest {
}
```

The next names will be considered as invalid:
The next cases will be considered as invalid:

```java
public class UserTest {
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
<version>3.24.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/github/lombrozo/testnames/Cases.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.lombrozo.testnames;

import java.util.Arrays;
import java.util.Collection;

@FunctionalInterface
public interface Cases {

Collection<TestCase> all();

static Cases correct() {
return () -> Arrays.asList(
new TestCase.FakeCase("removes"),
new TestCase.FakeCase("creates")
);
}

static Cases wrong(){
return () -> Arrays.asList(
new TestCase.FakeCase("remove"),
new TestCase.FakeCase("test")
);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/github/lombrozo/testnames/JavaParserCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.lombrozo.testnames;

import java.nio.file.Path;
import lombok.Data;

@Data
class JavaParserCase implements TestCase {

private final String className;
private final String name;
private final Path path;

JavaParserCase(final String className, final String name, final Path path) {
this.className = className;
this.name = name;
this.path = path;
}

@Override
public String className() {
return this.className;
}

@Override
public String name() {
return this.name;
}

@Override
public Path path() {
return this.path;
}
}
13 changes: 8 additions & 5 deletions src/main/java/com/github/lombrozo/testnames/JavaTestCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@
import java.util.Collection;
import java.util.List;

public final class JavaTestCode implements Names {
public final class JavaTestCode implements Cases {
private final Path path;

public JavaTestCode(final Path path) {
this.path = path;
}

@Override
public Collection<String> names() {
public Collection<TestCase> all() {
try {
final CompilationUnit parse = StaticJavaParser.parse(this.path);
final List<Node> childNodes = parse.getChildNodes();
final List<String> names = new ArrayList<>();
final List<TestCase> names = new ArrayList<>();
for (final Node childNode : childNodes) {
if (childNode instanceof ClassOrInterfaceDeclaration) {
final ClassOrInterfaceDeclaration clazz = (ClassOrInterfaceDeclaration) childNode;
clazz.getMethods().forEach(m -> {
if (!m.isPrivate() && (
m.isAnnotationPresent("Test")
|| m.isAnnotationPresent("ParameterizedTest")))
names.add(m.getNameAsString());
|| m.isAnnotationPresent("ParameterizedTest"))) {

final String name = m.getNameAsString();
names.add(new JavaParserCase(clazz.getNameAsString(), name, this.path));
}
});
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/github/lombrozo/testnames/Names.java

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/java/com/github/lombrozo/testnames/RuleForAllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

final class RuleForAllTests implements Rule {

private final Names tests;
private final Cases tests;

RuleForAllTests(final Names test) {
RuleForAllTests(final Cases test) {
this.tests = test;
}

@Override
public void validate() throws WrongTestName {
final Collection<String> names = this.tests.names();
for (final String test : names) {
new PresentSimpleRule(test).validate();
final Collection<TestCase> names = this.tests.all();
for (final TestCase test : names) {
new PresentSimpleRule(test.name()).validate();
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/github/lombrozo/testnames/TestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.lombrozo.testnames;

import java.nio.file.Path;
import java.nio.file.Paths;

public interface TestCase {

String className();

String name();

Path path();


class FakeCase implements TestCase {

private final String className;
private final String name;
private final Path path;

FakeCase(final String name) {
this("FakeClass", name, Paths.get("."));
}

FakeCase(final String className, final String name, final Path path) {
this.className = className;
this.name = name;
this.path = path;
}

@Override
public String className() {
return className;
}

@Override
public String name() {
return name;
}

@Override
public Path path() {
return path;
}
}
}
38 changes: 30 additions & 8 deletions src/test/java/com/github/lombrozo/testnames/JavaTestCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@ void getsNamesFromSimpleTest(@TempDir final Path temp) throws Exception {
final String java = "TestSimple.java";
final Path path = temp.resolve(java);
Files.write(path, new BytesOf(new ResourceOf(java)).asBytes());
final Names names = new JavaTestCode(path);
final Cases cases = new JavaTestCode(path);
MatcherAssert.assertThat(
names.names(),
Matchers.containsInAnyOrder("creates", "removes", "updates")
cases.all(),
Matchers.containsInAnyOrder(
new JavaParserCase(
"TestSimple",
"creates",
path
), new JavaParserCase(
"TestSimple",
"removes",
path
),
new JavaParserCase(
"TestSimple",
"updates",
path
)
)
);
}

Expand All @@ -29,18 +44,24 @@ void getsNamesFromParameterizedTest(@TempDir final Path temp) throws Exception {
final String java = "TestParameterized.java";
final Path path = temp.resolve(java);
Files.write(path, new BytesOf(new ResourceOf(java)).asBytes());
final Names names = new JavaTestCode(path);
final Cases cases = new JavaTestCode(path);
MatcherAssert.assertThat(
names.names(),
Matchers.containsInAnyOrder("checksCases")
cases.all(),
Matchers.containsInAnyOrder(
new JavaParserCase(
"TestParameterized",
"checksCases",
path
)
)
);
}

@Test
void throwsExceptionIfFileNotFound(@TempDir final Path temp) throws Exception {
Assertions.assertThrows(
IllegalStateException.class,
() -> new JavaTestCode(temp.resolve("TestNotFound.java")).names()
() -> new JavaTestCode(temp.resolve("TestNotFound.java")).all()
);
}

Expand All @@ -50,8 +71,9 @@ void throwsExceptionIfFileIsNotJava(@TempDir final Path temp) throws Exception {
Files.write(path, "Not Java".getBytes());
Assertions.assertThrows(
IllegalStateException.class,
() -> new JavaTestCode(path).names()
() -> new JavaTestCode(path).all()
);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RuleForAllTestsTest {
@Test
void validatesAllWithoutExceptions() {
try {
new RuleForAllTests(() -> Arrays.asList("removes", "creates")).validate();
new RuleForAllTests(Cases.correct()).validate();
} catch (final WrongTestName ex) {
Assertions.fail(ex);
}
Expand All @@ -19,7 +19,7 @@ void validatesAllWithoutExceptions() {
void validatesAllWithExceptions() {
Assertions.assertThrows(
WrongTestName.class,
() -> new RuleForAllTests(() -> Arrays.asList("creates", "test")).validate()
() -> new RuleForAllTests(Cases.wrong()).validate()
);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/TestSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.jupiter.api.Test;

class TestExample {
class TestSimple {
@Test
void creates() {
}
Expand Down

0 comments on commit f0fe681

Please sign in to comment.