Git Product home page Git Product logo

sbt-doctest's Introduction

sbt-doctest

Plugin for sbt that generates tests from examples in ScalaDoc.

Build Status Stories in Ready

Install

To use this plugin, add it to your project/plugins.sbt,

addSbtPlugin("com.github.tkawachi" % "sbt-doctest" % "0.3.3")

and add the following settings to your build.sbt.

doctestSettings

Using ScalaTest or specs2

This plugin generates tests for ScalaCheck by default. If you use ScalaTest or specs2, set doctestTestFramework to DoctestTestFramework.ScalaTest or DoctestTestFramework.Specs2 in build.sbt. Then it will generate tests for the specified framework.

// To generate tests for ScalaTest
doctestTestFramework := DoctestTestFramework.ScalaTest
// Or specify DoctestTestFramework.Specs2 or DoctestTestFramework.ScalaCheck

Note for libraryDependencies

doctestSettings adds specific version of testing libraries to libraryDependencies. Set doctestWithDependencies to false when you explicitly specify testing library dependencies in build.sbt.

doctestWithDependencies := false

libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "2.2.0" % "test",
  "org.scalacheck" %% "scalacheck" % "1.11.4" % "test"
  // And other library dependencies.
)

Usage

sbt-doctest will generate tests from doctests in ScalaDoc comments. These tests are automatically generated and run when sbt's test task is invoked.

Here is an example that shows the different doctest styles that are supported by the plugin:

object Test {

  /**
   * A sample function.
   *
   * {{{
   * # Python style
   * >>> Test.f(10)
   * 20
   *
   * # Scala REPL style
   * scala> Test.f(20)
   * res1: Int = 40
   *
   * # Property based test
   * prop> (i: Int) => Test.f(i) == (i * 2)
   * }}}
   */
  def f(x: Int) = x + x
}

It also supports multi-line inputs:

/**
 * {{{
 * # Python style
 * >>> Test.f(
 * ...   10
 * ... )
 * 20
 *
 * # Scala REPL style
 * scala> Test.f(
 *      |   20
 *      | )
 * res1: Int = 40
 *
 * # Property based test
 * prop> (i: Int) =>
 *     |   Test.f(i) == (i * 2)
 * }}}
 */
def f(x: Int) = x + x

Please use <BLANKLINE> when an output contains blank lines.

/**
 * {{{
 * # Python style
 * >>> Test.helloWorld
 * Hello
 * <BLANKLINE>
 * World
 *
 * # Scala REPL style
 * scala> Test.helloWorld
 * res0: String =
 * Hello
 * <BLANKLINE>
 * World
 * }}}
 */
def helloWorld = "Hello\n\nWorld"

License

MIT

sbt-doctest's People

Contributors

fthomas avatar tkawachi avatar tototoshi avatar waffle-iron avatar xuwei-k avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.