diff --git a/approvaltests-tests/pom.xml b/approvaltests-tests/pom.xml index c61d11f9..6d9d36f9 100644 --- a/approvaltests-tests/pom.xml +++ b/approvaltests-tests/pom.xml @@ -130,6 +130,11 @@ jakarta.mail 2.0.1 + + com.github.javaparser + javaparser-core + 3.23.1 + diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java b/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java new file mode 100644 index 00000000..b57bcf09 --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java @@ -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); + } +} diff --git a/approvaltests-util/src/main/java/com/spun/util/StringUtils.java b/approvaltests-util/src/main/java/com/spun/util/StringUtils.java index 77d0f681..9c2305f2 100644 --- a/approvaltests-util/src/main/java/com/spun/util/StringUtils.java +++ b/approvaltests-util/src/main/java/com/spun/util/StringUtils.java @@ -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); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/XmlApprovals.java b/approvaltests/src/main/java/org/approvaltests/XmlApprovals.java new file mode 100644 index 00000000..f9688af0 --- /dev/null +++ b/approvaltests/src/main/java/org/approvaltests/XmlApprovals.java @@ -0,0 +1,8 @@ +package org.approvaltests; + +public class XmlApprovals +{ + public static void verify(String xml) + { + } +}