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 Cucumber Expressions (#124) #157

Merged
merged 8 commits into from
Nov 23, 2021
Merged

Conversation

ilslv
Copy link
Member

@ilslv ilslv commented Nov 10, 2021

Part of #124

Synopsis

Sometimes regex is a bit too much and we want something easier. Cucumber expressions are a great fit in this scenario.

Solution

Support in attribute macros with #[given(expr = "...")]/#[given(expression = "...")]

Checklist

  • Created PR:
    • In draft mode
    • Name contains Draft: prefix
    • Name contains issue reference
    • Has assignee
  • Documentation is updated (if required)
  • Tests are updated (if required)
  • Changes conform code style
  • CHANGELOG entry is added (if required)
  • FCM (final commit message) is posted
    • and approved
  • Review is completed and changes are approved
  • Before merge:
    • Milestone is set
    • PR's name and description are correct and up-to-date
    • Draft: prefix is removed
    • All temporary labels are removed

@ilslv ilslv added the enhancement Improvement of existing features or bugfix label Nov 10, 2021
@ilslv ilslv added this to the 0.11 milestone Nov 10, 2021
@ilslv ilslv self-assigned this Nov 10, 2021
@ilslv
Copy link
Member Author

ilslv commented Nov 10, 2021

Current blockers:

@tyranron
Copy link
Member

@ilslv cucumber-expressions 0.1.0 is released, we can move on here.

@ilslv ilslv changed the title Draft: Support Cucumber Expressions Draft: Support Cucumber Expressions (#124) Nov 23, 2021
@ilslv
Copy link
Member Author

ilslv commented Nov 23, 2021

FCM

Support Cucumber Expressions (#157, #124)

@ilslv ilslv changed the title Draft: Support Cucumber Expressions (#124) Support Cucumber Expressions (#124) Nov 23, 2021
@ilslv ilslv marked this pull request as ready for review November 23, 2021 05:44
@ilslv ilslv requested a review from tyranron November 23, 2021 07:01
Copy link
Member

@tyranron tyranron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ilslv we need a clear example of how to use custom parameters in Cucumber Expressions.

Also, a FromStr example in the Book doesn't actually demonstrate how it works with a parameter captured by a Cucumber Expression.

@ilslv
Copy link
Member Author

ilslv commented Nov 23, 2021

@tyranron for custom parameters I propose something like this

enum Animal {
    Cat,
    Dog,
    Ferris,
}

impl FromStr for Animal {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "cat" => Ok(Self::Cat),
            "dog" => Ok(Self::Dog),
            "🦀" => Ok(Self::Ferris),
            _ => Err("expected 'cat', 'dog' or '🦀'"),
        }
    }
}

impl CustomParameter for Animal {
    const NAME = "animal";
    const REGEX = "cat|dog|🦀";
}

#[then(expr = "the {animal} is not hungry")]
async fn cat_is_fed(world: &mut AnimalWorld, animal: Animal) {
    sleep(Duration::from_secs(2)).await;

    match animal {
        Animal::Cat => assert!(!world.cat.hungry),
        Animal::Dog => assert!(!world.dog.hungry),
        Animal::Ferris => assert!(!world.ferris.hungry),
    };
}

With that we can validate that parameter name is right with const assertion. And to validate regex we can even use clippy lint : Playground.
Also, as we have exact type, we can use deref-based specialisation to fallback on default parameter name in our const assertion.

UPD:

Clippy lint will require our users to run it over tests codebase, which is rarely done. If we want to avoid this, we'll have to implement CustomParameter with our own derive macro, as I don't think there is another way of validating regex at compile time.

@tyranron
Copy link
Member

tyranron commented Nov 23, 2021

@ilslv let's move this discussion to the issue, because it won't be a part of this PR. And merge this PR only with FromStr.

@tyranron tyranron merged commit 7bc13da into main Nov 23, 2021
@tyranron tyranron deleted the 124-support-cucumber-expressions branch November 23, 2021 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing features or bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants