Skip to content

Commit 68e16fb

Browse files
committed
Replace GroovyRuntimeUtil.groovy... methods by a Groovy major version constant
1 parent 7a17113 commit 68e16fb

File tree

10 files changed

+31
-51
lines changed

10 files changed

+31
-51
lines changed

spock-core/src/main/java/org/spockframework/runtime/GroovyRuntimeUtil.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
2828
import org.codehaus.groovy.runtime.typehandling.*;
2929

30+
import static java.lang.Integer.parseInt;
3031
import static java.util.Arrays.asList;
3132

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

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

4649
public static boolean isTruthy(Object obj) {
@@ -329,26 +332,4 @@ public static Method toMethod(@Nullable MetaMethod metaMethod) {
329332
CachedMethod cachedMethod = ObjectUtil.asInstance(metaMethod, CachedMethod.class);
330333
return cachedMethod == null ? null : cachedMethod.getCachedMethod();
331334
}
332-
333-
@Internal
334-
public static boolean isGroovy2() {
335-
return GroovySystem.getVersion().startsWith("2.");
336-
}
337-
338-
@Internal
339-
public static boolean isGroovy3orNewer() {
340-
//Having isGroovy3() with just "startsWith("3.") there could be (in the future) no tests executed at all for Groovy 4
341-
return !isGroovy2();
342-
}
343-
344-
@Internal
345-
public static boolean isGroovy3orOlder() {
346-
return isGroovy2() || GroovySystem.getVersion().startsWith("3.");
347-
}
348-
349-
@Internal
350-
public static boolean isGroovy4orNewer() {
351-
//Having isGroovy4() with just "startsWith("4.") there could be (in the future) no tests executed at all for Groovy 5
352-
return !isGroovy3orOlder();
353-
}
354335
}

spock-specs/src/test-groovy-ge-3.0/groovy/org/spockframework/smoke/UsageOfNotYetImplemented.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import spock.lang.Specification
1010

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

1616
@NotYetImplemented

