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

Flexible Nested Steps #451

Open
Lovett1991 opened this issue Mar 23, 2020 · 5 comments
Open

Flexible Nested Steps #451

Lovett1991 opened this issue Mar 23, 2020 · 5 comments

Comments

@Lovett1991
Copy link

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?)

@janschaefer
Copy link
Contributor

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

@Lovett1991
Copy link
Author

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
Copy link
Author

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
Copy link
Author

Lovett1991 commented Dec 7, 2023 via email

@l-1squared
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants