-2

Regular expression to match the below cucumber steps

Given I login to system
Given login to system

Given user attempts to login to system
Given I attempt to login to system
Given attempts to login to system

This one Should fail - user or I or BLANK are valid but not he or other words

Given he login to system

I tried the below one but no help.

 Given(/(?: user|I?)?(?: attempts to)? login to system/, function () {});
Emma
  • 27,428
  • 11
  • 44
  • 69
Gan
  • 9
  • 5
  • I tried the below one but no help. ```Given(/(?: user|I?)?(?: attempts to)? login to system/, function () {});``` – Gan Jul 15 '19 at 16:17
  • `^Given(?: user| I)?(?: attempts? to)? login to system`? – depperm Jul 15 '19 at 16:22
  • @Gan You are expected to demonstrate your own attempts at solving this and then describe the specific problem you have, an obstacle you encountered. – Yunnosch Jul 15 '19 at 16:24
  • 1
    @Yunnosch he did provide his attempt in the comments, which is more than some – depperm Jul 15 '19 at 16:27
  • @depperm Good point actually. I missed that. Sorry. However, hiding this kind of helpful information in comments can cause exactly that problem: being missed. Also, "no help" is not exactly an explanation of the problem. How did it fail? For which input does it give false positives or false negatives. – Yunnosch Jul 15 '19 at 16:30
  • I copied the info from comment an deleted the three characters. Please [edit] yourself to fine tune. Also double check the `;` at the end of the shown coding attempt. It seems not to match your goal. – Yunnosch Jul 15 '19 at 16:33

2 Answers2

1

Don't use regex's at all when cuking. You will just make something vastly over complicated that can't possibly match all the different use cases your scenarios demand. Instead write a simple step definition for each one, and have each step definition call the same helper method to implement the functionality.

So you get

Given 'I login into the system' do
  login as: @i
end

Given 'Fred logs into the system' do
  login as: @fred
end

Given 'I login into the system as an admin' do
  login as: @i, role: 'admin'
end

... 

This simplifies, is DRY enough and removes completely the need for regex's in your step definitions (note: I've been cuking from the very beginning and haven't used a regex for several years, they give no benefit at all), and encourages better code when implementing (that login function is going to be used extensively).

diabolist
  • 3,990
  • 1
  • 11
  • 15
0

I'm guessing that you might be trying to design an expression similar to:

^Given\s*(?:I|user)?\s*(?:attempts?\s+to)?\s*(?:login\s+to\s+system)$

The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.

Emma
  • 27,428
  • 11
  • 44
  • 69