Skip to content

Commit

Permalink
. t find method's line numbers using java parser
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart authored and isidore committed Jan 22, 2024
1 parent 4ec518b commit da0d8f0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.23.1</version>
</dependency>
</dependencies>

<build>
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,12 @@ public static String padLeft(String contents, int targetLength)
}
return contents;
}
public static String removeFromEnd(String contents, String text)
{
return removeFromEnd(contents, text.length());
}
public static String removeFromEnd(String contents, int length)
{
return contents.substring(0, contents.length() - length);
}
}
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)
{
}
}

0 comments on commit da0d8f0

Please sign in to comment.