Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft assertions should use accept a list #863

Open
OdysseusLives opened this Issue Jul 27, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@OdysseusLives
Copy link

OdysseusLives commented Jul 27, 2018

Issue description

Soft assertions with verifyAll should be able to accept a list.

This would be most useful with parameterized tests, where only some assertions are applicable, yet the test setup and actions are the same.

Ideal example

def assertions = [1 == 1, 1 == 2, 2 == 3]
verifyAll {
    assertions.each {
        it
    }
}

as opposed to defining each assertion in a separate line in the verifyAll block:

verifyAll {
    1 == 1
    1 == 2
    2 == 3
}
@leonard84

This comment has been minimized.

Copy link
Member

leonard84 commented Jul 31, 2018

It is highly unlikely that this will be added, spock power assertios are implemented using AST transformations during compile time. For that to work, spock needs to know precisely which statement needs to be transformed, this will not work some random list. So what you basically have is a list of booleans.

@tburny

This comment has been minimized.

Copy link

tburny commented Sep 6, 2018

Basically assertions is a list of booleans, I used any()/every (see https://www.tutorialspoint.com/groovy/groovy_any_every.htm) in then:-blocks

assertions.every { it }

If you like it more readable:

// Store this somewhere (identitiy function)
def entryIsTrue = { a -> a }
// Usage
assertions every (entryIsTrue)

Does that help?

@leonard84

This comment has been minimized.

Copy link
Member

leonard84 commented Sep 7, 2018

What could be possible would be something like this:

verifyAll(Iterable<Closure>);

// note explicit assert statements are neccessary
def assertions = [{assert 1 == 1}, {assert 1 == 2}, {assert 2 == 3}]

verifyAll(assertions)

But you could easily implement this yourself

void customAsssert(Iterable<Closure> assertions) {
 def result = assertions.collect {
    try {
      it()
      return null
    } catch (AssertionError e)
      return e
    }
  }.findAll()
  if (result) {
    throw new MultipleFailureException(result)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.