From efa71548d153ed4ab2d8450462b8ad5f936c9dc9 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Wed, 3 Sep 2014 23:24:08 +0200 Subject: [PATCH] Add unit tests for TextAnalysisFormat Those tests use the random Analysis generator that is used in the unit tests for the subproject `incremental-compiler`. Random Analyses are serialized and then constructed back from this representation. --- .../inc/TextAnalysisFormatSpecification.scala | 112 ++++++++++++++++++ project/Sbt.scala | 2 +- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 compile/persist/src/test/scala/sbt/inc/TextAnalysisFormatSpecification.scala diff --git a/compile/persist/src/test/scala/sbt/inc/TextAnalysisFormatSpecification.scala b/compile/persist/src/test/scala/sbt/inc/TextAnalysisFormatSpecification.scala new file mode 100644 index 000000000..1b8c4cb15 --- /dev/null +++ b/compile/persist/src/test/scala/sbt/inc/TextAnalysisFormatSpecification.scala @@ -0,0 +1,112 @@ +package sbt +package inc + +import java.io.{ BufferedReader, File, StringReader, StringWriter } +import scala.math.abs +import org.scalacheck._ +import Gen._ +import Prop._ + +object TextAnalysisFormatTest extends Properties("TextAnalysisFormat") { + + val nameHashing = true + val dummyOutput = new xsbti.compile.SingleOutput { def outputDirectory: java.io.File = new java.io.File("dummy") } + val commonSetup = new CompileSetup(dummyOutput, new CompileOptions(Nil, Nil), "2.10.4", xsbti.compile.CompileOrder.Mixed, nameHashing) + val commonHeader = """format version: 5 + |output mode: + |1 items + |0 -> single + |output directories: + |1 items + |output dir -> dummy + |compile options: + |0 items + |javac options: + |0 items + |compiler version: + |1 items + |0 -> 2.10.4 + |compile order: + |1 items + |0 -> Mixed + |name hashing: + |1 items + |0 -> true""".stripMargin + + property("Write and read empty Analysis") = { + + val writer = new StringWriter + val analysis = Analysis.empty(nameHashing) + TextAnalysisFormat.write(writer, analysis, commonSetup) + + val result = writer.toString + + result.startsWith(commonHeader) + val reader = new BufferedReader(new StringReader(result)) + + val (readAnalysis, readSetup) = TextAnalysisFormat.read(reader) + + analysis == readAnalysis + + } + + property("Write and read simple Analysis") = { + + import TestCaseGenerators._ + + def f(s: String) = new File(s) + val aScala = f("A.scala") + val bScala = f("B.scala") + val aSource = genSource("A" :: "A$" :: Nil).sample.get + val bSource = genSource("B" :: "B$" :: Nil).sample.get + val cSource = genSource("C" :: Nil).sample.get + val exists = new Exists(true) + val sourceInfos = SourceInfos.makeInfo(Nil, Nil) + + var analysis = Analysis.empty(nameHashing) + analysis = analysis.addProduct(aScala, f("A.class"), exists, "A") + analysis = analysis.addProduct(aScala, f("A$.class"), exists, "A$") + analysis = analysis.addSource(aScala, aSource, exists, Nil, Nil, sourceInfos) + analysis = analysis.addBinaryDep(aScala, f("x.jar"), "x", exists) + analysis = analysis.addExternalDep(aScala, "C", cSource, inherited = false) + + val writer = new StringWriter + + TextAnalysisFormat.write(writer, analysis, commonSetup) + + val result = writer.toString + + result.startsWith(commonHeader) + val reader = new BufferedReader(new StringReader(result)) + + val (readAnalysis, readSetup) = TextAnalysisFormat.read(reader) + + compare(analysis, readAnalysis) + + } + + property("Write and read complex Analysis") = forAllNoShrink(TestCaseGenerators.genAnalysis(nameHashing)) { analysis: Analysis => + val writer = new StringWriter + + TextAnalysisFormat.write(writer, analysis, commonSetup) + + val result = writer.toString + + result.startsWith(commonHeader) + val reader = new BufferedReader(new StringReader(result)) + + val (readAnalysis, readSetup) = TextAnalysisFormat.read(reader) + + compare(analysis, readAnalysis) + } + + // Compare two analyses with useful labelling when they aren't equal. + private[this] def compare(left: Analysis, right: Analysis): Prop = + s" LEFT: $left" |: + s"RIGHT: $right" |: + s"STAMPS EQUAL: ${left.stamps == right.stamps}" |: + s"APIS EQUAL: ${left.apis == right.apis}" |: + s"RELATIONS EQUAL: ${left.relations == right.relations}" |: + "UNEQUAL" |: + (left == right) +} \ No newline at end of file diff --git a/project/Sbt.scala b/project/Sbt.scala index 5b2453dfb..2ece99dcf 100644 --- a/project/Sbt.scala +++ b/project/Sbt.scala @@ -153,7 +153,7 @@ object Sbt extends Build { // Defines the data structures for representing file fingerprints and relationships and the overall source analysis lazy val compileIncrementalSub = testedBaseProject(compilePath / "inc", "Incremental Compiler") dependsOn (apiSub, ioSub, logSub, classpathSub, relationSub) // Persists the incremental data structures using SBinary - lazy val compilePersistSub = baseProject(compilePath / "persist", "Persist") dependsOn (compileIncrementalSub, apiSub) settings (sbinary) + lazy val compilePersistSub = testedBaseProject(compilePath / "persist", "Persist") dependsOn (compileIncrementalSub, apiSub, compileIncrementalSub % "test->test") settings (sbinary) // sbt-side interface to compiler. Calls compiler-side interface reflectively lazy val compilerSub = testedBaseProject(compilePath, "Compile") dependsOn (launchInterfaceSub, interfaceSub % "compile;test->test", logSub, ioSub, classpathSub, logSub % "test->test", launchSub % "test->test", apiSub % "test") settings (compilerSettings: _*)