From 0bcd2e03f49f6c7133e9ef9e40b95d40970e4a29 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Wed, 31 Jul 2013 15:22:45 +0200 Subject: [PATCH] fixes #36: Graph.insertBar doesn't take maxColumn into account leading to StringIndexOutOfBoundsException --- build.sbt | 5 ++ .../net/virtualvoid/sbt/graph/Graph.scala | 14 +++-- .../virtualvoid/sbt/graph/GraphSpecs.scala | 55 +++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/test/scala/net/virtualvoid/sbt/graph/GraphSpecs.scala diff --git a/build.sbt b/build.sbt index 5000fd925..7401e1f27 100644 --- a/build.sbt +++ b/build.sbt @@ -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") diff --git a/src/main/scala/net/virtualvoid/sbt/graph/Graph.scala b/src/main/scala/net/virtualvoid/sbt/graph/Graph.scala index c8cd76a48..c351fa71a 100644 --- a/src/main/scala/net/virtualvoid/sbt/graph/Graph.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/Graph.scala @@ -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): _*) diff --git a/src/test/scala/net/virtualvoid/sbt/graph/GraphSpecs.scala b/src/test/scala/net/virtualvoid/sbt/graph/GraphSpecs.scala new file mode 100644 index 000000000..208ff5227 --- /dev/null +++ b/src/test/scala/net/virtualvoid/sbt/graph/GraphSpecs.scala @@ -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 + } + } +}