-
Notifications
You must be signed in to change notification settings - Fork 69
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
Where code blocks are missing their source code #237
Comments
Hi @Gleethos . I don't believe this to be a problem with Spock reports itself. Where blocks are represented as tables of data in the reports, not source code, and I would like to keep it that way. You may ask how could you avoid using lambdas as examples in your tests, which is a valid question. I have the following strategy, normally, and you can probably come up with others...
def 'my tests'( Function<MyThing, Object> example ) {
given:
def myThing = new MyThing()
when: 'something'
example( myThing )
then: 'error'
thrown expectedException
where:
example || expectedException
Examples.OnlyPositive || IllegalArgumentException
Examples.NoNulls || NullPointerException
}
enum Examples implements Function<MyThing, Object> {
OnlyPositive, NoNulls; // more
@Override
Object apply( MyThing myThing ) {
switch ( this ) {
case OnlyPositive:
return myThing.onlyPositive( -1 )
case NoNulls:
return myThing.dontLikeNull( null )
default:
throw new IllegalStateException( "Case not covered: $this" )
}
}
} |
A more generic (and more dynamic) and less verbose way to do this: Create an example class to be used when functions are to be tested: @TupleConstructor
class ExampleFunction {
String name
Function fun
def apply( arg ) {
fun( arg )
}
@Override
String toString() { name }
} Use that as examples: where:
example || expectedException
new ExampleFunction( 'only positive', { it.onlyPositive( -1 ) } ) || IllegalArgumentException
new ExampleFunction( 'no nulls', { it.dontLikeNull( null ) } ) || NullPointerException
|
Hi @renatoathaydes, thank you for your quick response!
I believe there is a misunderstanding here as to what I am suggesting! where : 'We use the following test data.'
day | age
Day.MONDAY | 24
Day.FRIDAY | 42 should also be somehow available as a ['day | age', 'Day.MONDAY | 24', 'Day.FRIDAY | 42'] I mostly agree with your objection of the usage of closure / lambda expressions EnumsThe string representation of an enum might merely be its name, Records / Data ClassesCertain record implementations or classes with a data centric purpose InstantiationThis ties into the previous point of data centric types So for example I have tests with data tables very similar to this:
ConstantsThis is very similar to the problem with enums. Data TypesOne last problem that comes to mind is that even for the most basic
This is extremely important when building reports intended to be Again, I am in no way suggesting the reform how this plugin currently generates its reports If all of this does not seem important to you than that is Thank you again for this wonderful project! |
Thanks man. That really makes me happy :)
I see now that I'd read your question and failed to understand the full request! Sorry about that. I will go through your suggestions and get back to you in the next couple of days. But I believe we can make this happen. Let's get your other issue fixed first, then I can address this. Also, please notice there's a Groovy 4 branch now, |
Hi again @Gleethos,
Ok, so if I remove that check (and remove the code from If that's going to work anyway for you, I will release this fix and #235 later today. |
Thank you for considering adding this!
Almost, except that all of the code lines should be So for example for the where : 'We use the following names:'
name << [
'Adam',
'Zoey',
'Tom'
] I would expect the following data exposed to the template: [
"name << [",
" 'Adam',",
" 'Zoey',",
" 'Tom'",
" ]"
] That way we can parse the data table ourselves |
I am not sure what you mean. I am certainly planing to migrate to Groovy 4. |
That was my question... was hoping some people could test the Groovy 4 branch before I release that. |
Sure! I will run it on a few hundred unit tests, and inform you of any problems. |
I tested the Groovy 4 branch on all my tests and it worked well! Also, I locally disabled the condition... if ( statement.statementLabel == 'where' ) {
waitForNextBlock = true
} at line 183 in the Would it be possible to remove the above condition and then simply adjust the existing report generators
If the check you were mentioning before is in fact the one I was just referencing I am happy with this solution and would consider this fully fixed yes! :) Edit: |
@Gleethos if you open a PR that is definitely doing what you think #237 requires, I am more than happy to accept that... I did remove that check, and then changed the default HTML report to filter blocks to exclude the And the same probably in the template report creator: |
I tested my changes on some new report template code |
Fix #237, where code blocks are missing their source code
This is a problem because certain data table entries will become unreadable when turned into
reports because
toString()
often distorts their representation.Consider the following example:
So when I try to make this into living documentation using the data table entries
using the
dataValues
in my template file:I get something like this:
However, what I really want is this:
I understand that this is not what the
dataValues
is for, what I would expect isthat there is the actual code of the
where
block in theblock.sourceCode
field,but it is always missing!
After looking into the code I noticed that there is a simple condition preventing the
where block code from being read, I removed it and it worked perfectly well.
I would be happy to fix this if that's okay, this would not change anything except
it would open up more option for users.
This is particularly interesting to me since we are using this plugin to generate
test suite browsers for pure documentation purposes so that newcomers can learn how to
use our libraries and APIs without having to dig through the source code files...
The text was updated successfully, but these errors were encountered: