Skip to content

Add support for implicit assertions combined with power assert plugin #201

@pshevche

Description

@pshevche

Why does this matter?

Spock's implicit assertions provide an ergonomic way of defining conditions by avoiding repeating invocations of assertion methods:

expect:
expectedValue == actualValue

Furthermore, Spock provides helper methods like with(target, closure), verifyEach(target, closure) and verifyAll(target, closure). These methods allow users to define implicit assertions on more complex objects, such as collections.

The goal of this story is to provide a similar experience with Spockk. In contrast to Spock, Spockk does not implement assertions destructuring itself, but provides it via the power-assert compiler plugin. This behavior should stay.

What should developers be able to write?

class SpecWithImplicitAssertions : Specfication() {
  def "feature with implicit assertions"() {
    given
    val a = 1
    val b  = 2

    expect
    a < b
  }

  def "feature with implicit assertions for custom objects"() {
    given
    val person = Person(firstname: "A", lastname: "B")
    
    expect
    verify(person) { // verify should replace Spock's with(...)
       it.firstname == "A"
       it.lastname == "B"
    }
  }

  def "feature with implicit assertions for collections"() {
    given
    val list = listOf(1, 2, 3)
    
    expect
    verifyEach(list) {
       it % 2 == 0
    }
  }
}

Acceptance criteria

  • Users can define implicit conditions as top-level statements in expectation blocks (i.e., then and expect)
  • Users can define implicit conditions for complex objects via verify helper instead of a with helper
  • All implicit conditions inside verifyAll must be evaluated
  • verifyEach allows defining implicit conditions for collections
  • Implicit and explicit conditions (i.e., using arbitrary assertion functions) can be mixed everywhere
  • Users should be able to opt-out of implicit assertions
  • Implicit conditions should be wrapped inside kotlin.test.assertTrue
  • If the user does not opt-out of implicit conditions, we should check early that kotlin.test dependency is available and notify the user about it
  • The feature body with rewritten conditions should match what Spock is doing with its error collectors etc.

How does Spock implement this?

No response

Additional context

Users should be able to opt-out of implicit assertions

The plugin should establish the following extension for a Gradle project that applies the plugin:

spockk {
   features {
     enable(SpockkFeature.IMPLICIT_ASSERTIONS)
     disable(SpockkFeature.IMPLICIT_ASSERTIONS)
   }
}

The features configuration allows users to determine whether features like implicit assertions should be enabled or disabled. SpockkFeature should be an enum that has a description of the feature and the default boolean value indicating whether it is enabled or not.

Metadata

Metadata

Assignees

Labels

type::storyLarge(r) item with user-facing value

Projects

Status

In progress

Relationships

None yet

Development

No branches or pull requests

Issue actions