spock-specs/src/test/groovy/org/spockframework/docs/datadriven/DataSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class DataSpec extends EmbeddedSpecification {
111111
- `feature [a: 2, b: 4, c: 6, d: 1, e: 6, #3]`
112112
end::data-providers-combined-result[]
113113
'''
114-
.stripIndent(*(GroovyRuntimeUtil.groovy3orNewer ? [true] : []))
114+
.stripIndent(*((GroovyRuntimeUtil.MAJOR_VERSION >= 3) ? [true] : []))
115115
.readLines()
116116
.findAll {it.startsWith('-') }
117117
.join('\n')
@@ -206,7 +206,7 @@ class DataSpec extends EmbeddedSpecification {
206206
- `feature [a: 6, b: 6, c: 9, #5]`
207207
end::single-data-providers-combined-result1[]
208208
'''
209-
.stripIndent(*(GroovyRuntimeUtil.groovy3orNewer ? [true] : []))
209+
.stripIndent(*((GroovyRuntimeUtil.MAJOR_VERSION >= 3) ? [true] : []))
210210
.readLines()
211211
.findAll {it.startsWith('-') }
212212
.join('\n')
@@ -251,7 +251,7 @@ class DataSpec extends EmbeddedSpecification {
251251
- `feature [a: 2, b: 4, c: 6, #3]`
252252
end::single-data-providers-combined-result2[]
253253
'''
254-
.stripIndent(*(GroovyRuntimeUtil.groovy3orNewer ? [true] : []))
254+
.stripIndent(*((GroovyRuntimeUtil.MAJOR_VERSION >= 3) ? [true] : []))
255255
.readLines()
256256
.findAll {it.startsWith('-') }
257257
.join('\n')

spock-specs/src/test/groovy/org/spockframework/groovy/SourcePositionPhaseSemanticAnalysis.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ State.NEW
6464
node.lastColumnNumber == 6
6565
}
6666

67-
@Requires({ GroovyRuntimeUtil.isGroovy2() }) //lastColumnNumber value fixed in new parser in Groovy 3
67+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 }) //lastColumnNumber value fixed in new parser in Groovy 3
6868
def "short-form class literals have accurate line/column info (Groovy 2)"() {
6969
inspector.load("""
7070
List
@@ -97,7 +97,7 @@ println( List )
9797
node3.lastColumnNumber == 17 // should be: 15
9898
}
9999
100-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
100+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
101101
def "short-form class literals have accurate line/column info"() {
102102
inspector.load("""
103103
List
@@ -130,7 +130,7 @@ println( List )
130130
node3.lastColumnNumber == 15
131131
}
132132
133-
@Requires({ GroovyRuntimeUtil.isGroovy2() }) //column number changed in Groovy 3
133+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 }) //column number changed in Groovy 3
134134
def "long-form class literals have accurate line/column info (Groovy 2)"() {
135135
inspector.load("""
136136
List.class
@@ -154,7 +154,7 @@ List.class.methods
154154
node2.lastColumnNumber == 11
155155
}
156156
157-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
157+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
158158
def "long-form class literals have accurate line/column info"() {
159159
inspector.load("""
160160
List.class

spock-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoMockMakerSpec.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Can not mock final classes with the following settings :
317317
mySpy.accessNonStaticFlag()
318318
}
319319

320-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
320+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
321321
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"() {
322322
when:
323323
AccessProtectedSubClass mySpy = Spy(mockMaker: mockito { s ->
@@ -331,7 +331,7 @@ Can not mock final classes with the following settings :
331331
mySpy.accessStaticFlag()
332332
}
333333

334-
@Requires({ GroovyRuntimeUtil.isGroovy2() })
334+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
335335
def "Access protected Groovy fields should be accessible in Groovy 2 when @Internal annotation is not copied due to sub-class mock maker"() {
336336
when:
337337
AccessProtectedSubClass mySpy = Spy(mockMaker: mockito { s ->

spock-specs/src/test/groovy/org/spockframework/smoke/ast/AstSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class AstSpec extends EmbeddedSpecification {
104104
textSnapshotter.assertThat(result.source.normalize()).matchesSnapshot()
105105
}
106106
107-
@Requires({ GroovyRuntimeUtil.groovy3orNewer })
107+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
108108
def "groovy 3 language features"() {
109109
when:
110110
def result = compiler.transpile('''
@@ -139,7 +139,7 @@ class Foo {
139139
def "enums"() {
140140
given:
141141
// groovy 4 renders differently
142-
def snapshotId = GroovyRuntimeUtil.groovy4orNewer ? "groovy4" : ""
142+
def snapshotId = (GroovyRuntimeUtil.MAJOR_VERSION >= 4) ? "groovy4" : ""
143143
144144
when:
145145
def result = compiler.transpile('''
@@ -346,7 +346,7 @@ class Ext <T extends Serializable, V extends Cloneable> {
346346
347347
def "Primitive types are used in AST transformation"() {
348348
given:
349-
def snapshotId = GroovyRuntimeUtil.groovy4orNewer ? "groovy4" : ""
349+
def snapshotId = (GroovyRuntimeUtil.MAJOR_VERSION >= 4) ? "groovy4" : ""
350350
351351
when:
352352
def result = compiler.transpileWithImports('''

spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionRendering.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ a[b]
359359
}
360360
}
361361

362-
@Requires({ GroovyRuntimeUtil.isGroovy2() })
362+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
363363
def "PostfixExpression (Groovy 2)"() {
364364
expect:
365365
isRendered """
@@ -372,7 +372,7 @@ x++ == null
372372
}
373373
}
374374

375-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
375+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
376376
def "PostfixExpression"() {
377377
expect:
378378
isRendered """
@@ -558,7 +558,7 @@ interface java.util.List
558558
}
559559
}
560560

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

574-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
574+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
575575
def "ClassExpression with dot-containing comments"() {
576576
expect:
577577
isRendered """

spock-specs/src/test/groovy/org/spockframework/smoke/condition/EqualityComparisonRendering.groovy

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ x == 123
115115
}
116116
}
117117

118-
@Requires({ GroovyRuntimeUtil.isGroovy2() })
118+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
119119
def "values with same literal representations (Groovy 2)"() {
120120
expect:
121121
isRendered """
@@ -128,7 +128,7 @@ x == 123
128128
}
129129
}
130130
131-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
131+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
132132
def "values with same literal representations"() {
133133
expect:
134134
isRendered """
@@ -207,4 +207,3 @@ x.equals(y)
207207
boolean equals(other) { false }
208208
}
209209
}
210-

spock-specs/src/test/groovy/org/spockframework/smoke/condition/InvalidConditions.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ x $op 42
6464
op << ["=", "+=", "-="]
6565
}
6666

67-
@Requires({ GroovyRuntimeUtil.isGroovy2() })
67+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION == 2 })
6868
def "assignments are not allowed in explicit conditions (Groovy 2)"() {
6969
when:
7070
compiler.compileFeatureBody("""
@@ -82,7 +82,7 @@ assert x $op 42
8282
op << ["=", "+=", "-="]
8383
}
8484
85-
@PendingFeatureIf(value = { GroovyRuntimeUtil.isGroovy3orNewer() }, exceptions = WrongExceptionThrownError,
85+
@PendingFeatureIf(value = { GroovyRuntimeUtil.MAJOR_VERSION >= 3 }, exceptions = WrongExceptionThrownError,
8686
reason = "+= and -= are allowed in Groovy 3, to be precised at Groovy side")
8787
@Issue("https://issues.apache.org/jira/browse/GROOVY-9360")
8888
def "assignment arithmetic operators are not allowed in explicit conditions"() {
@@ -102,7 +102,7 @@ assert x $op 42
102102
op << ["+=", "-="]
103103
}
104104
105-
@Requires({ GroovyRuntimeUtil.isGroovy3orNewer() })
105+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 3 })
106106
def "assignments are not allowed in explicit conditions"() {
107107
when:
108108
compiler.compileFeatureBody("""

spock-specs/src/test/groovy/org/spockframework/smoke/mock/JavaMocksForGroovyClasses.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class JavaMocksForGroovyClasses extends Specification {
7272
1 * mockMe.setBar("value")
7373
}
7474

75-
@Requires({ GroovyRuntimeUtil.isGroovy3orOlder() })
75+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 })
7676
def "mock call to GroovyObject.getProperty Groovy 2&3"() {
7777
when:
7878
def value = mockMe.getProperty("bar")
@@ -97,7 +97,7 @@ class JavaMocksForGroovyClasses extends Specification {
9797
https://github.com/spockframework/spock/pull/1717
9898
where Groovy 4.0.7 changed the compilation to indy, which makes the mockMe.getProperty("bar") call not distinguishable from mockMe.bar
9999
*/
100-
@Requires({ GroovyRuntimeUtil.isGroovy4orNewer() })
100+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 })
101101
def "mock call to GroovyObject.getProperty Groovy >=4.0.7"() {
102102
when:
103103
def value = mockMe.getProperty("bar")
@@ -287,7 +287,7 @@ class JavaMocksForGroovyClasses extends Specification {
287287
}
288288
289289
@Issue("https://github.com/spockframework/spock/issues/1256")
290-
@Requires({ GroovyRuntimeUtil.isGroovy3orOlder() })
290+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 })
291291
def "Mock object boolean (get + is) accessor via dot-notation (Groovy 2&3)"() {
292292
given:
293293
ExampleData mockData = Mock(ExampleData)
@@ -308,7 +308,7 @@ class JavaMocksForGroovyClasses extends Specification {
308308
* whereas Groovy 2 & 3 resolved the 'get'.
309309
*/
310310
@Issue("https://github.com/spockframework/spock/issues/1256")
311-
@Requires({ GroovyRuntimeUtil.isGroovy4orNewer() })
311+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 })
312312
def "Mock object boolean (get + is) accessor via dot-notation (Groovy 4)"() {
313313
given:
314314
ExampleData mockData = Mock(ExampleData)
@@ -324,15 +324,15 @@ class JavaMocksForGroovyClasses extends Specification {
324324
result == "Data is current"
325325
}
326326

327-
@Requires({ GroovyRuntimeUtil.isGroovy3orOlder() })
327+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION <= 3 })
328328
def "Real object boolean (get + is) accessor via dot-notation (Groovy 2&3)"() {
329329
given:
330330
def data = new ExampleData()
331331
expect: 'The getActive() method returns true'
332332
data.active
333333
}
334334
335-
@Requires({ GroovyRuntimeUtil.isGroovy4orNewer() })
335+
@Requires({ GroovyRuntimeUtil.MAJOR_VERSION >= 4 })
336336
def "Real object boolean (get + is) accessor via dot-notation (Groovy 4)"() {
337337
given:
338338
def data = new ExampleData()

0 commit comments

Comments
 (0)