Skip to content

Commit

Permalink
Merge pull request #92 from Kwestor/bugfix/locale-dependent-tests
Browse files Browse the repository at this point in the history
Bugfix/locale dependent tests
  • Loading branch information
sksamuel committed Jan 30, 2015
2 parents fb264df + 9cd568f commit c6ae6e2
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package scoverage

import java.io._

import scoverage.report.ScoverageXmlMerger

import scala.collection.{Set, mutable}
import scala.io.Source

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,11 @@ object ClassType {
}
}




case class ClassRef(name: String) {
lazy val simpleName = name.split(".").last
lazy val getPackage = name.split(".").dropRight(1).mkString(".")
}

object ClassRef {
def fromFilepath(path: String) = ClassRef(path.replace('/', '.'))
def apply(_package: String, className: String): ClassRef = ClassRef(_package.replace('/', '.') + "." + className)
Expand Down
19 changes: 11 additions & 8 deletions scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ScoveragePlugin(val global: Global) extends Plugin {
}
}
if (!opts.exists(_.startsWith("dataDir:")))
throw new RuntimeException("Cannot invoke plugin without specifiying <dataDir>")
throw new RuntimeException("Cannot invoke plugin without specifying <dataDir>")
instrumentationComponent.setOptions(options)
}

Expand Down Expand Up @@ -315,7 +315,7 @@ class ScoverageInstrumentationComponent(val global: Global)
c
}

// scalac generated classes, we just instrument the enclosed methods/statments
// scalac generated classes, we just instrument the enclosed methods/statements
// the location would stay as the source class
case c: ClassDef if c.symbol.isAnonymousClass || c.symbol.isAnonymousFunction =>
if (isFileIncluded(c.pos.source) && isClassIncluded(c.symbol))
Expand All @@ -336,7 +336,7 @@ class ScoverageInstrumentationComponent(val global: Global)
case DefDef(mods, _, _, _, _, _) if mods.isMacro => tree

// this will catch methods defined as macros, eg def test = macro testImpl
// it will not catch macro implemenations
// it will not catch macro implementations
case d: DefDef if d.symbol != null
&& d.symbol.annotations.size > 0
&& d.symbol.annotations.toString() == "macroImpl" =>
Expand Down Expand Up @@ -415,13 +415,13 @@ class ScoverageInstrumentationComponent(val global: Global)

// the If statement itself doesn't need to be instrumented, because instrumenting the condition is
// enough to determine if the If statement was executed.
// The two procedures (then and else) are instrumented seperately to determine if we entered
// The two procedures (then and else) are instrumented separately to determine if we entered
// both branches.
case i: If =>
treeCopy.If(i,
process(i.cond),
instrument(process(i.thenp), i.thenp, true),
instrument(process(i.elsep), i.elsep, true))
instrument(process(i.thenp), i.thenp, branch = true),
instrument(process(i.elsep), i.elsep, branch = true))

case _: Import => tree

Expand Down Expand Up @@ -533,9 +533,12 @@ class ScoverageInstrumentationComponent(val global: Global)
// This AST node corresponds to the following Scala code: expr: tpt
case t: Typed => super.transform(tree)

// instrument trys, catches and finally as seperate blocks
// instrument trys, catches and finally as separate blocks
case Try(t: Tree, cases: List[CaseDef], f: Tree) =>
treeCopy.Try(tree, instrument(process(t), t, true), transformCases(cases), instrument(process(f), f, true))
treeCopy.Try(tree,
instrument(process(t), t, branch = true),
transformCases(cases),
instrument(process(f), f, branch = true))

// type aliases, type parameters, abstract types
case t: TypeDef => super.transform(tree)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package scoverage.report

import scala.io.Source
import _root_.scoverage.MeasuredFile

import _root_.scoverage.{MeasuredFile, Statement}
import scala.io.Source

