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

Support hooks without parameter #26

Closed
gaeljw opened this issue Apr 25, 2020 · 2 comments · Fixed by #28 or #40
Closed

Support hooks without parameter #26

gaeljw opened this issue Apr 25, 2020 · 2 comments · Fixed by #28 or #40
Assignees
Labels
⚡ enhancement Request for new functionality

Comments

@gaeljw
Copy link
Member

gaeljw commented Apr 25, 2020

If possible we should support hooks without parameters.

For instance instead of writing that:

Before { _ =>
  // Some code
}

We should be able to write this:

Before {
  // Some code
}
@gaeljw gaeljw self-assigned this Apr 25, 2020
@gaeljw gaeljw added the ⚡ enhancement Request for new functionality label Apr 25, 2020
@gaeljw gaeljw added this to the 5.6.0 milestone Apr 25, 2020
@gaeljw gaeljw reopened this Apr 25, 2020
@gaeljw gaeljw removed this from the 5.6.0 milestone May 1, 2020
@gaeljw
Copy link
Member Author

gaeljw commented May 1, 2020

Naive implementation would be to add methods like this:

// Current method
def Before(tagExpression: String)(body: Scenario => Unit): Unit = {
  Before(tagExpression, DEFAULT_BEFORE_ORDER)(body)
}

// Added method
def Before(tagExpression: String)(body: => Unit): Unit = {
  Before(tagExpression, DEFAULT_BEFORE_ORDER)(_ => body)
}

But from a user perspective, you get errors "Cannot resolve overloaded method 'Before'".


After having a look, I came up with a solution that I don't think it's worth shipping because it would:

  • bring some complexity in the code (similar to what is done for Step in ScalaDsl)
  • bring a breaking change to the users (need to write Before() { } or Before(order= 2) { })

What I ended up writing:

  sealed trait HookType

  case object BeforeType extends HookType
  // TODO other types

  val Before = new Hook(BeforeType)
  // TODO other vals

  final class Hook(hookType: HookType) {
    def apply(tagExpression: String = EMPTY_TAG_EXPRESSION, order: Int = DEFAULT_BEFORE_ORDER): HookImpl = new HookImpl(hookType, tagExpression, order)
  }

  final class HookImpl(hookType: HookType, tagExpression: String, order: Int) {

    def apply(body: HookBody): Unit = {
      hookType match {
        case BeforeType =>
          registry.beforeHooks += ScalaHookDetails(tagExpression, order, body)
        // TODO other cases
      }
    }

    def apply(body: => Unit): Unit = {
      apply(_ => body)
    }

  }

If anyone has an idea how to implement it easily, please shout :)

@gaeljw gaeljw added 🙏 help wanted Help wanted - not prioritized by core team and removed 🙏 help wanted Help wanted - not prioritized by core team labels May 1, 2020
@gaeljw
Copy link
Member Author

gaeljw commented May 3, 2020

Actually, after a day of reflexion and after implementing the ParameterType stuff, the solution became obvious and there is no breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡ enhancement Request for new functionality
Projects
None yet
1 participant