JGiven icon indicating copy to clipboard operation
JGiven copied to clipboard

@NestedSteps should allow formatting of nested steps

Open prathyk opened this issue 7 years ago • 4 comments

Currently the nested steps are just indented. What if I want to enclose nested steps in curly braces?

something like - If I use groovy to write JGiven tests, I can easily create a dsl to create a customer instance:

new Customer{
   name: "John"
   Age: 25
   Gender: Male
}

I want the above DSL to appear as it is even in the console report. The methods name, Age, Gender are nested steps inside Customer. I tag the Customer method with @NestedSteps but it would have been nice if it allows us to format the nested steps, may be using a PrintFormatter etc... Like in @NestedSteps(PrintFormatter.class, "{$nested}"

I hope you get the idea. I want to control the formatting of nested steps. By the way, is there a way we can provide our own listener to listen to JGiven steps???

prathyk avatar Jan 23 '18 09:01 prathyk

The question is why you want to use nested steps here. JGiven can format complex objects and you can provide your own formatter for that. See http://jgiven.org/userguide/#_the_pojoformat_annotation for more details. Please check whether this solves your use case.

Regarding your 2nd question: no there is no mechanism yet to have custom step listeners. There is already an open issue for that: https://github.com/TNG/JGiven/issues/328

janschaefer avatar Jan 26 '18 06:01 janschaefer

Yes, I know about the POJO formatter but that is not enough for my usecase. I have a customer builder and the builder has many methods. If I have to print the customer using POJOFormatter, then I'll have to track what methods on the builder were called by using some kind of reflection. Moreover, I'll have to pass customer as a parameter than as a Closure as above.

Example: cust1 = new Customer{ name: "John" Age: 25 Gender: Male }

cust2 = new Customer{ name: "John2" Age: 21 Gender: Male Mobile: 99xx99xx9xx } For cust2, the Mobile number is specified but for cust1 it assumes some default mobile number. While toString() or POJOFormatter can print all fields of the customer, it cannot track which fields were explicitly set by the user.

A custom stepListener also would solve problem such as this, because I would then be able to take control of how my steps are formatted.

I think the distinguishing feature of JGiven when compared to Spock etc... is the ability to automatically generate Scenario text and there should be all flexibility in that process. I feel this is important feature for JGiven.

prathyk avatar Jan 28 '18 09:01 prathyk

Ok, I think I got it now. It is similar to #83. I think. The only difference is that you want to also be able to specify how the steps are formatted.

janschaefer avatar Jan 28 '18 11:01 janschaefer

Yes. correct. I saw #83 . I understood that to get builders which again take embedded builders, I needed lambdas. So, I figured out I could use groovy closures to get nested steps but then the nested step is always differentiated only by its indentation. I want to be able to control the formatting of the steps

prathyk avatar Jan 29 '18 07:01 prathyk