You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to create data-driven tests with Spock that use explicit or implicit type conversion?
In my sample project I have classes like Age or Name that wrap Integer and String plus a Person { Name name; Age age; } that uses them. I'd like to write my data like so:
def"a test"(Name name, Age age){
where:
name | age
"John" | 5"Lisa" | 2
}
or like this:
def"a test"(Person person){
where:
name | age
"John" | 5"Lisa" | 2
}
What I don't want to do is to write it like this, because that creates a lot of noise in the test:
def"a test"(Name name, Age age){
where:
name | age
Name.of("John") | newAge(5)
Name.of("Lisa") | newAge(2)
}
I've written a Groovy type coercion extension method from String to Person but Spock does not want to use it.
JUnit5 automatically uses a static factory for type conversion or I can supply an ArgumentConverter/ArgumentAggregator. Can I do something similar in Spock?
How to reproduce
Link to a gist or similar (optional)
I created a sample test if you want to take a look.
Supporting the type coercion or rather why it is not supported is probably worth looking at.
What you currently could do, is to write the test like this:
def"a test"(Name name, Age age) {
where:
theName | theAge
"John" | 5"Lisa" | 2
name =Name.of(theName)
age =newAge(theAge)
}
In reality you would probably do some magic like checking whether there is an unset Person parameter and whether there are parameters named name and age and then use those to construct the person.
And with my PR #1280 it will simply work as you expected.
It works both, with defining an extension module like you did, or with the help of the @Use extension.
But be aware that you have to annotate the specification, as the @Use extension cannot work on the code in the where block due to technical reasons as documented.
If you want to combine name and age into a Person parameter, you still have to go the Spock extension way though.
If you want to combine name and age into a Person parameter, you still have to go the Spock extension way though.
Alternatively, you could use a map if the object has a no args constructor, or add a custom asType to handle a other constructor.
Not as pretty, but easier than writing a custom extension.
Wow. Thank you so much for your fast response and I'm sorry for my tardiness when it comes to replying to the issue I created.
Thanks for your replies. I've incorporated your code suggestions into my example project. I hope it's ok that I directly copied your extension code@Vampire and added an attribution?
I eagerly look forward to the next Spock version that incorporates type coercion.
Activity
Vampire commentedon Feb 20, 2021
Supporting the type coercion or rather why it is not supported is probably worth looking at.
What you currently could do, is to write the test like this:
Vampire commentedon Feb 20, 2021
You could also write a global or annotation based extension that registers a feature method interceptor and adjusts as needed, like
In reality you would probably do some magic like checking whether there is an unset
Personparameter and whether there are parameters namednameandageand then use those to construct the person.Vampire commentedon Feb 21, 2021
Or a variant of the first version that also works:
Cast data variables with type coercion to the declared parameter type (…
Vampire commentedon Feb 21, 2021
And with my PR #1280 it will simply work as you expected.
It works both, with defining an extension module like you did, or with the help of the
@Useextension.But be aware that you have to annotate the specification, as the
@Useextension cannot work on the code in thewhereblock due to technical reasons as documented.If you want to combine
nameandageinto aPersonparameter, you still have to go the Spock extension way though.Cast data variables with type coercion to the declared parameter type (…
Cast data variables with type coercion to the declared parameter type (…
leonard84 commentedon Feb 22, 2021
Alternatively, you could use a map if the object has a no args constructor, or add a custom
asTypeto handle a other constructor.Not as pretty, but easier than writing a custom extension.
Cast data variables with type coercion to the declared parameter type (…
Richargh commentedon Feb 26, 2021
Wow. Thank you so much for your fast response and I'm sorry for my tardiness when it comes to replying to the issue I created.
Thanks for your replies. I've incorporated your code suggestions into my example project. I hope it's ok that I directly copied your extension code @Vampire and added an attribution?
I eagerly look forward to the next Spock version that incorporates type coercion.