sbt doctest

13

Click here to load reader

Upload: takashi-kawachi

Post on 28-Jun-2015

505 views

Category:

Engineering


7 download

TRANSCRIPT

Page 1: Sbt doctest

テストをもっと気軽に書こう sbt-doctest

@kawachi

Page 2: Sbt doctest

Python には doctest がある

• コメント内にある文字列を使ってテスト

• これが一番簡単そう

"""!This is the "example" module.!!The example module supplies one function, factorial(). For example,!!>>> factorial(5)!120!"""

Page 3: Sbt doctest

“scala doctest”で検索• 「だれか作ってるでしょ」

Page 4: Sbt doctest

でもこれ古い

• 2010年から更新されていない

Page 5: Sbt doctest

自分で作ってみる

• パターン見つけてテストつくるだけだし、自分でもなんとかなるんじゃないか

• sbt と組み合わせて sbt test のタイミングで生成するようにしたい

Page 6: Sbt doctest

できましたー

• http://github.com/tkawachi/sbt-doctest

Page 7: Sbt doctest

やってること1. scaladoc で使っている parser を使って ドキュメントを取り出す

2. parser combinator を使って ドキュメントに含まれているテストを取り出す

3. テストをファイルに保存する

以上を sourceGenerators in Test で実行する sbt plugin を作成。

Page 8: Sbt doctest

Python style とscala repl style をサポート

• タイプ数が少ないのは Python style だよなー

• console で実行してから貼り付けたい場合もある

• → とりあえず両方?

/**! * # Python style! * >>> Test.f(10)! * 20! *! * # Scala repl style! * scala> Test.f(20)! * res1: Int = 40! */

Page 9: Sbt doctest

scaladoc との兼ね合い

• scaladoc が崩れるのを防ぐには {{{ }}} 内で書くと良さそう

• {{{ }}} 内でなくてもテストは動く

/**! * {{{! * # Python style! * >>> Test.f(10)! * 20! *! * # Scala repl style! * scala> Test.f(20)! * res1: Int = 40! * }}}! */

Page 10: Sbt doctest

Import できます/**! * {{{! * # Python style! * >>> import Test.f! * >>> f(10)! * 20! *! * # Scala repl style! * scala> import Test.f2! * scala> f2(20)! * res1: Int = 40! * }}}! */

Page 11: Sbt doctest

haskell-doctest からprop> をインスパイア

• ScalaCheck のテストを生成

/**! * {{{! * prop> (i: Int) => i + i should === i * 2! * }}}! */

Page 12: Sbt doctest

今後の予定

• 複数行のテスト

• sonatype へリリース

Page 13: Sbt doctest

ご感想をお待ちしています

• http://github.com/tkawachi/sbt-doctest