From 97f5c783e92a585c8496f5039e22fbf48fe7fea9 Mon Sep 17 00:00:00 2001 From: Lars Eckart Date: Mon, 22 Jan 2024 21:08:44 +0200 Subject: [PATCH] . t determine line numbers via java-parser --- .../approvaltests/DocumentHelpersTest.java | 9 +- ...ersTest.listAllVerifyFunctions.approved.md | 114 +++++++++--------- .../org/approvaltests/ParsingFilesTest.java | 76 +++++++++++- 3 files changed, 132 insertions(+), 67 deletions(-) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.java b/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.java index a6391652..be0328d3 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.java @@ -1,5 +1,6 @@ package org.approvaltests; +import com.github.javaparser.Range; import com.spun.util.FormattedException; import com.spun.util.ObjectUtils; import com.spun.util.StringUtils; @@ -85,7 +86,7 @@ void testLineNumbers() getLink(Query.first(Approvals.class.getMethods(), m -> m.getName().equals("verifyAll") && m.getParameterTypes()[0].equals(Object[].class))); } - private String showParameters(Method m) + public static String showParameters(Method m) { return StringUtils.join(Query.select(m.getParameters(), p -> String.format("%s", p.getType().getSimpleName())), ","); @@ -94,9 +95,9 @@ public static String getLink(Method m) { String baseUrl = "https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java"; String file = m.getDeclaringClass().getName().replace('.', '/') + ".java"; - CtMethod methodX = getMethodX(m); - int start = getLineNumber(methodX); - int end = start + 3; + Range methodLines = ParsingFilesTest.getMethodLines(m); + int start = methodLines.begin.line; + int end = methodLines.end.line; return String.format("%s/%s#L%s-L%s", baseUrl, file, start, end); } private static CtMethod getMethodX(Method method) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md b/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md index b01fdf84..02021b83 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md +++ b/approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md @@ -1,59 +1,59 @@ - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L199-L202) (ApprovalApprover) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L182-L185) (ApprovalWriter) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L174-L177) (ApprovalWriter,ApprovalNamer) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L228-L231) (ExecutableCommand) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L158-L161) (File) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L236-L239) (Map) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L57-L60) (Object) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L244-L247) (ResultSet) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L252-L255) (SqlLoader) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L49-L52) (String) - * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L66-L69) (Verifiable) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L103-L106) (Object[],Function1) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L133-L136) (String,Iterable) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L120-L123) (String,Iterable,Function1) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L86-L89) (String,Object[]) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L94-L97) (String,Object[],Function1) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L141-L144) (String,String,Iterable) - * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L94-L97) (String,String,Object[]) - * Approvals. [verifyEachFileInDirectory ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L264-L267) (File) - * Approvals. [verifyEachFileInDirectory ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L272-L275) (File,FileFilter) - * Approvals. [verifyEachFileInDirectory ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L280-L283) (File,FilenameFilter) - * Approvals. [verifyException ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L385-L388) (Action0) - * Approvals. [verifyHtml ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L150-L153) (String) - * Approvals. [verifyXml ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L190-L193) (String) - * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L54-L57) (BufferedImage) - * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L62-L65) (Component) - * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L46-L49) (Image) - * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L70-L73) (Paintable) - * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L121-L124) (Paintable,int,Duration,Function1) - * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L111-L114) (Paintable,int,Function1) - * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L98-L101) (int,Duration,Function1) - * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L78-L81) (int,Function1) - * AwtApprovals. [verifySequenceWithTimings ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L83-L86) (int,Function1) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L36-L39) (Function1,Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L53-L56) (Function2,Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L71-L74) (Function3,Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L89-L92) (Function4,Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L108-L111) (Function5,Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L128-L131) (Function6,Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L148-L151) (Function7,Object[],Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L172-L175) (Function8,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L197-L200) (Function9,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L24-L27) (Object,String,Object[][]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L213-L216) (Function1,Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L225-L228) (Function2,Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L237-L240) (Function3,Object[],Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L249-L252) (Function4,Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L262-L265) (Function5,Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L276-L279) (Function6,Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L290-L293) (Function7,Object[],Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L308-L311) (Function8,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) - * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L326-L329) (Function9,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) - * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L38) (Object) - * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L39-L42) (Object,Function1) - * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L18-L21) (String) - * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L26-L29) (String,boolean) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L194-L197) (ApprovalApprover) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L177-L180) (ApprovalWriter) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L169-L172) (ApprovalWriter,ApprovalNamer) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L223-L226) (ExecutableCommand) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L153-L156) (File) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L231-L234) (Map) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L52-L55) (Object) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L239-L242) (ResultSet) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L247-L250) (SqlLoader) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L44-L47) (String) + * Approvals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L61-L64) (Verifiable) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L98-L101) (Object[],Function1) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L128-L131) (String,Iterable) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L115-L118) (String,Iterable,Function1) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L81-L84) (String,Object[]) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L107-L110) (String,Object[],Function1) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L136-L139) (String,String,Iterable) + * Approvals. [verifyAll ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L89-L92) (String,String,Object[]) + * Approvals. [verifyEachFileInDirectory ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L259-L262) (File) + * Approvals. [verifyEachFileInDirectory ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L267-L270) (File,FileFilter) + * Approvals. [verifyEachFileInDirectory ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L275-L278) (File,FilenameFilter) + * Approvals. [verifyException ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L380-L383) (Action0) + * Approvals. [verifyHtml ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L145-L148) (String) + * Approvals. [verifyXml ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/Approvals.java#L185-L188) (String) + * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L40-L43) (BufferedImage) + * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L48-L51) (Component) + * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L32-L35) (Image) + * AwtApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L56-L59) (Paintable) + * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L106-L110) (Paintable,int,Duration,Function1) + * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L96-L100) (Paintable,int,Function1) + * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L83-L87) (int,Duration,Function1) + * AwtApprovals. [verifySequence ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L64-L67) (int,Function1) + * AwtApprovals. [verifySequenceWithTimings ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/awt/AwtApprovals.java#L68-L72) (int,Function1) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L31-L35) (Function1,Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L47-L52) (Function2,Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L65-L70) (Function3,Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L83-L88) (Function4,Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L101-L107) (Function5,Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L121-L127) (Function6,Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L141-L147) (Function7,Object[],Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L164-L172) (Function8,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L189-L196) (Function9,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyAllCombinations ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L19-L22) (Object,String,Object[][]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L208-L212) (Function1,Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L219-L224) (Function2,Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L231-L236) (Function3,Object[],Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L243-L248) (Function4,Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L255-L261) (Function5,Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L269-L275) (Function6,Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L283-L290) (Function7,Object[],Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L300-L308) (Function8,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) + * CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L318-L325) (Function9,Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[],Object[]) + * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L31-L34) (Object) + * JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L38) (Object,Function1) + * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L14-L17) (String) + * JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L22-L25) (String,boolean) * JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L10-L13) (Object) * JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L14-L17) (Object) - * VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L19) (ContextAware) + * VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L12-L15) (ContextAware) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java b/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java index b57bcf09..24e54a30 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/ParsingFilesTest.java @@ -2,7 +2,11 @@ import com.github.javaparser.Range; import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; import com.github.javaparser.utils.SourceRoot; +import com.spun.util.FormattedException; import com.spun.util.StringUtils; import org.approvaltests.core.Options; import org.junit.jupiter.api.Test; @@ -10,25 +14,46 @@ import java.io.File; import java.lang.reflect.Method; import java.nio.file.Paths; +import java.util.List; public class ParsingFilesTest { @Test - public void getLineNumberOfThisMethod() throws Exception + void testWithoutGenerics() { var expected = """ - (line 5,col 3)-(line 7,col 3) + T[] -> Object[] + IN3[] -> Object[] + List -> List + IN[][] -> Object[][] """; + String[] cases = {"T[]", "IN3[]", "List", "IN[][]"}; + Approvals.verifyAll("", cases, c -> String.format("%s -> %s", c, removeGenerics(c)), new Options().inline(expected)); + } + @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) + public static Range getMethodLines(Method method) { CompilationUnit compilationUnit = parseJavaFile(method); - return compilationUnit.getClassByName("XmlApprovals").get().getMember(0).getRange().get(); + List methods = compilationUnit.getClassByName(method.getDeclaringClass().getSimpleName()) + .get().getMethods(); + for (MethodDeclaration methodDeclaration : methods) + { + if (isMethodDeclarationEqualToMethod(methodDeclaration, method)) + { return methodDeclaration.getRange().get(); } + } + throw new FormattedException("Could not find method %s(%s) in %s", method.getName(), + DocumentHelpersTest.showParameters(method), method.getDeclaringClass()); } - private CompilationUnit parseJavaFile(Method method) + private static CompilationUnit parseJavaFile(Method method) { File filePath = getSourceFile(method); String baseDir = StringUtils.removeFromEnd(System.getProperty("user.dir"), "-tests") + "/src/main/java/"; @@ -37,11 +62,50 @@ private CompilationUnit parseJavaFile(Method method) String name = filePath.getName(); return sourceRoot.parse(packageName, name); } - private File getSourceFile(Method method) + private static 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); } + public static boolean isMethodDeclarationEqualToMethod(MethodDeclaration methodDeclaration, Method method) + { + String currentMethod = String.format("%s(%s)", method.getName(), DocumentHelpersTest.showParameters(method)); + if (!methodDeclaration.getNameAsString().equals(method.getName())) + { return false; } + List> compiledMethodParameters = List.of(method.getParameterTypes()); + NodeList parsedMethodParameters = methodDeclaration.getParameters(); + if (compiledMethodParameters.size() != parsedMethodParameters.size()) + { return false; } + for (int i = 0; i < compiledMethodParameters.size(); i++) + { + String compileName = compiledMethodParameters.get(i).getSimpleName(); + String parseName = parsedMethodParameters.get(i).getTypeAsString(); + if (parsedMethodParameters.get(i).isVarArgs()) { + parseName += "[]"; + } + String withoutGenerics = removeGenerics(parseName); + if (!compileName.equals(withoutGenerics)) + { return false; } + } + return true; + } + private static String removeGenerics(String parseName) + { + String without = parseName.replaceAll("<.*>", ""); + if (without.endsWith("[][]")) + { + String name = StringUtils.removeFromEnd(without, "[][]"); + if (name.length() <= 3) + { return "Object[][]"; } + } + else if (without.endsWith("[]")) + { + String name = StringUtils.removeFromEnd(without, "[]"); + if (name.length() <= 3) + { return "Object[]"; } + } + return without; + } }