Skip to content

Commit

Permalink
#10 distinct short and long method descriptions
Browse files Browse the repository at this point in the history
This avoids that they show up twice just because Scala
compiles an object to a class that has each method twice, one
as static and one as an instance method
  • Loading branch information
FRosner committed Jan 25, 2016
1 parent 845b56e commit 90f3e91
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/de/frosner/replhelper/Helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Helper private (val reflections: Reflections, val filter: Class[_] => Bool
case ((className, category), categoryMethods) => {
s"${Console.BOLD}${category}${Console.RESET} [$className]" + NewLine + categoryMethods.map {
case (method, help) => "- " + getMethodSignature(method, help) + s": ${help.shortDescription}"
}.mkString(NewLine)
}.distinct.mkString(NewLine)
}
}.mkString(NewLine+NewLine)
)
Expand Down Expand Up @@ -92,7 +92,7 @@ class Helper private (val reflections: Reflections, val filter: Class[_] => Bool
case ((className, category), method, help) => getMethodSignature(method, help)
}.map {
methodWithHelp => getLongDescriptionPrintable(methodWithHelp, out)
}.mkString(NewLine+NewLine)
}.distinct.mkString(NewLine+NewLine)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@ object DummyObjectThatIsNoCompanion {
@Help(
category = "c",
shortDescription = "s",
longDescription = "l"
longDescription = "DummyObjectThatIsNoCompanionLong"
)
def method = ???

val name = this.getClass.getSimpleName.replace("$", "")
val category = "c"

val expectedMethodShortDescription = "- method(): s"

val expectedShortDescription = Array(
s"${Console.BOLD}c${Console.RESET} [${DummyObjectThatIsNoCompanion.name}]",
expectedMethodShortDescription,
""
)

val expectedMethodLongDescription = "DummyObjectThatIsNoCompanionLong"

val expectedLongDescription = Array(
s"${Console.BOLD}method()${Console.RESET} [${DummyObjectThatIsNoCompanion.name}]",
expectedMethodLongDescription,
""
)

}
30 changes: 20 additions & 10 deletions src/test/scala/de/frosner/replhelper/HelperTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ class HelperTest extends FlatSpec with Matchers {
result.toString.split(NewLine, -1) shouldBe TestClass.expectedShortOutput ++ TestClass2.expectedShortOutput
}

it should "not show duplicate entries for singleton objects" in {
val result = new ByteArrayOutputStream()
val out = new PrintStream(result)
val helper = Helper(ClasspathHelper.forClass(DummyObjectThatIsNoCompanion.getClass))
helper.printAllMethods(out)
result.toString.split(NewLine, -1).count(_ == DummyObjectThatIsNoCompanion.expectedMethodShortDescription) shouldBe 1
}

"When specific methods are requested, it" should "show the long description" in {
val result = new ByteArrayOutputStream()
val out = new PrintStream(result)
Expand All @@ -246,6 +254,16 @@ class HelperTest extends FlatSpec with Matchers {
result.toString.split(NewLine, -1) shouldBe TestClass2.expectedLongOutput
}

it should "not show duplicate entries for singleton objects" in {
val result = new ByteArrayOutputStream()
val out = new PrintStream(result)
val helper = Helper(ClasspathHelper.forClass(DummyObjectThatIsNoCompanion.getClass))
helper.printMethods("method", out)
println(result)
result.toString.split(NewLine, -1).count(_ == DummyObjectThatIsNoCompanion.expectedMethodLongDescription) shouldBe 1
}


"Curried parameters" should "be printed in correct order in the short description" in {
val result = new ByteArrayOutputStream()
val out = new PrintStream(result)
Expand Down Expand Up @@ -275,23 +293,15 @@ class HelperTest extends FlatSpec with Matchers {
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.name}]",
"l",
""
)
result.toString.split(NewLine, -1) shouldBe DummyObjectThatIsNoCompanion.expectedLongDescription
}

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.name}]",
"- method(): s",
""
)
result.toString.split(NewLine, -1) shouldBe DummyObjectThatIsNoCompanion.expectedShortDescription
}

}

0 comments on commit 90f3e91

Please sign in to comment.