-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #39 There should be one single When step per scenario
- Loading branch information
Showing
11 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
gherkin-checks/src/main/java/org/sonar/gherkin/checks/OneSingleWhenPerScenarioCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* SonarQube Gherkin Analyzer | ||
* Copyright (C) 2016-2016 David RACODON | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.gherkin.checks; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.gherkin.api.tree.BasicScenarioTree; | ||
import org.sonar.plugins.gherkin.api.tree.StepTree; | ||
import org.sonar.plugins.gherkin.api.tree.Tree; | ||
import org.sonar.plugins.gherkin.api.visitors.SubscriptionVisitorCheck; | ||
import org.sonar.plugins.gherkin.api.visitors.issue.PreciseIssue; | ||
import org.sonar.squidbridge.annotations.ActivatedByDefault; | ||
import org.sonar.squidbridge.annotations.SqaleConstantRemediation; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Rule( | ||
key = "one-single-when-per-scenario", | ||
name = "There should be one single When step per scenario", | ||
priority = Priority.MAJOR, | ||
tags = {Tags.DESIGN}) | ||
@SqaleConstantRemediation("15min") | ||
@ActivatedByDefault | ||
public class OneSingleWhenPerScenarioCheck extends SubscriptionVisitorCheck { | ||
|
||
@Override | ||
public List<Tree.Kind> nodesToVisit() { | ||
return ImmutableList.of(Tree.Kind.SCENARIO, Tree.Kind.SCENARIO_OUTLINE); | ||
} | ||
|
||
@Override | ||
public void visitNode(Tree tree) { | ||
List<StepTree> whenSteps = ((BasicScenarioTree) tree).steps() | ||
.stream() | ||
.filter(s -> s.type() == StepTree.StepType.WHEN) | ||
.collect(Collectors.toList()); | ||
|
||
if (whenSteps.size() > 1) { | ||
PreciseIssue issue = addPreciseIssue(whenSteps.get(0), "Merge all the When steps or split the scenario up in multiple scenarios."); | ||
for (int i = 1; i < whenSteps.size(); i++) { | ||
issue.secondary(whenSteps.get(i), "When step"); | ||
} | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...src/main/resources/org/sonar/l10n/gherkin/rules/gherkin/one-single-when-per-scenario.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<p> | ||
There should be one single <code>When</code> step per scenario. If you feel compelled to add more, it is usually a | ||
sign that you should split the scenario up in multiple scenarios or that some <code>When</code> steps could be | ||
converted into <code>Given</code> steps. | ||
</p> | ||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
Scenario: Add a nice bike to my cart | ||
Given I am a customer | ||
When I go to the nice bike product page | ||
And I add the nice bike to my cart # Noncompliant | ||
Then I should see the nice bike in my cart | ||
</pre> | ||
|
||
<h2>Compliant Solution</h2> | ||
<pre> | ||
Scenario: Add a nice bike to my cart | ||
Given I am a customer | ||
And I am on the nice bike product page | ||
When I add the nice bike to my cart | ||
Then I should see the nice bike in my cart | ||
</pre> |
32 changes: 32 additions & 0 deletions
32
gherkin-checks/src/test/java/org/sonar/gherkin/checks/OneSingleWhenPerScenarioCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SonarQube Gherkin Analyzer | ||
* Copyright (C) 2016-2016 David RACODON | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.gherkin.checks; | ||
|
||
import org.junit.Test; | ||
import org.sonar.gherkin.checks.verifier.GherkinCheckVerifier; | ||
|
||
public class OneSingleWhenPerScenarioCheckTest { | ||
|
||
@Test | ||
public void test() { | ||
GherkinCheckVerifier.verify(new OneSingleWhenPerScenarioCheck(), CheckTestUtils.getTestFile("one-single-when-per-scenario.feature")); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
gherkin-checks/src/test/resources/checks/one-single-when-per-scenario.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Feature: My feature One single when step per scenario | ||
Blabla... | ||
|
||
Scenario Outline: Scenario 1 - One single when step per scenario | ||
Given Blabla given... | ||
# Noncompliant [[sc=5;ec=33;secondary=+1]] {{Merge all the When steps or split the scenario up in multiple scenarios.}} | ||
When Blabla when... <number> | ||
And Blabla when and blabla when | ||
Then Blabla then... | ||
Examples: | ||
| number | | ||
| 1 | | ||
| 2 | | ||
|
||
Scenario: Scenario 2 - One single when step per scenario | ||
Given Blabla given... | ||
# Noncompliant [[sc=5;ec=24;secondary=+1]] {{Merge all the When steps or split the scenario up in multiple scenarios.}} | ||
When Blabla when... | ||
When Blabla when and blabla when... | ||
Then Blabla then... |
20 changes: 20 additions & 0 deletions
20
its/ruling/projects/custom/one-single-when-per-scenario.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Feature: My feature One single when step per scenario | ||
Blabla... | ||
|
||
Scenario Outline: Scenario 1 - One single when step per scenario | ||
Given Blabla given... | ||
# Noncompliant [[sc=5;ec=33;secondary=+1]] {{Merge all the When steps or split the scenario up in multiple scenarios.}} | ||
When Blabla when... <number> | ||
And Blabla when and blabla when | ||
Then Blabla then... | ||
Examples: | ||
| number | | ||
| 1 | | ||
| 2 | | ||
|
||
Scenario: Scenario 2 - One single when step per scenario | ||
Given Blabla given... | ||
# Noncompliant [[sc=5;ec=24;secondary=+1]] {{Merge all the When steps or split the scenario up in multiple scenarios.}} | ||
When Blabla when... | ||
When Blabla when and blabla when... | ||
Then Blabla then... |
21 changes: 21 additions & 0 deletions
21
its/ruling/tests/src/test/expected/gherkin-one-single-when-per-scenario.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
'project:custom/duplicated-steps/duplicates-in-background-and-scenarios.feature':[ | ||
19, | ||
], | ||
'project:custom/duplicated-steps/duplicates-in-scenarios.feature':[ | ||
17, | ||
], | ||
'project:custom/one-single-when-per-scenario.feature':[ | ||
7, | ||
18, | ||
], | ||
'project:custom/steps-right-order.feature':[ | ||
6, | ||
], | ||
'project:custom/use-and-but.feature':[ | ||
25, | ||
], | ||
'project:custom/when-step-regular-expression.feature':[ | ||
7, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
'project:custom/one-single-when-per-scenario.feature':[ | ||
19, | ||
], | ||
'project:custom/use-and-but.feature':[ | ||
7, | ||
14, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters