Sha256: 61d641d42d5ceb89784abbf07625af18b9f08092c916e511ac04d2ea7963a1d4

Contents?: true

Size: 1.79 KB

Versions: 84

Compression:

Stored size: 1.79 KB

Contents

import play.api.libs.json.Json

import scala.io.Source

class BowlingTestGenerator {
  implicit val testCaseReader = Json.reads[BowlingTestCase]

  private val filename = "bowling.json"
  private val fileContents = Source.fromFile(filename).getLines.mkString
  private val json = Json.parse(fileContents)

  def write {
    print("import org.scalatest.{FunSuite, Matchers}" + System.lineSeparator())
    print(System.lineSeparator())
    print("class BowlingSuite extends FunSuite with Matchers {" + System.lineSeparator())

    writeTestCases()

    print("}" + System.lineSeparator())
  }

  private def writeTestCases(): Unit = {
    println("// " +  (json \ "score" \ "description").get.as[List[String]].mkString(" "))

    val testCases = (json \ "score" \ "cases").get.as[List[BowlingTestCase]]

    testCases.foreach(tc => {
      print("\ttest(\"" + tc.description + "\") {" + System.lineSeparator())
      println("pending")
      print("val score = List(")
      print(tc.rolls.map(roll => roll.toString).mkString(", "))
      println(").foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()")

      println("score match {")
      if (tc.expected == -1) {
        println("case Right(_) => fail(\"Unexpected score returned. Failure expected\")")
        println("case Left(_) => ")
      } else {
        println("case Right(n) => assert(n == " + tc.expected.toString + ")")
        println("case Left(_) => fail(\"" + tc.description + "\")")
      }
      println("}")
      print("\t}" + System.lineSeparator())
      print(System.lineSeparator())
    })
  }
}

case class BowlingTestCase(description: String,
                           rolls: List[Int],
                           expected: Int)

object BowlingTestGenerator {
  def main(args: Array[String]): Unit = {
    new BowlingTestGenerator().write
  }
}

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
trackler-2.0.6.40 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.39 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.38 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.37 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.36 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.35 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.34 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.33 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.32 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.31 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.30 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.29 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.28 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.27 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.26 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.25 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.24 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.23 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.22 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala
trackler-2.0.6.21 tracks/scala/testgen/src/main/scala/BowlingTestGenerator.scala