Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
import org.codehaus.groovy.runtime.typehandling.*;

import static java.lang.Integer.parseInt;
import static java.util.Arrays.asList;

/**
Expand All @@ -41,6 +42,8 @@ public abstract class GroovyRuntimeUtil {
private static final String GET = "get";
private static final String IS = "is";

@Internal
public static final int MAJOR_VERSION = parseInt(GroovySystem.getVersion().split("\\.", 2)[0]);
public static Object[] EMPTY_ARGUMENTS = new Object[0];

public static boolean isTruthy(Object obj) {
Expand Down Expand Up @@ -329,26 +332,4 @@ public static Method toMethod(@Nullable MetaMethod metaMethod) {
CachedMethod cachedMethod = ObjectUtil.asInstance(metaMethod, CachedMethod.class);
return cachedMethod == null ? null : cachedMethod.getCachedMethod();
}

@Internal
public static boolean isGroovy2() {
return GroovySystem.getVersion().startsWith("2.");
}

@Internal
public static boolean isGroovy3orNewer() {
//Having isGroovy3() with just "startsWith("3.") there could be (in the future) no tests executed at all for Groovy 4
return !isGroovy2();
}

@Internal
public static boolean isGroovy3orOlder() {
return isGroovy2() || GroovySystem.getVersion().startsWith("3.");
}

@Internal
public static boolean isGroovy4orNewer() {
//Having isGroovy4() with just "startsWith("4.") there could be (in the future) no tests executed at all for Groovy 5
return !isGroovy3orOlder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import spock.lang.Specification

//Those tests requires Groovy 3.0.3+, see UsageOfNotYetImplementedJUnit4 for deprecated groovy.transform.NotYetImplemented tests
@Issue(["https://github.com/spockframework/spock/issues/1127", "https://issues.apache.org/jira/browse/GROOVY-9492"])
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
class UsageOfNotYetImplemented extends Specification {

@NotYetImplemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class DataSpec extends EmbeddedSpecification {
- `feature [a: 2, b: 4, c: 6, d: 1, e: 6, #3]`
end::data-providers-combined-result[]
'''
.stripIndent(*(GroovyRuntimeUtil.groovy3orNewer ? [true] : []))
.stripIndent(*((GroovyRuntimeUtil.MAJOR_VERSION >= 3) ? [true] : []))
.readLines()
.findAll {it.startsWith('-') }
.join('\n')
Expand Down Expand Up @@ -206,7 +206,7 @@ class DataSpec extends EmbeddedSpecification {
- `feature [a: 6, b: 6, c: 9, #5]`
end::single-data-providers-combined-result1[]
'''
.stripIndent(*(GroovyRuntimeUtil.groovy3orNewer ? [true] : []))
.stripIndent(*((GroovyRuntimeUtil.MAJOR_VERSION >= 3) ? [true] : []))
.readLines()
.findAll {it.startsWith('-') }
.join('\n')
Expand Down Expand Up @@ -251,7 +251,7 @@ class DataSpec extends EmbeddedSpecification {
- `feature [a: 2, b: 4, c: 6, #3]`
end::single-data-providers-combined-result2[]
'''
.stripIndent(*(GroovyRuntimeUtil.groovy3orNewer ? [true] : []))
.stripIndent(*((GroovyRuntimeUtil.MAJOR_VERSION >= 3) ? [true] : []))
.readLines()
.findAll {it.startsWith('-') }
.join('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ State.NEW
node.lastColumnNumber == 6
}

@Requires({ GroovyRuntimeUtil.isGroovy2() }) //lastColumnNumber value fixed in new parser in Groovy 3
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 }) //lastColumnNumber value fixed in new parser in Groovy 3
def "short-form class literals have accurate line/column info (Groovy 2)"() {
inspector.load("""
List
Expand Down Expand Up @@ -97,7 +97,7 @@ println( List )
node3.lastColumnNumber == 17 // should be: 15
}

@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "short-form class literals have accurate line/column info"() {
inspector.load("""
List
Expand Down Expand Up @@ -130,7 +130,7 @@ println( List )
node3.lastColumnNumber == 15
}

@Requires({ GroovyRuntimeUtil.isGroovy2() }) //column number changed in Groovy 3
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 }) //column number changed in Groovy 3
def "long-form class literals have accurate line/column info (Groovy 2)"() {
inspector.load("""
List.class
Expand All @@ -154,7 +154,7 @@ List.class.methods
node2.lastColumnNumber == 11
}

@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "long-form class literals have accurate line/column info"() {
inspector.load("""
List.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ Can not mock final classes with the following settings :
mySpy.accessNonStaticFlag()
}

@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "Access protected Groovy fields should still be accessible in Groovy 3&4 when @Internal annotation is not copied due to sub-class mock maker"() {
when:
AccessProtectedSubClass mySpy = Spy(mockMaker: mockito { s ->
Expand All @@ -331,7 +331,7 @@ Can not mock final classes with the following settings :
mySpy.accessStaticFlag()
}

@Requires({ GroovyRuntimeUtil.isGroovy2() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
def "Access protected Groovy fields should be accessible in Groovy 2 when @Internal annotation is not copied due to sub-class mock maker"() {
when:
AccessProtectedSubClass mySpy = Spy(mockMaker: mockito { s ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class AstSpec extends EmbeddedSpecification {
textSnapshotter.assertThat(result.source.normalize()).matchesSnapshot()
}

@Requires({ GroovyRuntimeUtil.groovy3orNewer })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "groovy 3 language features"() {
when:
def result = compiler.transpile('''
Expand Down Expand Up @@ -139,7 +139,7 @@ class Foo {
def "enums"() {
given:
// groovy 4 renders differently
def snapshotId = GroovyRuntimeUtil.groovy4orNewer ? "groovy4" : ""
def snapshotId = (GroovyRuntimeUtil.MAJOR_VERSION >= 4) ? "groovy4" : ""

when:
def result = compiler.transpile('''
Expand Down Expand Up @@ -346,7 +346,7 @@ class Ext <T extends Serializable, V extends Cloneable> {

def "Primitive types are used in AST transformation"() {
given:
def snapshotId = GroovyRuntimeUtil.groovy4orNewer ? "groovy4" : ""
def snapshotId = (GroovyRuntimeUtil.MAJOR_VERSION >= 4) ? "groovy4" : ""

when:
def result = compiler.transpileWithImports('''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ a[b]
}
}

@Requires({ GroovyRuntimeUtil.isGroovy2() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
def "PostfixExpression (Groovy 2)"() {
expect:
isRendered """
Expand All @@ -372,7 +372,7 @@ x++ == null
}
}

@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "PostfixExpression"() {
expect:
isRendered """
Expand Down Expand Up @@ -558,7 +558,7 @@ interface java.util.List
}
}

@Requires({ GroovyRuntimeUtil.isGroovy2() }) //comments are no longer included in power assertion's error message in Groovy 3
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 }) //comments are no longer included in power assertion's error message in Groovy 3
def "ClassExpression with dot-containing comments (Groovy 2)"() {
expect:
isRendered """
Expand All @@ -571,7 +571,7 @@ java.util./*.awt.*/List == java.lang.String // I. Like. Dots.
}
}

