Skip to content

Commit

Permalink
Add optional reason to Requires and IgnoreIf (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lőrinc Pap committed Jul 21, 2021
1 parent 450dc1b commit f183e7b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[![Gitter](https://badges.gitter.im/spockframework/spock.svg)](https://gitter.im/spockframework/spock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Revved up by Gradle Enterprise](https://img.shields.io/badge/Revved%20up%20by-Gradle%20Enterprise-06A0CE?logo=Gradle&labelColor=02303A)](https://ge.spockframework.org/scans)


Spock Framework
===============

Expand Down Expand Up @@ -38,8 +37,8 @@ repositories {
}
dependencies {
compile 'org.spockframework.spock:spock-core:spock-2.0'
compile 'org.spockframework.spock:spock-spring:spock-2.0'
testImplementation 'org.spockframework.spock:spock-core:spock-2.0'
testImplementation 'org.spockframework.spock:spock-spring:spock-2.0'
}
```
3. For intermediate releases you can also use the commit-hash as version, e.g. compile `com.github.spockframework.spock:spock-core:d91bf785a1`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* @author Peter Niederwieser
*/
public class IgnoreIfExtension extends ConditionalExtension<IgnoreIf> {

private static final String DEFAULT_MESSAGE = "Ignored via @" + IgnoreIf.class.getSimpleName();

@Override
protected Class<? extends Closure> getConditionClass(IgnoreIf annotation) {
return annotation.value();
Expand All @@ -35,21 +38,30 @@ protected Class<? extends Closure> getConditionClass(IgnoreIf annotation) {
@Override
protected void specConditionResult(boolean result, IgnoreIf annotation, SpecInfo spec) {
if (result) {
spec.skip("Ignored via @IgnoreIf");
spec.skip(ignoredMessage(annotation));
}
}

@Override
protected void featureConditionResult(boolean result, IgnoreIf annotation, FeatureInfo feature) {
if (result) {
feature.skip("Ignored via @IgnoreIf");
feature.skip(ignoredMessage(annotation));
}
}

@Override
protected void iterationConditionResult(boolean result, IgnoreIf annotation, IMethodInvocation invocation) {
if (result) {
throw new TestAbortedException("Ignored via @IgnoreIf");
throw new TestAbortedException(ignoredMessage(annotation));
}
}

private static String ignoredMessage(IgnoreIf annotation) {
String reason = annotation.reason();
if (reason.isEmpty()) {
return DEFAULT_MESSAGE;
} else {
return DEFAULT_MESSAGE + ": " + reason;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* @author Peter Niederwieser
*/
public class RequiresExtension extends ConditionalExtension<Requires> {

private static final String DEFAULT_MESSAGE = "Ignored via @" + Requires.class.getSimpleName();

@Override
protected Class<? extends Closure> getConditionClass(Requires annotation) {
return annotation.value();
Expand All @@ -35,21 +38,30 @@ protected Class<? extends Closure> getConditionClass(Requires annotation) {
@Override
protected void specConditionResult(boolean result, Requires annotation, SpecInfo spec) {
if (!result) {
spec.skip("Ignored via @Requires");
spec.skip(ignoredMessage(annotation));
}
}

@Override
protected void featureConditionResult(boolean result, Requires annotation, FeatureInfo feature) {
if (!result) {
feature.skip("Ignored via @Requires");
feature.skip(ignoredMessage(annotation));
}
}

@Override
protected void iterationConditionResult(boolean result, Requires annotation, IMethodInvocation invocation) {
if (!result) {
throw new TestAbortedException("Ignored via @Requires");
throw new TestAbortedException(ignoredMessage(annotation));
}
}

private static String ignoredMessage(Requires annotation) {
String reason = annotation.reason();
if (reason.isEmpty()) {
return DEFAULT_MESSAGE;
} else {
return DEFAULT_MESSAGE + ": " + reason;
}
}
}
4 changes: 3 additions & 1 deletion spock-core/src/main/java/spock/lang/IgnoreIf.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
@ExtensionAnnotation(IgnoreIfExtension.class)
@Repeatable(IgnoreIf.Container.class)
public @interface IgnoreIf {
Class<? extends Closure> value();
String reason() default "";

Class<? extends Closure> value();

/**
* @since 2.0
Expand Down
2 changes: 2 additions & 0 deletions spock-core/src/main/java/spock/lang/Requires.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
@ExtensionAnnotation(RequiresExtension.class)
@Repeatable(Requires.Container.class)
public @interface Requires {
String reason() default "";

Class<? extends Closure> value();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ class Foo extends Specification {
result.testsSucceededCount == 1
}

def "ignored spec contains custom message"() {
when:
def result = runner.runWithImports """
@IgnoreIf(reason = "dummy message", value = { true })
class Foo extends Specification {
def "basic usage"() {
expect: false
}
}
"""

then:
result.allEvents().skipped().list()[0].payload.get() == 'Ignored via @IgnoreIf: dummy message'
}

def "spec usage with unqualified static method access"() {
when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ class Foo extends Specification {
result.testsSucceededCount == 0
}

def "ignored spec contains custom message"() {
when:
def result = runner.runWithImports """
@Requires(reason = "dummy message", value = { false })
class Foo extends Specification {
def "basic usage"() {
expect: false
}
}
"""

then:
result.allEvents().skipped().list()[0].payload.get() == 'Ignored via @Requires: dummy message'
}

def "fails if condition cannot be instantiated"() {
when:
runner.runWithImports """
Expand Down

0 comments on commit f183e7b

Please sign in to comment.