JGiven icon indicating copy to clipboard operation
JGiven copied to clipboard

Flexible Nested Steps

Open Lovett1991 opened this issue 4 years ago • 5 comments

Enhancement Request

Hi guys,

I've extended JGiven to allow for flexible nesting within tests. This means that in code can be written as...

public class Given extends Stage<Given> {
  
  public GivenUser user() {
    // Create GivenUser and inject states etc
  }

  public static class GivenUser extends NestedStage<GivenUser> {
    public GivenUser male() {
      // Set gender
    }
  }
}

Then in the test you can have code like..

given().a().user()
  .that().is().male();

The report then treats this as a nested step...

Given a user
    that is male

I'm currently in the process of seeing if I can push this to github, or even contribute to this library directly.

Is this a feature you would accept (provided my organisation permit it?)

Lovett1991 avatar Mar 23 '20 12:03 Lovett1991

Looks interesting. Would NestedStage be then a predefined class of JGiven?

janschaefer avatar Jun 14 '20 10:06 janschaefer

Yup the nested class looks like this:

public class ExtendedStage<SELF extends ExtendedStage<?>> extends Stage<SELF> {

    private ScenarioBase scenarioBase;

    public SELF an() {
        return self();
    }

    public SELF a() { return self(); }

    public SELF the() {
        return self();
    }

    public SELF that() { return self(); }

    public SELF is() { return self(); }

    @Hidden
    public SELF withScenarioBase(ScenarioBase scenarioBase) {
        this.scenarioBase = scenarioBase;
        return self();
    }

    protected <T extends NestedStage<T,SELF>> T nest(Class<T> clazz) {
        return scenarioBase
                .addStage(clazz)
                .withReverse(self())
                .withScenarioBase(scenarioBase);
    }

    public static class NestedStage<NESTED extends ExtendedStage<?>, SELF> extends ExtendedStage<NESTED> {
        private SELF reverse;
        private ScenarioBase scenarioBase;

        @Hidden
        public NESTED withReverse(SELF reverse) {
            this.reverse = reverse;
            return self();
        }

        public SELF backup() {
            return reverse;
        }

    }
}

The way I got it working is a massive hack due to some fields being private, could be tidied up.

Lovett1991 avatar Jun 29 '20 07:06 Lovett1991

hey @janschaefer I've made my implementation public:

https://github.com/Lovett1991/JGiven-Extension

It's pretty hacky, but I've used it in several projects now with moderately complex tests and it works quite nicely (for me anyway). I used to use a similar library in my previous job but that was kept in house, so when I moved I just wrote this from scratch.

I wouldn't mind creating a PR if you think it's a feature worth having, although finding the free time is difficult.

Lovett1991 avatar Sep 25 '20 17:09 Lovett1991

That is the repo. It has been quite some time since I’ve looked into this. If it’s a feature you’re looking to add to the repository then I can see if I can find some time.

On 6 Dec 2023, at 14:47, Tiago @.***> wrote:

@Lovett1991 https://github.com/Lovett1991 could you maybe share more details of your implementation, so that others can explore this option? Is is this code: https://github.com/Lovett1991/JGiven-Extension/tree/dev ? Thanks

— Reply to this email directly, view it on GitHub https://github.com/TNG/JGiven/issues/451#issuecomment-1843036368, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHATEPL6WJHWZRR7Z4V6PSLYICAPHAVCNFSM4LR223PKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBUGMYDGNRTGY4A. You are receiving this because you were mentioned.

Lovett1991 avatar Dec 07 '23 01:12 Lovett1991

Hi @Lovett1991 if you find the time for a contribution that would be very welcome

l-1squared avatar Dec 07 '23 06:12 l-1squared