fixes #36: Graph.insertBar doesn't take maxColumn into account leading to StringIndexOutOfBoundsException

This commit is contained in:
Johannes Rudolph 2013-07-31 15:22:45 +02:00
parent 71f8f3a53a
commit 0bcd2e03f4
3 changed files with 68 additions and 6 deletions

View File

@ -13,4 +13,9 @@ libraryDependencies <++= scalaVersion { version =>
else Nil
}
libraryDependencies <+= scalaVersion { version =>
if (version startsWith "2.9") "org.specs2" % "specs2_2.9.3" % "1.12.4.1" % "test"
else "org.specs2" %% "specs2" % "2.1.1" % "test"
}
scalacOptions ++= Seq("-deprecation", "-unchecked")

View File

@ -23,12 +23,14 @@ object Graph
if (s.length > maxColumn) s.slice(0, maxColumn - 2) + ".."
else s
def insertBar(s: String, at: Int): String =
s.slice(0, at) +
(s(at).toString match {
case " " => "|"
case x => x
}) +
s.slice(at + 1, s.length)
if (at < s.length)
s.slice(0, at) +
(s(at).toString match {
case " " => "|"
case x => x
}) +
s.slice(at + 1, s.length)
else s
def toAsciiLines(node: A, level: Int): Vector[String] = {
val line = limitLine((twoSpaces * level) + (if (level == 0) "" else "+-") + display(node))
val cs = Vector(children(node): _*)

View File

@ -0,0 +1,55 @@
package net.virtualvoid.sbt.graph
import org.specs2.mutable.Specification
class GraphSpecs extends Specification {
sealed trait Tree
case class Branch(left: Tree, right: Tree) extends Tree
case class Leaf(i: Int) extends Tree
def children(t: Tree): Seq[Tree] = t match {
case Branch(left, right) => Seq(left, right)
case _: Leaf => Nil
}
def display(t: Tree): String = t match {
case Branch(left, right) => "Branch"
case Leaf(value) => value.toString * value
}
"Graph" should {
"layout simple graphs" in {
val simple = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
Graph.toAscii(simple, children, display, 20) ===
"""Branch
| +-Branch
| | +-1
| | +-22
| |\u0020
| +-333
| """.stripMargin
}
"layout deep graphs" in {
val simple = Branch(Branch(Branch(Branch(Branch(Branch(Leaf(1), Leaf(1)), Leaf(1)), Leaf(1)), Leaf(2)), Leaf(3)), Leaf(4))
Graph.toAscii(simple, children, display, 10) ===
"""Branch
| +-Branch
| | +-Br..
| | | +-..
| | | | ..
| | | | ..
| | | | ..
| | | | ..
| | | | | |\u0020
| | | | ..
| | | | |\u0020
| | | | ..
| | | |\u0020
| | | +-22
| | |\u0020
| | +-333
| |\u0020
| +-4444
| """.stripMargin
}
}
}