Skip to content

Commit

Permalink
#1 remove $ from class name
Browse files Browse the repository at this point in the history
  • Loading branch information
FRosner committed Jan 4, 2016
2 parents e5cad8b + d4c8c52 commit 1efc67e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/main/scala/de/frosner/replhelper/Helper.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.frosner.replhelper

import java.io._

import scala.util.{Failure, Try}
import java.io.PrintStream

/**
* Utility class to provide interactive help for methods of a given class. It scans the class method definitions for
Expand All @@ -19,6 +17,8 @@ case class Helper[T](classWithHelp: Class[T]) {
type ShortDescription = String
type LongDescription = String

private val simpleClassName = classWithHelp.getSimpleName.replace("$", "")

private[replhelper] val methods = {
val methodsThatOfferHelp = classWithHelp.getMethods.filter(method => method.getAnnotations.exists(
annotation => annotation.isInstanceOf[Help]
Expand Down Expand Up @@ -48,7 +48,7 @@ case class Helper[T](classWithHelp: Class[T]) {
def printAllMethods(out: PrintStream) = out.println(
methods.map {
case (category, methods) => {
s"\033[1m${category}\033[0m [${classWithHelp.getSimpleName}]" + NEWLINE + methods.map {
s"${Console.BOLD}${category}${Console.RESET} [$simpleClassName]" + NEWLINE + methods.map {
case (name, help) => "- " + getMethodSignature(name, help) + s": ${help.shortDescription}"
}.mkString(NEWLINE)
}
Expand Down Expand Up @@ -90,7 +90,7 @@ case class Helper[T](classWithHelp: Class[T]) {

private def getLongDescriptionPrintable(methodWithHelp: (String, Help), out: PrintStream) = {
val (name, help) = methodWithHelp
s"\033[1m${getMethodSignature(name, help)}\033[0m [${classWithHelp.getSimpleName}]" + NEWLINE + help.longDescription
s"${Console.BOLD}${getMethodSignature(name, help)}${Console.RESET} [$simpleClassName]" + NEWLINE + help.longDescription
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.frosner.replhelper

object DummyObjectThatIsNoCompanion {

@Help(
category = "c",
shortDescription = "s",
longDescription = "l"
)
def method = ???

}
38 changes: 31 additions & 7 deletions src/test/scala/de/frosner/replhelper/HelperTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ class HelperTest extends FlatSpec with Matchers {
val helper = Helper(new TestClass().getClass)
helper.printAllMethods(out)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"\033[1ma\033[0m [TestClass]",
s"${Console.BOLD}a${Console.RESET} [TestClass]",
"- help(): short help",
"- xhelp(): short help",
"",
s"\033[1mbbb\033[0m [TestClass]",
s"${Console.BOLD}bbb${Console.RESET} [TestClass]",
"- helpWithParameters(i: Int)(s: String): sph",
""
)
Expand All @@ -110,7 +110,7 @@ class HelperTest extends FlatSpec with Matchers {
val helper = Helper(new TestClass().getClass)
helper.printMethods("help", out)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"\033[1mhelp()\033[0m [TestClass]",
s"${Console.BOLD}help()${Console.RESET} [TestClass]",
"long help",
""
)
Expand All @@ -123,10 +123,10 @@ class HelperTest extends FlatSpec with Matchers {
helper.printMethods("method", out)
println(result.toString)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"\033[1mmethod()\033[0m [TestClass2]",
s"${Console.BOLD}method()${Console.RESET} [TestClass2]",
"method without parameters",
"",
s"\033[1mmethod(s: String)\033[0m [TestClass2]",
s"${Console.BOLD}method(s: String)${Console.RESET} [TestClass2]",
"method with one parameter",
""
)
Expand All @@ -138,7 +138,7 @@ class HelperTest extends FlatSpec with Matchers {
val helper = Helper(new TestClass3().getClass)
helper.printAllMethods(out)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"\033[1mcategory\033[0m [TestClass3]",
s"${Console.BOLD}category${Console.RESET} [TestClass3]",
"- method(1)(2)(3)(4)(5)(6)(7)(8)(9): short",
""
)
Expand All @@ -150,10 +150,34 @@ class HelperTest extends FlatSpec with Matchers {
val helper = Helper(new TestClass3().getClass)
helper.printMethods("method", out)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"\033[1mmethod(1)(2)(3)(4)(5)(6)(7)(8)(9)\033[0m [TestClass3]",
s"${Console.BOLD}method(1)(2)(3)(4)(5)(6)(7)(8)(9)${Console.RESET} [TestClass3]",
"long",
""
)
}

"Solitary objects" should "not contain $ in their name when long description is printed" in {
val result = new ByteArrayOutputStream()
val out = new PrintStream(result)
val helper = Helper(DummyObjectThatIsNoCompanion.getClass)
helper.printMethods("method", out)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"${Console.BOLD}method()${Console.RESET} [DummyObjectThatIsNoCompanion]",
"l",
""
)
}

it should "not contain $ in their name when short description is printed" in {
val result = new ByteArrayOutputStream()
val out = new PrintStream(result)
val helper = Helper(DummyObjectThatIsNoCompanion.getClass)
helper.printAllMethods(out)
result.toString.split(NEWLINE, -1) shouldBe Array(
s"${Console.BOLD}c${Console.RESET} [DummyObjectThatIsNoCompanion]",
"- method(): s",
""
)
}

}

0 comments on commit 1efc67e

Please sign in to comment.