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

Propagate Step being matched by multiple Regexes as AmbiguousMatchError #143

Merged
merged 30 commits into from
Oct 19, 2021

Conversation

ilslv
Copy link
Member

@ilslv ilslv commented Oct 18, 2021

Synopsis

For now if Step is matched by multiple Regexes, we just use one of them, which can lead to very non-intuitive and frustrating errors.

Solution

Propagate that case as an AmbiguousMatchError.

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

This PR is nominated at RU RustCon Contest

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

ilslv commented Oct 18, 2021

FCM

Error when step matches multiple step functions (#143)

@ilslv ilslv marked this pull request as ready for review October 18, 2021 12:50
@ilslv ilslv changed the title Draft: Propagate Step being matched by multiple Regexes as AmbiguousMatchError Propagate Step being matched by multiple Regexes as AmbiguousMatchError Oct 18, 2021
@ilslv ilslv requested a review from tyranron October 18, 2021 13:22
Some(::cucumber::step::Location {
path: ::std::convert::From::from(::std::file!()),
line: ::std::line!(),
column: ::std::column!(),
Copy link
Member

Choose a reason for hiding this comment

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

Given that we always provide cucumber::step::Location in the generated code, I think we may strip Option out of many places in of codegen machinery.

src/event.rs Outdated
///
/// [`Regex`]: regex::Regex
/// [`Step`]: gherkin::Step
AmbiguousMatch(step::AmbiguousMatchError),
Copy link
Member

Choose a reason for hiding this comment

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

@ilslv can we just reuse Failed variant instead of introducing a new one over the whole code base, and try to downcast into step::AmbiguousMatchError in writer::Basic to provide a better printing?

Copy link
Member Author

Choose a reason for hiding this comment

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

@tyranron we can, but I've would like to have this error encoded inside event. Mostly to show that this invariant is checked on a type level and not expect our users to read all docs or book. We can use something like this inside Failed variant

enum StepError { 
    AmbiguousMatch(..),
    Panic(Arc<Info>),
}

Copy link
Member

Choose a reason for hiding this comment

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

@ilslv yes, this is better than introducing a whole new event variant.

src/step.rs Outdated
then: HashMap::new(),
given: BTreeMap::new(),
when: BTreeMap::new(),
then: BTreeMap::new(),
Copy link
Member

Choose a reason for hiding this comment

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

It's unclear why we've switched to BTreeMap instead of HashMap.

Copy link
Member Author

Choose a reason for hiding this comment

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

@tyranron output test now requires stable ordering of Steps to show, which of them were matched inside AmbiguousMatchError. HashMap couldn't provide it, because of path inside of a Location and how rust tests work with names like somerandomjbrsh-output.

Copy link
Member

Choose a reason for hiding this comment

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

@ilslv could this be handled by tests somehow, not on the library level?

Copy link
Member Author

Choose a reason for hiding this comment

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

@tyranron yes, we can rearrange AmbiguousMatchError in unpacked event and only then output it.

Copy link
Member

Choose a reason for hiding this comment

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

@ilslv then let's hack it in tests 👍

@tyranron tyranron marked this pull request as draft October 18, 2021 16:54
@ilslv ilslv marked this pull request as ready for review October 19, 2021 10:35
@ilslv ilslv requested a review from tyranron October 19, 2021 10:36
src/step.rs Outdated
} else {
// Instead of `.unwrap()` to avoid documenting `# Panics`
// section.
unreachable!()
Copy link
Member

Choose a reason for hiding this comment

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

.unwrap_or_else(|| unreachable!())? 🌚

self.steps = mem::take(&mut self.steps).given(regex, step);
pub fn given(
mut self,
loc: Option<step::Location>,
Copy link
Member

Choose a reason for hiding this comment

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

I think having this in runner::Basic and Cucumber methods has no sense, because user will unlikely specify Locations by hand, while the proc-macro machinery does this via .steps() method.

@tyranron tyranron merged commit a95ddb1 into main Oct 19, 2021
@tyranron tyranron deleted the ambiguous-error branch October 19, 2021 16:42
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