File tree Expand file tree Collapse file tree 4 files changed +39
-18
lines changed
src/partest/scala/tools/partest Expand file tree Collapse file tree 4 files changed +39
-18
lines changed Original file line number Diff line number Diff line change 66package scala .tools .partest
77
88import scala .tools .nsc ._
9+ import settings .ScalaVersion
910import io .Directory
1011import util .{ SourceFile , BatchSourceFile , CommandLineParser }
1112import reporters .{Reporter , ConsoleReporter }
@@ -101,4 +102,30 @@ abstract class DirectTest extends App {
101102 final def log (msg : => Any ) {
102103 if (isDebug) Console .err println msg
103104 }
105+
106+ /**
107+ * Run a test only if the current java version is at least the version specified.
108+ */
109+ def testUnderJavaAtLeast [A ](version : String )(yesRun : => A ) = new TestUnderJavaAtLeast (version, { yesRun })
110+
111+ class TestUnderJavaAtLeast [A ](version : String , yesRun : => A ) {
112+ val javaVersion = System .getProperty(" java.specification.version" )
113+
114+ // the "ScalaVersion" class parses Java specification versions just fine
115+ val requiredJavaVersion = ScalaVersion (version)
116+ val executingJavaVersion = ScalaVersion (javaVersion)
117+ val shouldRun = executingJavaVersion >= requiredJavaVersion
118+ val preamble = if (shouldRun) " Attempting" else " Doing fallback for"
119+
120+ def logInfo () = log(s " $preamble java $version specific test under java version $javaVersion" )
121+
122+ /*
123+ * If the current java version is at least 'version' then 'yesRun' is evaluated
124+ * otherwise 'fallback' is
125+ */
126+ def otherwise (fallback : => A ): A = {
127+ logInfo()
128+ if (shouldRun) yesRun else fallback
129+ }
130+ }
104131}
Original file line number Diff line number Diff line change @@ -112,12 +112,12 @@ object Driver {
112112 System .setErr(System .out)
113113 try {
114114 // this test is only valid under JDK 1.7+
115- // cheat a little by using 'ScalaVersion' because it can parse java versions just as well
116- val requiredJavaVersion = ScalaVersion (" 1.7" )
117- val executingJavaVersion = ScalaVersion (System .getProperty(" java.specification.version" ))
118- if (executingJavaVersion >= requiredJavaVersion) {
115+ testUnderJavaAtLeast(" 1.7" ) {
119116 generateClass()
120117 compile()
118+ ()
119+ } otherwise {
120+ ()
121121 }
122122 }
123123 finally
Original file line number Diff line number Diff line change @@ -61,17 +61,14 @@ class Driver extends HasDefaultMethod {
6161 System .setErr(System .out)
6262 try {
6363 // this test is only valid under JDK 1.8+
64- // cheat a little by using 'ScalaVersion' because it can parse java versions just as well
65- val requiredJavaVersion = ScalaVersion (" 1.8" )
66- val executingJavaVersion = ScalaVersion (System .getProperty(" java.specification.version" ))
67- if (executingJavaVersion >= requiredJavaVersion) {
64+ testUnderJavaAtLeast(" 1.8" ) {
6865 generateInterface()
6966 compile()
7067 Class .forName(" Driver" ).newInstance()
71- } else {
72- // under other versions just dump the expected results
68+ ()
69+ } otherwise {
7370 println(" hello from publicMethod" )
74- println(" hello from staticMethod" )
71+ println(" hello from staticMethod" )
7572 }
7673 }
7774 finally
Original file line number Diff line number Diff line change @@ -3,14 +3,11 @@ import scala.tools.partest._
33object Test extends CompilerTest {
44 import global ._
55
6- def javaVersion = scala.util.Properties .javaVersion
7- def isJavaEight = javaVersion startsWith " 1.8"
86 // This way we auto-pass on non-java8 since there's nothing to check
9- override lazy val units = {
10- val res : List [CompilationUnit ] = if (isJavaEight) javaCompilationUnits(global)(defaultMethodSource) else Nil
11- val word = if (isJavaEight) " Attempting" else " Skipping"
12- log(s " $word java8-specific test under java version $javaVersion" )
13- res
7+ override lazy val units : List [CompilationUnit ] = testUnderJavaAtLeast(" 1.8" ) {
8+ javaCompilationUnits(global)(defaultMethodSource)
9+ } otherwise {
10+ Nil
1411 }
1512
1613 private def defaultMethodSource = """
You can’t perform that action at this time.
0 commit comments