-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Three handy predicates to check classpath (#70)
- Loading branch information
Showing
6 changed files
with
247 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/scala/com/thoughtworks/EnableWithArtifactTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.thoughtworks | ||
|
||
import org.scalatest._ | ||
import enableIf._ | ||
|
||
import scala.util.control.TailCalls._ | ||
import org.scalatest.freespec.AnyFreeSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
|
||
/** | ||
* @author 沈达 (Darcy Shen) <[email protected]> | ||
*/ | ||
class EnableWithArtifactTest extends AnyFreeSpec with Matchers { | ||
"test the constant regex of classpath" in { | ||
assert { | ||
"/path/to/scala-library-2.10.8.jar" match { | ||
case classpathRegex(_, artifactId, version) => | ||
"scala-library".equals(artifactId) && "2.10.8".equals(version) | ||
} | ||
} | ||
assert { | ||
"/path/to/quasiquotes_2.10-2.1.1.jar" match { | ||
case classpathRegex(_, artifactId, version) => | ||
"quasiquotes_2.10".equals(artifactId) && "2.1.1".equals(version) | ||
} | ||
} | ||
} | ||
|
||
"Test if we are using quasiquotes explicitly" in { | ||
|
||
object ExplicitQ { | ||
|
||
@enableIf(classpathMatchesArtifact(crossScalaBinaryVersion("quasiquotes"), "2.1.1")) | ||
def whichIsEnabled = "good" | ||
} | ||
object ImplicitQ { | ||
@enableIf(classpathMatches(".*scala-library-2\\.1[123]\\..*".r)) | ||
def whichIsEnabled = "bad" | ||
|
||
@enableIf(classpathMatches(".*scala-2\\.1[123]\\..*".r)) | ||
def whichIsEnabled = "bad" | ||
} | ||
|
||
|
||
import ExplicitQ._ | ||
import ImplicitQ._ | ||
if (scala.util.Properties.versionNumberString < "2.11") { | ||
assert(whichIsEnabled == "good") | ||
} else { | ||
assert(whichIsEnabled == "bad") | ||
} | ||
} | ||
|
||
"Add TailRec.flatMap for Scala 2.10 " in { | ||
|
||
@enableIf(classpathMatches(".*scala-library-2\\.10.*".r)) | ||
implicit class FlatMapForTailRec[A](underlying: TailRec[A]) { | ||
final def flatMap[B](f: A => TailRec[B]): TailRec[B] = { | ||
tailcall(f(underlying.result)) | ||
} | ||
} | ||
|
||
def ten = done(10) | ||
|
||
def tenPlusOne = ten.flatMap(i => done(i + 1)) | ||
|
||
assert(tenPlusOne.result == 11) | ||
} | ||
|
||
"Add TailRec.flatMap for Scala 2.10 via classpathContains " in { | ||
|
||
@enableIf(classpathContains("scala-library-2.10.")) | ||
implicit class FlatMapForTailRec[A](underlying: TailRec[A]) { | ||
final def flatMap[B](f: A => TailRec[B]): TailRec[B] = { | ||
tailcall(f(underlying.result)) | ||
} | ||
} | ||
|
||
def ten = done(10) | ||
|
||
def tenPlusOne = ten.flatMap(i => done(i + 1)) | ||
|
||
assert(tenPlusOne.result == 11) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/scala/com/thoughtworks/EnableWithClasspathTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.thoughtworks | ||
|
||
import org.scalatest._ | ||
import enableIf._ | ||
|
||
import scala.util.control.TailCalls._ | ||
import org.scalatest.freespec.AnyFreeSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
|
||
/** | ||
* @author 沈达 (Darcy Shen) <[email protected]> | ||
*/ | ||
class EnableWithClasspathTest extends AnyFreeSpec with Matchers { | ||
|
||
"enableWithClasspath by regex" in { | ||
|
||
object ShouldEnable { | ||
|
||
@enableIf(classpathMatches(".*scala.*".r)) | ||
def whichIsEnabled = "good" | ||
|
||
} | ||
object ShouldDisable { | ||
|
||
@enableIf(classpathMatches(".*should_not_exist.*".r)) | ||
def whichIsEnabled = "bad" | ||
} | ||
|
||
import ShouldEnable._ | ||
import ShouldDisable._ | ||
assert(whichIsEnabled == "good") | ||
|
||
} | ||
|
||
"Add TailRec.flatMap for Scala 2.10 " in { | ||
|
||
@enableIf(classpathMatches(".*scala-library-2.10.*".r)) | ||
implicit class FlatMapForTailRec[A](underlying: TailRec[A]) { | ||
final def flatMap[B](f: A => TailRec[B]): TailRec[B] = { | ||
tailcall(f(underlying.result)) | ||
} | ||
} | ||
|
||
def ten = done(10) | ||
|
||
def tenPlusOne = ten.flatMap(i => done(i + 1)) | ||
|
||
assert(tenPlusOne.result == 11) | ||
} | ||
} |