Skip to content

Instantly share code, notes, and snippets.

@noelmarkham
Created December 11, 2014 20:37
Show Gist options
  • Save noelmarkham/e9f34549df34fb640d63 to your computer and use it in GitHub Desktop.
Save noelmarkham/e9f34549df34fb640d63 to your computer and use it in GitHub Desktop.
Automatic typeclass (scalacheck arbitrary here) generation using Shapeless
scala> :paste
// Entering paste mode (ctrl-D to finish)
import org.scalacheck.Prop.forAll
import org.scalacheck.Properties
import shapeless.contrib.scalacheck._
case class WebServerConfig(hostname: String, port: Int)
case class ApplicationConfig(webServer: WebServerConfig)
class Application(applicationConfig: ApplicationConfig) {
import applicationConfig._
def callWebServer(s: String): String = s"This web server on ${webServer.hostname}:${webServer.port} was called with $s"
}
// Exiting paste mode, now interpreting.
import org.scalacheck.Prop.forAll
import org.scalacheck.Properties
import shapeless.contrib.scalacheck._
defined class WebServerConfig
defined class ApplicationConfig
defined class Application
scala> val prop = forAll { (c: ApplicationConfig, s: String) =>
| val app = new Application(c)
| val response = app.callWebServer(s)
|
| response.contains(c.webServer.hostname) && response.contains(s)
| }
prop: org.scalacheck.Prop = Prop
scala> prop.check
+ OK, passed 100 tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment