• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
    Scala
  • License
    MIT License
  • Created over 10 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Doctest for scala

sbt-doctest

Plugin for sbt that generates tests from examples in ScalaDoc.

Install

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

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

This plugin supports sbt 1.x.

It's automatically enabled for JVM projects. Scala.js is currently not supported (See #52).

sbt-doctest allows you to choose which test library to use by doctestTestFramework. By default, the tests are generated for ScalaCheck. The test libraries need to be added separately to libraryDependencies.

Using ScalaCheck

If you are using ScalaCheck, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "org.scalacheck" %% "scalacheck" % "1.17.0" % Test
)

doctestTestFramework := DoctestTestFramework.ScalaCheck // Default value for doctestTestFramework

Using ScalaTest

If you are using ScalaTest, add the following lines to your build.sbt:

// ScalaTest 3.2
// ScalaTest 3.2 has been modularized. sbt-doctest generates tests using FunSpec.
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest-funspec" % "3.2.18" % Test,
  "org.scalatestplus" %% "scalacheck-1-17" % "3.2.18.0" % Test
)

// ScalaTest 3.1
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.1.2" % Test,
  "org.scalatestplus" %% "scalacheck-1-14" % "3.1.2.0" % Test
)

// ScalaTest 3.0
libraryDependencies ++= Seq(
  "org.scalatest"  %% "scalatest"  % "3.0.9"  % Test,
  "org.scalacheck" %% "scalacheck" % "1.14.0" % Test
)

doctestTestFramework := DoctestTestFramework.ScalaTest

Due to changes in the ScalaTest API, the test code generated will be slightly different depending on the version of ScalaTest. sbt-doctest automatically determines which test code to generate by looking at libraryDependencies.

If you want to explicitly specify the version of ScalaTest to be generated, you can specify doctestScalaTestVersion.

doctestScalaTestVersion := Some("3.2.15")

Using Specs2

If you are using Specs2, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "org.specs2" %% "specs2-scalacheck" % "4.20.5" % Test
)

doctestTestFramework := DoctestTestFramework.Specs2

Using Minitest

If you are using Minitest, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "io.monix" %% "minitest" % "2.9.5" % Test,
  "io.monix" %% "minitest-laws" % "2.9.6" % Test
)

doctestTestFramework := DoctestTestFramework.Minitest

Using µTest

If you are using µTest, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "com.lihaoyi" %% "utest" % "0.8.3" % Test
)

doctestTestFramework := DoctestTestFramework.MicroTest

Using MUnit

If you are using MUnit, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "org.scalameta" %% "munit" % "0.7.29" % Test
)

doctestTestFramework := DoctestTestFramework.Munit

In case you are configuring µTest or using a custom test framework, you can do something like this below in your build.sbt:

testFrameworks -= new TestFramework("utest.runner.Framework")
testFrameworks += new TestFramework("test.utest.MyCustomFramework")

which means that you are removing utest.runner.Framework inserted automatically for you by sbt-doctest and you are inserting your own custom test framework, provided by class test.utest.MyCustomFramework, in this example.

Caveats

There are still dependencies from ScalaTest and/or ScalaCheck when property checks are employed.

The difficulty can be circumvented for the time being by providing all dependencies in build.sbt, like shown in the example below which uses uTest with property checks, which require ScalaTest and ScalaCheck as well:

libraryDependencies ++= Seq(
  "com.lihaoyi"    %% "utest"      % "0.8.3"  % Test,
  "org.scalatest"  %% "scalatest"  % "3.0.9"  % Test,
  "org.scalacheck" %% "scalacheck" % "1.17.0" % Test
)
      
doctestTestFramework := DoctestTestFramework.MicroTest

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"

Ignoring Some Files

If you don't want to generate doctests for some of your sources, then specify a regex pattern:

doctestIgnoreRegex := Some(".*SomeClass.scala")

Source files with canonical paths (using UNIX separator - /) matching the regex, will not be used for doctest generation.

Only Code Blocks Mode

If you all you need is to check that code in Scaladoc code blocks (text inside {{{}}}) compiles), you can enable only code blocks mode by setting doctestOnlyCodeBlocksMode to true:

doctestOnlyCodeBlocksMode := true

Generated tests won't have any assertions, unless they are present in your Scaladocs.

HTML Entities

Often when documenting libraries that work with HTML you need to encode HTML entities so that they will be displayed in browsers.

However, sbt-doctest ignores these and attempts to compare encoded HTML with unencoded HTML entities. You can fix this by enabling decoding of HTML entities. Just add the following setting to your build.sbt:

doctestDecodeHtmlEntities := true

Now the following should pass:

  /**
   * {{{
   * >>> Main.html
   * &lt;html&gt;&lt;/html&gt;
   * }}}
   */
  val html = "<html></html>"

Markdown

Also supports code examples in Markdown documentation. To enable add the following to your build.sbt:

doctestMarkdownEnabled := true

Any code blocks that start with the ```scala markdown directive will be parsed. It searches *.md under baseDirectory by default. It can be configured by doctestMarkdownPathFinder.

// default
doctestMarkdownPathFinder := baseDirectory.value * "*.md"

// search doc/ recursively
doctestMarkdownPathFinder := baseDirectory.value * "doc" ** "*.md" 

See an example markdown.

Compatibility with other sbt plugins

If you happen to have other plugins that use scalameta (e.g. sbt-scalafmt) please make sure those plugins don't bring conflicting version of scalameta.

At this moment sbt-scalafmt need to be of version 1.6.x at least.

License

MIT

More Repositories

1

sbt-lock

Gemfile.lock for sbt
Scala
77
star
2

aichat

Integrate OpenAI Chat API with command line
Go
22
star
3

sbt-repeat

Repeat sbt command many times
Scala
11
star
4

appexception-logback

Throwable converter to show only my frames
Java
7
star
5

scalikejdbc-stream

scalikejdbc stream adapters
Scala
6
star
6

exhash

A hash of a stacktrace.
Java
6
star
7

sslreminder

Check expiration date of SSL certificates periodically, then remind you via email.
Go
6
star
8

corona-test-runner

Test runner for Corona SDK
Lua
4
star
9

play-filter-only

Utility to apply Play's EssentialFilter to only specific paths
Scala
4
star
10

lunkaku

ランカク
Python
4
star
11

homebrew-alt

Ruby
3
star
12

hipworktime

Ruby
3
star
13

psbridge

Java
2
star
14

durationkt

Small duration library for Kotlin
Kotlin
2
star
15

ddd-tools

Scala
2
star
16

ruby_redis_bitmap_metrics

Ruby
2
star
17

c-geohexv3

C implementation of geohex v3
Shell
2
star
18

play-logback-ui

Scala
2
star
19

apkuploader

Python
2
star
20

tabrss

http://tabrss.herokuapp.com/
Ruby
1
star
21

totp

Scala
1
star
22

go-geohexv3

golang implementation of geohexv3
Go
1
star
23

sbt-play-eb-docker

Scala
1
star
24

my-aichat-prompts

1
star
25

gotestloop

Utility command to run `go test` repeatedly
Go
1
star
26

estimer

timer porgram
Python
1
star
27

filecord

Go
1
star
28

tkawachi.github.com

http://tkawachi.github.io/
JavaScript
1
star
29

yad-server

1
star
30

future-throttle

Scala
1
star
31

free-as-in-freedom-review

ReVIEW formatted version of http://goo.gl/I4hWwe
Shell
1
star
32

mdedit

My Markdown editor
Scala
1
star
33

akka-utf8-decoder

Scala
1
star
34

bitcoin-exercise

Scala
1
star
35

hipbot

Go
1
star
36

byoyomi

将棋用秒読みタイマー
Kotlin
1
star
37

sqlite-scalikejdbc-test

Scala
1
star
38

hocon2json

A simple command to convert Typesafe config (including HOCON) to JSON.
Scala
1
star