jonsamwell / dart_gherkin

A Gherkin parsers and runner for Dart which is very similar to Cucumber, it provides the base BDD functionality ready for use in platform specific implementations i.e. flutter/web
MIT License
44 stars 42 forks source link

Generify World in Hook Class #73

Open sashokbg opened 5 months ago

sashokbg commented 5 months ago

I propose to generify the methods that use the World objects in the Hook class such as:

  Future<void> onAfterScenarioWorldCreated(
    World world,
    String scenario,
    Iterable<Tag> tags,
  ) =>

And use something like:

class Hook<T extends World> {
  Future<void> onAfterScenarioWorldCreated(
      T world,
      String scenario,
      Iterable<Tag> tags,
      ) async {
    // Your implementation here
  }
}

This way we can use our own instances of World

// Define the subclass that overrides the method
class MyHook extends Hook<MyWorld> {
  @override
  Future<void> onAfterScenarioWorldCreated(
      MyWorld world,
      String scenario,
      Iterable<Tag> tags,
      ) async {
    // Your implementation here
  }
}
youssef-t commented 3 months ago

Hello,

As of today (with gherkin 3.1.0), one way to use a custom World instance is to cast it inside the method onAfterScenarioWorldCreated. This solution is still a workaround and your suggestion would be the cleanest way to do it.

Future<void> onAfterScenarioWorldCreated(
  World world,
  String scenario,
  Iterable<Tag> tags,
) async {
  assert(world is MyWorld, "world is not an instance of MyWorld");
  MyWorld myWorld = world as MyWorld;
  ...
}