Skip to content

Commit

Permalink
fix: support for spock1.3-groovy2.4. fixes #112
Browse files Browse the repository at this point in the history
  • Loading branch information
joke committed Nov 26, 2021
1 parent 756579a commit cb01ead
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
}

dependencies {
testImplementation platform('org.codehaus.groovy:groovy-bom:3.0.9')
testImplementation platform('org.spockframework:spock-bom:2.0-groovy-3.0')
testImplementation platform('org.codehaus.groovy:groovy-bom:2.4.21')
testImplementation platform('org.spockframework:spock-bom:1.3-groovy-2.4')

testImplementation project(':spock-mockable')
testImplementation 'org.spockframework:spock-core'
Expand Down
18 changes: 18 additions & 0 deletions examples/spock13-groovy25/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id 'java'
id 'groovy'
}

dependencies {
testImplementation platform('org.codehaus.groovy:groovy-bom:2.5.15')
testImplementation platform('org.spockframework:spock-bom:1.3-groovy-2.5')

testImplementation project(':spock-mockable')
testImplementation 'org.spockframework:spock-core'
testImplementation 'org.objenesis:objenesis:3.2'
testImplementation 'org.slf4j:slf4j-simple:1.7.32'
}

test {
forkEvery 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.joke.spockmockable;

public final class Person {

private String firstName = "John";
private String lastName = "Doe";
private final Address address = new Address("Murder Lane");

private Person() {}

protected final String getFirstName() {
return firstName;
}

private String getLastName() {
return lastName;
}

private final Address getAddress() {
return address;
}

private final static class Address {
private final String street;

private Address(final String street) {
this.street = street;
}

public String getStreet() {
return street;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.joke.spockmockable

import org.spockframework.mock.MockUtil
import spock.lang.Specification

@Mockable([Person, Person.Address])
class PersonTest extends Specification {

def mockUtil = new MockUtil()

def 'final from class is removed'() {
setup:
Person person = Mock()

expect:
mockUtil.isMock person
}

def 'final from subclass is removed'() {
setup:
Person.Address address = Mock()

expect:
mockUtil.isMock address
}

def 'final from method is removed'() {
setup:
Person person = Mock()

when:
def res = person.firstName

then:
1 * person.firstName >> 'Dorothy'

expect:
res == 'Dorothy'
}

def 'private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.lastName

then:
1 * person.lastName >> 'Gale'

expect:
res == 'Gale'
}

def 'final is removed and private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.address.street

then:
1 * person.address >> new Person.Address('Yellow Brick Road')

expect:
res == 'Yellow Brick Road'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.joke.spockmockable

import org.spockframework.mock.MockUtil

class PersonTestWithInheritance extends TestBase {

def mockUtil = new MockUtil()

def 'final from class is removed'() {
setup:
Person person = Mock()

expect:
mockUtil.isMock person
}

def 'final from subclass is removed'() {
setup:
Person.Address address = Mock()

expect:
mockUtil.isMock address
}

def 'final from method is removed'() {
setup:
Person person = Mock()

when:
def res = person.firstName

then:
1 * person.firstName >> 'Dorothy'

expect:
res == 'Dorothy'
}

def 'private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.lastName

then:
1 * person.lastName >> 'Gale'

expect:
res == 'Gale'
}

def 'final is removed and private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.address.street

then:
1 * person.address >> new Person.Address('Yellow Brick Road')

expect:
res == 'Yellow Brick Road'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.joke.spockmockable;

import spock.lang.Specification;

@Mockable([Person, Person.Address])
abstract class TestBase extends Specification {
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
rootProject.name = 'spock-mockable'

include 'examples:kotlin'
include 'examples:spock13'
include 'examples:spock13-groovy24'
include 'examples:spock13-groovy25'
include 'examples:spock20'
include 'examples:spring'
include 'spock-mockable'
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import static org.codehaus.groovy.control.CompilationUnit.SourceUnitOperation
import static org.codehaus.groovy.control.CompilePhase.CANONICALIZATION
import static org.codehaus.groovy.control.CompilePhase.OUTPUT

@CompileStatic
@GroovyASTTransformation(phase = CANONICALIZATION)
class MockableASTTransformation extends AbstractASTTransformation implements CompilationUnitAware {

Expand All @@ -24,7 +23,7 @@ class MockableASTTransformation extends AbstractASTTransformation implements Com
@Override
void visit(ASTNode[] nodes, SourceUnit source) {
def annotation = nodes[0] as AnnotationNode
detectedClasses.addAll(getMemberClassList(annotation, 'value')*.name as List)
detectedClasses.addAll(getClassList(annotation, 'value')*.name as List)
annotation.members.clear()
}

Expand Down

0 comments on commit cb01ead

Please sign in to comment.