final class example extends Annotation with StaticAnnotation
Generates unit tests from examples in Scaladoc in files
.
Getting started
Suppose you have a source file src/main/scala/yourPackage/YourClass.scala
,
which contains some code examples in its Scaladoc.
You can run those examples as test cases with this library.
Step 1: Add this library as test dependency
Add the following code in your build.sbt
:
libraryDependencies += "com.thoughtworks.example" %% "example" % "latest.release" % Test
libraryDependencies += "org.scalatest" %% "scalatest" % "latest.release" % Test
addCompilerPlugin(("org.scalameta" % "paradise" % "3.0.0-M8").cross(CrossVersion.patch))
// paradise plugin should only be enabled in test configuration.
autoCompilerPlugins in Test := true
autoCompilerPlugins in Compile := false
Step 2: Create the test suite class
Create a source file at src/test/scala/yourPackage/YourClassSpec.scala
, with the following content:
import com.thoughtworks.example
@example("src/main/scala/yourPackage/YourClass.scala")
class YourClassSpec extends org.scalatest.FreeSpec
The @example
annotation will extract code in Scaladoc in src/main/scala/yourPackage/YourClass.scala
as a org.scalatest.FreeSpec
Step 3: Run tests
sbt test
You will notice that all code blocks inside
{{{
}}}
in Scaladoc comments in src/test/scala/yourPackage/YourClass.scala
are executed.
Common code
Code blocks before any Scaladoc tag are shared by all test cases. For example:
import org.scalatest._, Matchers._
Then Scalatest matchers will be available for all test cases.
Author:
杨博 (Yang Bo) <[email protected]>
- Annotations
- @compileTimeOnly( ... )
A code block under a Scaladoc tag is a test case. The test case is inside a org.scalatest.FreeSpec
this should be(a[FreeSpec])
, A code block may define variables.
val i = 1 val s = "text"
Those variables are accessible from other code blocks under the same Scaladoc tag.
i should be(1)
s should be("text")
- Note
A variable defined under a Scaladoc tag is not accessible from code blocks under another tags.
"i" shouldNot compile
- See also
- Alphabetic
- By Inheritance
- example
- StaticAnnotation
- Annotation
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
-
new
example(files: String*)
- files
Source files that contain Scaladoc to import
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (example, B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
apply(defn: Any): Any
Returns a class definition that contains unit cases imported from Scaladoc comments.
Returns a class definition that contains unit cases imported from Scaladoc comments.
- Annotations
- @compileTimeOnly( ... )
- Note
This method is the inline macro implementation of this annotation example.
"new example().apply(???)" shouldNot compile
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
- def ensuring(cond: (example) ⇒ Boolean, msg: ⇒ Any): example
- def ensuring(cond: (example) ⇒ Boolean): example
- def ensuring(cond: Boolean, msg: ⇒ Any): example
- def ensuring(cond: Boolean): example
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
- def formatted(fmtstr: String): String
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
- def →[B](y: B): (example, B)