/** @author Stephen Samuel */
class CodeGrid(mfile: MeasuredFile) {
class CodeGrid(mFile: MeasuredFile) {

case class Cell(char: Char, var status: StatementStatus)

private val lineBreak = System.getProperty("line.separator")

// Array of lines, each line is an array of cells, where a cell is a character + coverage info for that position
// All cells defaul to NoData until the highlighted information is applied
// note: we must reinclude the line sep to keep source positions correct.
private val lines = source(mfile).split(lineBreak).map(line => (line.toCharArray ++ lineBreak).map(Cell(_, NoData)))
// All cells default to NoData until the highlighted information is applied
// note: we must re-include the line sep to keep source positions correct.
private val lines = source(mFile).split(lineBreak).map(line => (line.toCharArray ++ lineBreak).map(Cell(_, NoData)))

// useful to have a single array to write into the cells
private val cells = lines.flatten

// apply the instrumentation data to the cells updating their coverage info
mfile.statements.foreach(stmt => {
mFile.statements.foreach(stmt => {
for ( k <- stmt.start until stmt.end ) {
if (k < cells.size) {
// if the cell is set to Invoked, then it be changed to NotInvoked, as an inner statement will override
// outer containing statments. If a cell is NotInvoked then it can not be changed further.
// outer containing statements. If a cell is NotInvoked then it can not be changed further.
// in that block were executed
cells(k).status match {
case Invoked => if (!stmt.isInvoked) cells(k).status = NotInvoked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ScoverageHtmlWriter(sourceDirectory: File, outputDir: File) {
def packageOverview(pack: MeasuredPackage): Node = {
<html>
{header}<body style="font-family: monospace;">
{classesTable(pack.classes, false)}
{classesTable(pack.classes, addPath = false)}
</body>
</html>
}
Expand Down Expand Up @@ -417,7 +417,7 @@ class ScoverageHtmlWriter(sourceDirectory: File, outputDir: File) {
{stats(coverage)}
</div>
<div>
{classesTable(coverage.classes, true)}
{classesTable(coverage.classes, addPath = true)}
</div>
</div>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object ScoverageXmlReader {

var id = 0
val coverage = Coverage()
(xml \\ "statement") foreach { node => {
(xml \\ "statement") foreach { node =>

val source = node \ "@source"
val pkg = node \ "@package"
Expand Down Expand Up @@ -55,7 +55,6 @@ object ScoverageXmlReader {
count.text.toInt
)
}
}
coverage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _root_.scoverage.MeasuredFile
import scala.xml.Node

/** @author Stephen Samuel */
class StatementWriter(mfile: MeasuredFile) {
class StatementWriter(mFile: MeasuredFile) {

val GREEN = "#AEF1AE"
val RED = "#F0ADAD"
Expand All @@ -25,7 +25,7 @@ class StatementWriter(mfile: MeasuredFile) {
<th>Tree</th>
<th>Symbol</th>
<th>Code</th>
</tr>{mfile.statements.toSeq.sortBy(_.line).map(stmt => {
</tr>{mFile.statements.toSeq.sortBy(_.line).map(stmt => {
<tr>
<td>
{stmt.line}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scoverage

import java.io.File
import java.util.UUID
import java.util.{Locale, UUID}
import javax.xml.parsers.DocumentBuilderFactory

import org.scalatest.{BeforeAndAfter, FunSuite, OneInstancePerTest}
Expand Down Expand Up @@ -62,19 +62,16 @@ class CoberturaXmlWriterTest extends FunSuite with BeforeAndAfter with OneInstan
builder.setErrorHandler(new ErrorHandler() {
@Override
def error(e: SAXParseException) {
e.printStackTrace()
assert(false)
fail(e)
}
@Override
def fatalError(e: SAXParseException) {
e.printStackTrace()
assert(false)
fail(e)
}

@Override
def warning(e: SAXParseException) {
e.printStackTrace()
assert(false)
fail(e)
}
})
builder.parse(fileIn(dir))
Expand All @@ -100,8 +97,10 @@ class CoberturaXmlWriterTest extends FunSuite with BeforeAndAfter with OneInstan

val xml = XML.loadFile(fileIn(dir))

assert(xml \\ "coverage" \@ "line-rate" === "0.33", "line-rate")
assert(xml \\ "coverage" \@ "branch-rate" === "0.50", "branch-rate")
def formattedLocally(decimal: BigDecimal) = "%.2f".format(decimal)

assert(xml \\ "coverage" \@ "line-rate" === formattedLocally(0.33), "line-rate")
assert(xml \\ "coverage" \@ "branch-rate" === formattedLocally(0.50), "branch-rate")

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.scalatest.{FreeSpec, Matchers, OneInstancePerTest}
class IOUtilsTest extends FreeSpec with MockitoSugar with OneInstancePerTest with Matchers {

"io utils" - {
"should parse measurment files" in {
"should parse measurement files" in {
val file = File.createTempFile("scoveragemeasurementtest", "txt")
val writer = new FileWriter(file)
writer.write("1\n5\n9\n\n10\n")
Expand Down
38 changes: 19 additions & 19 deletions scalac-scoverage-plugin/src/test/scala/scoverage/LocationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LocationTest extends FreeSpec with Matchers {
"for classes" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.test\nclass Sammy")
val loc = compiler.locations.result.find(_._1 == "Template").get._2
val loc = compiler.locations.result().find(_._1 == "Template").get._2
loc.packageName shouldBe "com.test"
loc.className shouldBe "Sammy"
loc.topLevelClass shouldBe "Sammy"
Expand All @@ -20,7 +20,7 @@ class LocationTest extends FreeSpec with Matchers {
"for objects" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.test\nobject Bammy { def foo = 'boo } ")
val loc = compiler.locations.result.find(_._1 == "Template").get._2
val loc = compiler.locations.result().find(_._1 == "Template").get._2
loc.packageName shouldBe "com.test"
loc.className shouldBe "Bammy"
loc.topLevelClass shouldBe "Bammy"
Expand All @@ -31,7 +31,7 @@ class LocationTest extends FreeSpec with Matchers {
"for traits" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.test\ntrait Gammy { def goo = 'hoo } ")
val loc = compiler.locations.result.find(_._1 == "Template").get._2
val loc = compiler.locations.result().find(_._1 == "Template").get._2
loc.packageName shouldBe "com.test"
loc.className shouldBe "Gammy"
loc.topLevelClass shouldBe "Gammy"
Expand All @@ -43,7 +43,7 @@ class LocationTest extends FreeSpec with Matchers {
"should correctly process methods" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.methodtest \n class Hammy { def foo = 'boo } ")
val loc = compiler.locations.result.find(_._2.method == "foo").get._2
val loc = compiler.locations.result().find(_._2.method == "foo").get._2
loc.packageName shouldBe "com.methodtest"
loc.className shouldBe "Hammy"
loc.classType shouldBe ClassType.Class
Expand All @@ -52,7 +52,7 @@ class LocationTest extends FreeSpec with Matchers {
"should correctly process nested methods" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.methodtest \n class Hammy { def foo = { def goo = { getClass; 3 }; goo } } ")
val loc = compiler.locations.result.find(_._2.method == "goo").get._2
val loc = compiler.locations.result().find(_._2.method == "goo").get._2
loc.packageName shouldBe "com.methodtest"
loc.className shouldBe "Hammy"
loc.topLevelClass shouldBe "Hammy"
Expand All @@ -63,7 +63,7 @@ class LocationTest extends FreeSpec with Matchers {
"should process anon functions as inside the enclosing method" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.methodtest \n class Jammy { def moo = { Option(\"bat\").map(_.length) } } ")
val loc = compiler.locations.result.find(_._1 == "Function").get._2
val loc = compiler.locations.result().find(_._1 == "Function").get._2
loc.packageName shouldBe "com.methodtest"
loc.className shouldBe "Jammy"
loc.method shouldBe "moo"
Expand All @@ -74,7 +74,7 @@ class LocationTest extends FreeSpec with Matchers {
"for nested classes" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.methodtest \n class Jammy { class Pammy } ")
val loc = compiler.locations.result.find(_._2.className == "Pammy").get._2
val loc = compiler.locations.result().find(_._2.className == "Pammy").get._2
loc.packageName shouldBe "com.methodtest"
loc.className shouldBe "Pammy"
loc.topLevelClass shouldBe "Jammy"
Expand All @@ -85,7 +85,7 @@ class LocationTest extends FreeSpec with Matchers {
"for nested objects" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.methodtest \n class Jammy { object Zammy } ")
val loc = compiler.locations.result.find(_._2.className == "Zammy").get._2
val loc = compiler.locations.result().find(_._2.className == "Zammy").get._2
loc.packageName shouldBe "com.methodtest"
loc.className shouldBe "Zammy"
loc.topLevelClass shouldBe "Jammy"
Expand All @@ -96,7 +96,7 @@ class LocationTest extends FreeSpec with Matchers {
"for nested traits" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.methodtest \n class Jammy { trait Mammy } ")
val loc = compiler.locations.result.find(_._2.className == "Mammy").get._2
val loc = compiler.locations.result().find(_._2.className == "Mammy").get._2
loc.packageName shouldBe "com.methodtest"
loc.className shouldBe "Mammy"
loc.topLevelClass shouldBe "Jammy"
Expand All @@ -111,7 +111,7 @@ class LocationTest extends FreeSpec with Matchers {
compiler.compile("package com.a \n " +
"package b \n" +
"class Kammy ")
val loc = compiler.locations.result.find(_._1 == "Template").get._2
val loc = compiler.locations.result().find(_._1 == "Template").get._2
loc.packageName shouldBe "com.a.b"
loc.className shouldBe "Kammy"
loc.topLevelClass shouldBe "Kammy"
Expand All @@ -124,7 +124,7 @@ class LocationTest extends FreeSpec with Matchers {
compiler.compile("package com.a \n " +
"package b \n" +
"object Kammy ")
val loc = compiler.locations.result.find(_._1 == "Template").get._2
val loc = compiler.locations.result().find(_._1 == "Template").get._2
loc.packageName shouldBe "com.a.b"
loc.className shouldBe "Kammy"
loc.method shouldBe "<none>"
Expand All @@ -136,7 +136,7 @@ class LocationTest extends FreeSpec with Matchers {
compiler.compile("package com.a \n " +
"package b \n" +
"trait Kammy ")
val loc = compiler.locations.result.find(_._1 == "Template").get._2
val loc = compiler.locations.result().find(_._1 == "Template").get._2
loc.packageName shouldBe "com.a.b"
loc.className shouldBe "Kammy"
loc.topLevelClass shouldBe "Kammy"
Expand All @@ -149,7 +149,7 @@ class LocationTest extends FreeSpec with Matchers {
"for class constructor body" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.b \n class Tammy { val name = 'sam } ")
val loc = compiler.locations.result.find(_._1 == "ValDef").get._2
val loc = compiler.locations.result().find(_._1 == "ValDef").get._2
loc.packageName shouldBe "com.b"
loc.className shouldBe "Tammy"
loc.method shouldBe "<none>"
Expand All @@ -159,7 +159,7 @@ class LocationTest extends FreeSpec with Matchers {
"for object constructor body" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.b \n object Yammy { val name = 'sam } ")
val loc = compiler.locations.result.find(_._1 == "ValDef").get._2
val loc = compiler.locations.result().find(_._1 == "ValDef").get._2
loc.packageName shouldBe "com.b"
loc.className shouldBe "Yammy"
loc.topLevelClass shouldBe "Yammy"
Expand All @@ -170,7 +170,7 @@ class LocationTest extends FreeSpec with Matchers {
"for trait constructor body" in {
val compiler = ScoverageCompiler.locationCompiler
compiler.compile("package com.b \n trait Wammy { val name = 'sam } ")
val loc = compiler.locations.result.find(_._1 == "ValDef").get._2
val loc = compiler.locations.result().find(_._1 == "ValDef").get._2
loc.packageName shouldBe "com.b"
loc.className shouldBe "Wammy"
loc.topLevelClass shouldBe "Wammy"
Expand All @@ -185,8 +185,8 @@ class LocationTest extends FreeSpec with Matchers {
.compile(
"package com.a; object A { def foo(b : B) : Unit = b.invoke }; trait B { def invoke : Unit }; class C { A.foo(new B { def invoke = () }) }")
println()
println(compiler.locations.result.mkString("\n"))
val loc = compiler.locations.result.filter(_._1 == "Template").last._2
println(compiler.locations.result().mkString("\n"))
val loc = compiler.locations.result().filter(_._1 == "Template").last._2
loc.packageName shouldBe "com.a"
loc.className shouldBe "C"
loc.topLevelClass shouldBe "C"
Expand All @@ -199,8 +199,8 @@ class LocationTest extends FreeSpec with Matchers {
compiler.compile(
"package com.a; object A { def foo(b : B) : Unit = b.invoke }; trait B { def invoke : Unit }; class C { A.foo(new B { def invoke = () }) }")
println()
println(compiler.locations.result.mkString("\n"))
val loc = compiler.locations.result.filter(_._1 == "DefDef").last._2
println(compiler.locations.result().mkString("\n"))
val loc = compiler.locations.result().filter(_._1 == "DefDef").last._2
loc.packageName shouldBe "com.a"
loc.className shouldBe "C"
loc.topLevelClass shouldBe "C"
Expand Down
Loading

0 comments on commit c6ae6e2

Please sign in to comment.