-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
. t find method's line numbers using java parser
- Loading branch information
1 parent
4ec518b
commit da0d8f0
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.approvaltests; | ||
|
||
import com.github.javaparser.Range; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.utils.SourceRoot; | ||
import com.spun.util.StringUtils; | ||
import org.approvaltests.core.Options; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.nio.file.Paths; | ||
|
||
public class ParsingFilesTest | ||
{ | ||
@Test | ||
public void getLineNumberOfThisMethod() throws Exception | ||
{ | ||
var expected = """ | ||
(line 5,col 3)-(line 7,col 3) | ||
"""; | ||
Method method = XmlApprovals.class.getMethod("verify", String.class); | ||
Range r = getMethodLines(method); | ||
Approvals.verify(r, new Options().inline(expected)); | ||
} | ||
private Range getMethodLines(Method method) | ||
{ | ||
CompilationUnit compilationUnit = parseJavaFile(method); | ||
return compilationUnit.getClassByName("XmlApprovals").get().getMember(0).getRange().get(); | ||
} | ||
private CompilationUnit parseJavaFile(Method method) | ||
{ | ||
File filePath = getSourceFile(method); | ||
String baseDir = StringUtils.removeFromEnd(System.getProperty("user.dir"), "-tests") + "/src/main/java/"; | ||
SourceRoot sourceRoot = new SourceRoot(Paths.get(baseDir)); | ||
String packageName = method.getDeclaringClass().getPackageName(); | ||
String name = filePath.getName(); | ||
return sourceRoot.parse(packageName, name); | ||
} | ||
private File getSourceFile(Method method) | ||
{ | ||
String baseDir = StringUtils.removeFromEnd(System.getProperty("user.dir"), "-tests"); | ||
String className = method.getDeclaringClass().getName(); | ||
String relativePath = "src/main/java/" + className.replace('.', '/') + ".java"; | ||
return new File(baseDir, relativePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
approvaltests/src/main/java/org/approvaltests/XmlApprovals.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.approvaltests; | ||
|
||
public class XmlApprovals | ||
{ | ||
public static void verify(String xml) | ||
{ | ||
} | ||
} |