Skip to content

Commit

Permalink
Update bob exercise to conform to conform to 1.2 canonical-json.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricemery committed Feb 5, 2018
1 parent 3e711fd commit cc89b30
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
10 changes: 0 additions & 10 deletions exercises/bob/.meta/description.md

This file was deleted.

11 changes: 11 additions & 0 deletions exercises/bob/example.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
object Bob {
def response(statement: String): String = statement match {
case ShoutingQuestion() => "Calm down, I know what I'm doing!"
case Shouting() => "Whoa, chill out!"
case Question() => "Sure."
case Silence() => "Fine. Be that way!"
case _ => "Whatever."
}

case object ShoutingQuestion {
def unapply(statement: String) =
hasLetter(statement) && isOnlyUppercase(statement) &&
statement.trim.endsWith("?")

private def hasLetter(s: String) = s.matches(".*[A-Z].*")

private def isOnlyUppercase(s: String) = s == s.toUpperCase
}

case object Shouting {
def unapply(statement: String) =
hasLetter(statement) && isOnlyUppercase(statement)
Expand Down
5 changes: 3 additions & 2 deletions exercises/bob/src/test/scala/BobTest.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.0 */
/** @version 1.2.0 */
class BobTest extends FunSuite with Matchers {


test("stating something") {
Bob.response("Tom-ay-to, tom-aaaah-to.") should be ("Whatever.")
}
Expand Down Expand Up @@ -44,7 +45,7 @@ class BobTest extends FunSuite with Matchers {

test("forceful question") {
pending
Bob.response("WHAT THE HELL WERE YOU THINKING?") should be ("Whoa, chill out!")
Bob.response("WHAT THE HELL WERE YOU THINKING?") should be ("Calm down, I know what I'm doing!")
}

test("shouting numbers") {
Expand Down
7 changes: 4 additions & 3 deletions testgen/src/main/scala/BobTestGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import java.io.File

import testgen.TestSuiteBuilder
import testgen.TestSuiteBuilder.fromLabeledTest
import testgen.TestSuiteBuilder.fromLabeledTestFromInput

object BobTestGenerator {
def main(args: Array[String]): Unit = {
val file = new File("bob.json")
val file = new File("src/main/resources/bob.json")

val code = TestSuiteBuilder.build(file, fromLabeledTestFromInput("heyBob"))

val code = TestSuiteBuilder.build(file, fromLabeledTest("input"))
println(s"-------------")
println(code)
println(s"-------------")
Expand Down
16 changes: 16 additions & 0 deletions testgen/src/main/scala/testgen/TestSuiteBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ object TestSuiteBuilder {
TestCaseData(labeledTest.description, sutCall, expected)
}

// Use when arguments are layered under "input" json element
def fromLabeledTestFromInput(argNames: String*): ToTestCaseData =
withLabeledTest { sut => labeledTest =>
val sutFunction = labeledTest.property
val args = sutArgsFromInput(labeledTest.result, argNames: _*)
val sutCall = s"$sut.$sutFunction(${args})"
val expected = toString(labeledTest.expected)

TestCaseData(labeledTest.description, sutCall, expected)
}

def fromLabeledTestAlt(propArgs: (String, Seq[String])*): ToTestCaseData =
withLabeledTest { sut => labeledTest =>
val sutFunction = labeledTest.property
Expand Down Expand Up @@ -110,12 +121,17 @@ object TestSuiteBuilder {

def sutName(exerciseName: String) =
exerciseName split "-" map (_.capitalize) mkString

def testSuiteName(exerciseName: String): String =
sutName(exerciseName) + "Test"

def sutArgs(parseResult: CanonicalDataParser.ParseResult, argNames: String*): String =
argNames map (name => toString(parseResult(name))) mkString(", ")

// Use when arguments are layered under "input" json element
def sutArgsFromInput(parseResult: CanonicalDataParser.ParseResult, argNames: String*): String =
argNames map (name => toString(parseResult("input").asInstanceOf[Map[String, Any]](name))) mkString(", ")

def sutArgsAlt(parseResult: CanonicalDataParser.ParseResult, propArgs: (String, Seq[String])*): String =
propArgs collect {
case ((property, argNames)) if parseResult("property") == property =>
Expand Down

0 comments on commit cc89b30

Please sign in to comment.