From 18e215394913a7b84ca0ef22c23bcf65d768ab1b Mon Sep 17 00:00:00 2001 From: Teletha Date: Mon, 22 Apr 2024 18:51:32 +0900 Subject: [PATCH] fix: test cache the expected result --- src/test/java/reincarnation/CodeVerifier.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/test/java/reincarnation/CodeVerifier.java b/src/test/java/reincarnation/CodeVerifier.java index 091b396d..93bfd343 100644 --- a/src/test/java/reincarnation/CodeVerifier.java +++ b/src/test/java/reincarnation/CodeVerifier.java @@ -79,6 +79,12 @@ public class CodeVerifier { /** The built-in parameters. */ private static final List texts = List.of("", " ", "a", "A", "あ", "\\", "\t", "some value"); + /** + * Cache the original expected value. If a test containing static references is run multiple + * times, the expected value may be different each time. + */ + private static final Map cache = new ConcurrentHashMap(); + /** The debugged class manager. */ private static final Set debugged = ConcurrentHashMap.newKeySet(); @@ -110,15 +116,20 @@ protected final void verify(TestCode code) { Class target = code.getClass(); // ======================================================== - // execute test code and store results by java + // Execute test code and store results by java. + // + // Cache the original expected value. If a test containing static references is run multiple + // times, the expected value may be different each time. // ======================================================== JavaVerifier base = new JavaVerifier(target, "Execute original test case by java."); List inputs = prepareInputs(base.method); - List expecteds = new ArrayList(); - - for (Object input : inputs) { - expecteds.add(base.verifier.apply(input)); - } + List expecteds = cache.computeIfAbsent(target, key -> { + List list = new ArrayList(); + for (Object input : inputs) { + list.add(base.verifier.apply(input)); + } + return list; + }); if (base.method.isAnnotationPresent(ShowJavaResultOnly.class)) { System.out.println("Show Java result : " + base.method); @@ -129,19 +140,19 @@ protected final void verify(TestCode code) { } // ======================================================== - // prepare recompile + // Prepare recompile. // ======================================================== StringBuilder debugLog = debugger.replaceOutput(); String decompiled = ""; try { // ======================================================== - // decompile code + // Decompile code. // ======================================================== decompiled = decompile(target, debuggable != null); // ======================================================== - // recompile java code + // Recompile java code. // ======================================================== Class recompiledClass; Silent notifier = new Silent(); @@ -164,7 +175,7 @@ protected final void verify(TestCode code) { } // ======================================================== - // execute recompiled code and compare result with original + // Execute recompiled code and compare result with original. // ======================================================== JavaVerifier java = new JavaVerifier(recompiledClass, decompiled); @@ -242,13 +253,7 @@ private String decompile(Class target, boolean debug) { if (CompilerType.current.get() == CompilerType.ECJ) { Reincarnation.loader.set(ClassLoader.getSystemClassLoader()); } else { - try { - compileAllTestsByJavac(); - Reincarnation.loader.set(JavacClassLoader); - target = JavacClassLoader.loadClass(target.getName()); - } catch (ClassNotFoundException e) { - throw I.quiet(e); - } + Reincarnation.loader.set(ClassLoader.getSystemClassLoader()); } JavaCodingOption options = new JavaCodingOption();