From cc052b598c2821b85df6096db8d2571271f31bf4 Mon Sep 17 00:00:00 2001 From: Stu Hood Date: Mon, 6 Jul 2015 13:43:14 -0700 Subject: [PATCH] Add test for stable APIs with identical constants --- .../sbt/compiler/javac/JavaCompilerSpec.scala | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/compile/src/test/scala/sbt/compiler/javac/JavaCompilerSpec.scala b/compile/src/test/scala/sbt/compiler/javac/JavaCompilerSpec.scala index f5a131674..6d2494248 100644 --- a/compile/src/test/scala/sbt/compiler/javac/JavaCompilerSpec.scala +++ b/compile/src/test/scala/sbt/compiler/javac/JavaCompilerSpec.scala @@ -5,7 +5,7 @@ import java.net.URLClassLoader import sbt._ import org.specs2.Specification -import xsbt.api.SameAPI +import xsbt.api.{ SameAPI, DefaultShowAPI } import xsbti.api.SourceAPI import xsbti.{ Severity, Problem } @@ -20,20 +20,19 @@ object JavaCompilerSpec extends Specification { issue errors and warnings ${findsErrors(local)} Compiling a file with forked javac should - compile a java file ${works(forked)} - issue errors and warnings ${findsErrors(forked)} - yield the same errors as local javac $forkSameAsLocal + compile a java file ${works(forked)} + issue errors and warnings ${findsErrors(forked)} + yield the same errors as local javac $forkSameAsLocal Documenting a file with forked javadoc should - document a java file ${docWorks(forked)} - find errors in a java file ${findsDocErrors(forked)} + document a java file ${docWorks(forked)} + find errors in a java file ${findsDocErrors(forked)} - // TODO: move somewhere analysis-specific Analyzing classes generated by javac should - result in different APIs for static-final-primitive fields ${analyzeStaticDifference(local)} + result in matching APIs for stable static-final fields ${analyzeStaticDifference(local, "A", "A")} + result in different APIs for changed static-final fields ${analyzeStaticDifference(local, "A", "B")} """ - // TODO - write a test to ensure that java .class files wind up in the right spot, and we can call the compiled java code. def docWorks(compiler: JavaTools) = IO.withTemporaryDirectory { out => val (result, problems) = doc(compiler, Seq(knownSampleGoodFile), Seq("-d", out.getAbsolutePath)) val compiled = result must beTrue @@ -70,7 +69,11 @@ object JavaCompilerSpec extends Specification { errored and foundErrorAndWarning and hasKnownErrors } - def analyzeStaticDifference(compiler: JavaTools) = { + /** + * Compiles with the given constant values, and confirms that if the strings are equal, then the + * the APIs are equal. + */ + def analyzeStaticDifference(compiler: JavaTools, left: String, right: String) = { def compileWithPrimitive(templateValue: String) = IO.withTemporaryDirectory { out => // copy the input file to a temporary location and change the templateValue @@ -87,11 +90,13 @@ object JavaCompilerSpec extends Specification { (origCompiled, ClassToAPI(Seq(clazzz))) } - // compile with two different primitive values, and confirm that they're non-equal - val (prevCompiled, prevAPI) = compileWithPrimitive("A") - val (nextCompiled, nextAPI) = compileWithPrimitive("B") - val apisNonEqual = SameAPI(prevAPI, nextAPI) must beFalse - prevCompiled and nextCompiled and apisNonEqual + // compile with two different primitive values, and confirm that they match if their + // values match + val (leftCompiled, leftAPI) = compileWithPrimitive(left) + val (rightCompiled, rightAPI) = compileWithPrimitive(right) + val apisExpectedMatch = SameAPI(leftAPI, rightAPI) must beEqualTo(left == right) + + leftCompiled and rightCompiled and apisExpectedMatch } def lineMatches(p: Problem, lineno: Int): Boolean =