diff --git a/.scalafmt.conf b/.scalafmt.conf index 77352ea..f726136 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,3 +1,3 @@ runner.dialect = "scala212" -version = "3.4.3" +version = "3.5.0" maxColumn = 80 diff --git a/secret.sbt b/secret.sbt index bb264c7..3014621 100644 --- a/secret.sbt +++ b/secret.sbt @@ -5,10 +5,15 @@ lazy val secret = { IO.delete(secretDirectory) org.eclipse.jgit.api.Git .cloneRepository() - .setURI("https://github.com/ThoughtWorksInc/tw-data-china-continuous-delivery-password.git") + .setURI( + "https://github.com/ThoughtWorksInc/tw-data-china-continuous-delivery-password.git" + ) .setDirectory(secretDirectory) .setCredentialsProvider( - new org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider(token, "") + new org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider( + token, + "" + ) ) .call() .close() diff --git a/src/main/scala/com/thoughtworks/Example.scala b/src/main/scala/com/thoughtworks/Example.scala index 2cfa8d3..eb15339 100644 --- a/src/main/scala/com/thoughtworks/Example.scala +++ b/src/main/scala/com/thoughtworks/Example.scala @@ -17,26 +17,29 @@ import scala.reflect.NameTransformer * * =Getting started= * - * Suppose you have some source files under `src/main/scala`, which contain some code examples in their Scaladoc. You - * can run those examples as test cases with this library. + * Suppose you have some source files under `src/main/scala`, which contain + * some code examples in their Scaladoc. You can run those examples as test + * cases with this library. * * ==Step 1: Add this plug-in in your sbt settings== * - * `
// project/plugins.sbt addSbtPlugin("com.thoughtworks.example" % "sbt-example" % "latest.release")` + * `
// project/plugins.sbt addSbtPlugin("com.thoughtworks.example" % + * "sbt-example" % "latest.release")` * - * `
// build.sbt enablePlugins(Example) libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % Test - *` + * `
// build.sbt enablePlugins(Example) libraryDependencies += + * "org.scalatest" %% "scalatest" % "3.2.10" % Test` * * ==Step 2: Run tests== * * `
sbt test` * - * You will notice that all code blocks inside
{{{ }}}
in Scaladoc comments under `src/main/scala` are
- * executed.
+ * You will notice that all code blocks inside {{{ }}}
in Scaladoc
+ * comments under `src/main/scala` are executed.
*
* =Common code=
*
- * Code blocks before any Scaladoc tag are shared by all test cases. For example:
+ * Code blocks before any Scaladoc tag are shared by all test cases. For
+ * example:
*
* {{{
* import org.scalatest.freespec.AnyFreeSpec
@@ -45,7 +48,8 @@ import scala.reflect.NameTransformer
* Then the name `FreeSpec` will be available for all test cases.
*
* @note
- * A variable defined under a Scaladoc tag is not accessible from code blocks under another tag.
+ * A variable defined under a Scaladoc tag is not accessible from code blocks
+ * under another tag.
*
* {{{
* "i" shouldNot compile
@@ -58,7 +62,8 @@ import scala.reflect.NameTransformer
* val s = "text"
* }}}
*
- * Those variables are accessible from other code blocks under the same Scaladoc tag.
+ * Those variables are accessible from other code blocks under the same
+ * Scaladoc tag.
*
* {{{
* i should be(1)
@@ -84,27 +89,38 @@ import scala.reflect.NameTransformer
*/
object Example extends AutoPlugin {
- private def exampleStats(source: Source, logger: Logger, testDialect: Dialect): Seq[Stat] = {
+ private def exampleStats(
+ source: Source,
+ logger: Logger,
+ testDialect: Dialect
+ ): Seq[Stat] = {
val comments = AssociatedComments(source)
def scaladocTestTree(leadingComments: Set[Token.Comment]): List[Stat] = {
leadingComments.toList.flatMap { comment =>
ScaladocParser.parseScaladoc(comment).toSeq.flatMap { scaladoc =>
- val (code, trailing, tags) = scaladoc.foldRight[(List[Stat], List[Stat], List[Stat])]((Nil, Nil, Nil)) {
- case (
- DocToken(DocToken.CodeBlock, None, Some(codeBlock)),
- (codeAccumulator, trailingAccumulator, tagAccumulator)
- ) =>
- val Term.Block(stats) =
- new ScalametaParser(Input.String("{\n" + codeBlock + "\n}"))(testDialect)
- .parseStat()
- (stats ++: codeAccumulator, trailingAccumulator, tagAccumulator)
- case (
- DocToken(tagKind: DocToken.TagKind, Some(name), Some(description)),
- (codeAccumulator, trailingAccumulator, tagAccumulator)
- ) =>
- if (codeAccumulator.nonEmpty) {
- val tag = q"""
+ val (code, trailing, tags) = scaladoc
+ .foldRight[(List[Stat], List[Stat], List[Stat])]((Nil, Nil, Nil)) {
+ case (
+ DocToken(DocToken.CodeBlock, None, Some(codeBlock)),
+ (codeAccumulator, trailingAccumulator, tagAccumulator)
+ ) =>
+ val Term.Block(stats) =
+ new ScalametaParser(Input.String("{\n" + codeBlock + "\n}"))(
+ testDialect
+ )
+ .parseStat()
+ (stats ++: codeAccumulator, trailingAccumulator, tagAccumulator)
+ case (
+ DocToken(
+ tagKind: DocToken.TagKind,
+ Some(name),
+ Some(description)
+ ),
+ (codeAccumulator, trailingAccumulator, tagAccumulator)
+ ) =>
+ if (codeAccumulator.nonEmpty) {
+ val tag = q"""
${Lit.String(s"${tagKind.label} $name")}.in(try {
this.markup($description)
..$codeAccumulator
@@ -112,98 +128,109 @@ object Example extends AutoPlugin {
..$trailingAccumulator
})
"""
- (Nil, Nil, tag :: tagAccumulator)
- } else {
- (Nil, Nil, tagAccumulator)
- }
- case (
- DocToken(tagKind: DocToken.TagKind, None, Some(body)),
- (codeAccumulator, trailingAccumulator, tagAccumulator)
- ) =>
- if (codeAccumulator.nonEmpty) {
- val tag = q"""
+ (Nil, Nil, tag :: tagAccumulator)
+ } else {
+ (Nil, Nil, tagAccumulator)
+ }
+ case (
+ DocToken(tagKind: DocToken.TagKind, None, Some(body)),
+ (codeAccumulator, trailingAccumulator, tagAccumulator)
+ ) =>
+ if (codeAccumulator.nonEmpty) {
+ val tag = q"""
${Lit.String(s"${tagKind.label} $body")}.in(try {
..$codeAccumulator
} finally {
..$trailingAccumulator
})
"""
- (Nil, Nil, tag :: tagAccumulator)
- } else {
- (Nil, Nil, tagAccumulator)
- }
- case (
- token @ DocToken(_: DocToken.TagKind, _, None),
- (codeAccumulator, trailingAccumulator, tagAccumulator)
- ) =>
- if (codeAccumulator.nonEmpty) {
- val tag = q"""
+ (Nil, Nil, tag :: tagAccumulator)
+ } else {
+ (Nil, Nil, tagAccumulator)
+ }
+ case (
+ token @ DocToken(_: DocToken.TagKind, _, None),
+ (codeAccumulator, trailingAccumulator, tagAccumulator)
+ ) =>
+ if (codeAccumulator.nonEmpty) {
+ val tag = q"""
${Lit.String(token.toString)}.in(try {
..$codeAccumulator
} finally {
..$trailingAccumulator
})
"""
- (Nil, Nil, tag :: tagAccumulator)
- } else {
- (Nil, Nil, tagAccumulator)
- }
- case (
- DocToken(DocToken.Description, None, Some(text)),
- (codeAccumulator, trailingAccumulator, tagAccumulator)
- ) if text.startsWith("@") =>
- logger.warn(
- s"Invalid Scaladoc tag detected at ${comment.pos} (missing parameters for the tag?): \n\t$text"
- )
- if (codeAccumulator.nonEmpty) {
- val tag = q"""
+ (Nil, Nil, tag :: tagAccumulator)
+ } else {
+ (Nil, Nil, tagAccumulator)
+ }
+ case (
+ DocToken(DocToken.Description, None, Some(text)),
+ (codeAccumulator, trailingAccumulator, tagAccumulator)
+ ) if text.startsWith("@") =>
+ logger.warn(
+ s"Invalid Scaladoc tag detected at ${comment.pos} (missing parameters for the tag?): \n\t$text"
+ )
+ if (codeAccumulator.nonEmpty) {
+ val tag = q"""
${Lit.String(text)}.in(try {
..$codeAccumulator
} finally {
..$trailingAccumulator
})
"""
- (Nil, Nil, tag :: tagAccumulator)
- } else {
- (Nil, Nil, tagAccumulator)
- }
- case (DocToken(DocToken.Paragraph, None, None), accumulators) =>
- accumulators
- case (otherToken, (codeAccumulator, trailingAccumulator, tagAccumulator)) =>
- val tokenXml = otherToken match {
- case DocToken(DocToken.InheritDoc, None, None) =>
- "@inheritdoc"
- case DocToken(DocToken.Paragraph, None, Some(text)) =>
- {text}
- case DocToken(DocToken.Heading1, None, Some(text)) => -{text}
+ case DocToken(DocToken.Heading1, None, Some(text)) => +