@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "ClassExpression with dot-containing comments"() {
expect:
isRendered """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ x == 123
}
}

@Requires({ GroovyRuntimeUtil.isGroovy2() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
def "values with same literal representations (Groovy 2)"() {
expect:
isRendered """
Expand All @@ -128,7 +128,7 @@ x == 123
}
}

@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "values with same literal representations"() {
expect:
isRendered """
Expand Down Expand Up @@ -207,4 +207,3 @@ x.equals(y)
boolean equals(other) { false }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ x $op 42
op << ["=", "+=", "-="]
}

@Requires({ GroovyRuntimeUtil.isGroovy2() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
def "assignments are not allowed in explicit conditions (Groovy 2)"() {
when:
compiler.compileFeatureBody("""
Expand All @@ -82,7 +82,7 @@ assert x $op 42
op << ["=", "+=", "-="]
}
@PendingFeatureIf(value = { GroovyRuntimeUtil.isGroovy3orNewer() }, exceptions = WrongExceptionThrownError,
@PendingFeatureIf(value = { GroovyRuntimeUtil.MAJOR_VERSION >= 3 }, exceptions = WrongExceptionThrownError,
reason = "+= and -= are allowed in Groovy 3, to be precised at Groovy side")
@Issue("https://issues.apache.org/jira/browse/GROOVY-9360")
def "assignment arithmetic operators are not allowed in explicit conditions"() {
Expand All @@ -102,7 +102,7 @@ assert x $op 42
op << ["+=", "-="]
}
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
def "assignments are not allowed in explicit conditions"() {
when:
compiler.compileFeatureBody("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class JavaMocksForGroovyClasses extends Specification {
1 * mockMe.setBar("value")
}

@Requires({ GroovyRuntimeUtil.isGroovy3orOlder() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 })
def "mock call to GroovyObject.getProperty Groovy 2&3"() {
when:
def value = mockMe.getProperty("bar")
Expand All @@ -97,7 +97,7 @@ class JavaMocksForGroovyClasses extends Specification {
https://github.com/spockframework/spock/pull/1717
where Groovy 4.0.7 changed the compilation to indy, which makes the mockMe.getProperty("bar") call not distinguishable from mockMe.bar
*/
@Requires({ GroovyRuntimeUtil.isGroovy4orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 })
def "mock call to GroovyObject.getProperty Groovy >=4.0.7"() {
when:
def value = mockMe.getProperty("bar")
Expand Down Expand Up @@ -287,7 +287,7 @@ class JavaMocksForGroovyClasses extends Specification {
}

@Issue("https://github.com/spockframework/spock/issues/1256")
@Requires({ GroovyRuntimeUtil.isGroovy3orOlder() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 })
def "Mock object boolean (get + is) accessor via dot-notation (Groovy 2&3)"() {
given:
ExampleData mockData = Mock(ExampleData)
Expand All @@ -308,7 +308,7 @@ class JavaMocksForGroovyClasses extends Specification {
* whereas Groovy 2 & 3 resolved the 'get'.
*/
@Issue("https://github.com/spockframework/spock/issues/1256")
@Requires({ GroovyRuntimeUtil.isGroovy4orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 })
def "Mock object boolean (get + is) accessor via dot-notation (Groovy 4)"() {
given:
ExampleData mockData = Mock(ExampleData)
Expand All @@ -324,15 +324,15 @@ class JavaMocksForGroovyClasses extends Specification {
result == "Data is current"
}

@Requires({ GroovyRuntimeUtil.isGroovy3orOlder() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 })
def "Real object boolean (get + is) accessor via dot-notation (Groovy 2&3)"() {
given:
def data = new ExampleData()
expect: 'The getActive() method returns true'
data.active
}

@Requires({ GroovyRuntimeUtil.isGroovy4orNewer() })
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 })
def "Real object boolean (get + is) accessor via dot-notation (Groovy 4)"() {
given:
def data = new ExampleData()
Expand Down