scalacheck

I put my Scala String Utilities library on Github alvin February 12, 2019 - 10:56am

I put my Scala String Utilities library on Github a few days ago. It includes my Q String Interpolator, and several other string utility functions. It also demonstrates how to write ScalaTest and ScalaCheck tests with an SBT project.

ScalaCheck custom generator examples

Table of Contents1 - Custom generators2 - Built-in ScalaCheck generators3 - How to use ScalaCheck generators4 - More ScalaCheck generators

Writing custom generators for ScalaCheck can be one of the more difficult and/or time-consuming parts of using it. As a result I thought I’d start putting together a list of generators that I have written or seen elsewhere. Unfortunately I can’t credit all the ones I’ve seen in other places because I google’d and copied them many moons ago, but I’ll give credit/attribution to all the ones I can.

Back to top

Custom generators

This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:

This is a page from my book, Functional Programming, Simplified

ScalaCheck 2: A More-Complicated Example

“But I was trying to think why I don’t use QuickCheck — which is a very nice tool — more. I think it’s because the situations that cause me trouble are ones that I would find it difficult to generate test data for.”

Simon Peyton Jones, in the book, Coders at Work

In this lesson I’ll share a non-trivial example of how I used ScalaCheck to test a function I wrote recently.

How to use ScalaCheck in the SBT console

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

How to compile, run, and package a Scala project with SBT

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.2, “How to compile, run, and package a Scala project with SBT.”

Back to top

Problem

You want to use SBT to compile and run a Scala project, and package the project as a JAR file.

Back to top

Solution

Create a directory layout to match what SBT expects, then run sbt compile to compile your project, sbt run to run your project, and sbt package to package your project as a JAR file.

Table of Contents

  1. Problem
  2. Solution
Back to top