-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #46 New rule: Features should be written in the defined language
- Loading branch information
Showing
19 changed files
with
302 additions
and
7 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
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
90 changes: 90 additions & 0 deletions
90
gherkin-checks/src/main/java/org/sonar/gherkin/checks/DefinedFeatureLanguageCheck.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,90 @@ | ||
/* | ||
* SonarQube Gherkin Analyzer | ||
* Copyright (C) 2016-2016 David RACODON | ||
* david.racodon@gmail.com | ||
* | ||
* 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.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.check.RuleProperty; | ||
import org.sonar.gherkin.parser.GherkinDialectProvider; | ||
import org.sonar.plugins.gherkin.api.tree.GherkinDocumentTree; | ||
import org.sonar.plugins.gherkin.api.visitors.DoubleDispatchVisitorCheck; | ||
import org.sonar.squidbridge.annotations.SqaleConstantRemediation; | ||
|
||
import java.util.Set; | ||
|
||
@Rule( | ||
key = "defined-feature-language", | ||
name = "Features should be written in the defined language", | ||
priority = Priority.MAJOR, | ||
tags = {Tags.DESIGN}) | ||
@SqaleConstantRemediation("30min") | ||
public class DefinedFeatureLanguageCheck extends DoubleDispatchVisitorCheck { | ||
|
||
private static final String DEFAULT_LANGUAGE = GherkinDialectProvider.DEFAULT_LANGUAGE; | ||
|
||
private static final Set<String> SUPPORTED_LANGUAGES = ImmutableSet.of("af", "am", "ar", "ast", "az", "bg", "bm", | ||
"bs", "ca", "cs", "cy-GB", "da", "de", "el", "em", "en", "en-Scouse", "en-au", "en-lol", "en-old", "en-pirate", | ||
"eo", "es", "et", "fa", "fi", "fr", "ga", "gj", "gl", "he", "hi", "hr", "ht", "hu", "id", "is", "it", "ja", "jv", | ||
"ka", "kn", "ko", "lt", "lu", "lv", "mn", "nl", "no", "pa", "pl", "pt", "ro", "ru", "sk", "sl", "sr-Cyrl", | ||
"sr-Latn", "sv", "ta", "th", "tl", "tlh", "tr", "tt", "uk", "ur", "uz", "vi", "zh-CN", "zh-TW"); | ||
|
||
private static final String SUPPORTED_LANGUAGES_AS_STRING = "af, am, ar, ast, az, bg, bm, bs, ca, cs, " | ||
+ "cy-GB, da, de, el, em, en, en-Scouse, en-au, en-lol, en-old, en-pirate, eo, es, et, fa, fi, fr, ga, gj, gl, " | ||
+ "he, hi, hr, ht, hu, id, is, it, ja, jv, ka, kn, ko, lt, lu, lv, mn, nl, no, pa, pl, pt, ro, ru, sk, sl, " | ||
+ "sr-Cyrl, sr-Latn, sv, ta, th, tl, tlh, tr, tt, uk, ur, uz, vi, zh-CN, zh-TW"; | ||
|
||
@RuleProperty( | ||
key = "language", | ||
description = "Language of the Gherkin documents. Allowed values are: " + SUPPORTED_LANGUAGES_AS_STRING, | ||
defaultValue = DEFAULT_LANGUAGE) | ||
private String language = DEFAULT_LANGUAGE; | ||
|
||
@Override | ||
public void visitGherkinDocument(GherkinDocumentTree tree) { | ||
if (!tree.language().equals(language)) { | ||
if (tree.languageDeclaration() != null) { | ||
addPreciseIssue(tree.languageDeclaration(), "Update the language definition to '" + language + "' and translate the content of the file."); | ||
} else { | ||
addFileIssue("Add a '" + language + "' language definition and translate the content of the file."); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void validateParameters() { | ||
if (!SUPPORTED_LANGUAGES.contains(language)) { | ||
throw new IllegalStateException(languageParamErrorMessage()); | ||
} | ||
} | ||
|
||
private String languageParamErrorMessage() { | ||
return CheckUtils.paramErrorMessage( | ||
this.getClass(), | ||
"language parameter \"" + language + "\" is not valid. Allowed values are: " + SUPPORTED_LANGUAGES_AS_STRING); | ||
} | ||
|
||
@VisibleForTesting | ||
void setLanguage(String language) { | ||
this.language = language; | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...cks/src/main/resources/org/sonar/l10n/gherkin/rules/gherkin/defined-feature-language.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,3 @@ | ||
<p> | ||
This rule checks that all the Gherkin documents are written in the defined language. | ||
</p> |
88 changes: 88 additions & 0 deletions
88
gherkin-checks/src/test/java/org/sonar/gherkin/checks/DefinedFeatureLanguageCheckTest.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,88 @@ | ||
/* | ||
* SonarQube Gherkin Analyzer | ||
* Copyright (C) 2016-2016 David RACODON | ||
* david.racodon@gmail.com | ||
* | ||
* 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; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
public class DefinedFeatureLanguageCheckTest { | ||
|
||
@Test | ||
public void should_be_written_in_the_default_language_and_not_raise_any_issue() { | ||
GherkinCheckVerifier.issues( | ||
new DefinedFeatureLanguageCheck(), | ||
CheckTestUtils.getTestFile("defined-feature-language/default.feature")) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void should_be_written_in_the_custom_language_and_not_raise_any_issue() { | ||
DefinedFeatureLanguageCheck check = new DefinedFeatureLanguageCheck(); | ||
check.setLanguage("fr"); | ||
|
||
GherkinCheckVerifier.issues( | ||
check, | ||
CheckTestUtils.getTestFile("defined-feature-language/fr.feature"), | ||
"fr") | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void should_not_be_written_in_the_default_language_and_raise_an_issue() { | ||
GherkinCheckVerifier.issues( | ||
new DefinedFeatureLanguageCheck(), | ||
CheckTestUtils.getTestFile("defined-feature-language/not-default.feature"), | ||
"fr") | ||
.next().atLine(1).withMessage("Update the language definition to 'en' and translate the content of the file.") | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void should_not_be_written_in_the_custom_language_and_raise_an_issue() { | ||
DefinedFeatureLanguageCheck check = new DefinedFeatureLanguageCheck(); | ||
check.setLanguage("fr"); | ||
|
||
GherkinCheckVerifier.issues( | ||
check, | ||
CheckTestUtils.getTestFile("defined-feature-language/not-fr.feature")) | ||
.next().withMessage("Add a 'fr' language definition and translate the content of the file.") | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void should_throw_an_illegal_state_exception_as_the_language_parameter_is_not_valid() { | ||
try { | ||
DefinedFeatureLanguageCheck check = new DefinedFeatureLanguageCheck(); | ||
check.setLanguage("abc"); | ||
|
||
GherkinCheckVerifier.issues(check, CheckTestUtils.getTestFile("defined-feature-language/default.feature")).noMore(); | ||
|
||
} catch (IllegalStateException e) { | ||
assertThat(e.getMessage()).isEqualTo("Check gherkin:defined-feature-language (Features should be written in the defined language): " | ||
+ "language parameter \"abc\" is not valid. Allowed values are: af, am, ar, ast, az, bg, bm, bs, ca, cs, " | ||
+ "cy-GB, da, de, el, em, en, en-Scouse, en-au, en-lol, en-old, en-pirate, eo, es, et, fa, fi, fr, ga, gj, gl, " | ||
+ "he, hi, hr, ht, hu, id, is, it, ja, jv, ka, kn, ko, lt, lu, lv, mn, nl, no, pa, pl, pt, ro, ru, sk, sl, " | ||
+ "sr-Cyrl, sr-Latn, sv, ta, th, tl, tlh, tr, tt, uk, ur, uz, vi, zh-CN, zh-TW"); | ||
} | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
gherkin-checks/src/test/resources/checks/defined-feature-language/default.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,7 @@ | ||
Feature: My feature - defined feature language default | ||
Blabla... | ||
|
||
Scenario: My scenario - defined feature language default | ||
Given Blabla given... | ||
When Blabla when... | ||
Then Blabla then... |
8 changes: 8 additions & 0 deletions
8
gherkin-checks/src/test/resources/checks/defined-feature-language/fr.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,8 @@ | ||
# language: fr | ||
Fonctionnalité: My feature - defined feature language french | ||
Blabla... | ||
|
||
Scénario: My scenario - defined feature language french | ||
Soit Blabla given... | ||
Lorsque Blabla when... | ||
Alors Blabla then... |
8 changes: 8 additions & 0 deletions
8
gherkin-checks/src/test/resources/checks/defined-feature-language/not-default.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,8 @@ | ||
# language: fr | ||
Fonctionnalité: My feature - defined feature language not default | ||
Blabla... | ||
|
||
Scénario: My scenario - defined feature language not default | ||
Soit Blabla given... | ||
Lorsque Blabla when... | ||
Alors Blabla then... |
7 changes: 7 additions & 0 deletions
7
gherkin-checks/src/test/resources/checks/defined-feature-language/not-fr.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,7 @@ | ||
Feature: My feature - defined feature language not french | ||
Blabla... | ||
|
||
Scenario: My scenario - defined feature language not french | ||
Given Blabla given... | ||
When Blabla when... | ||
Then Blabla then... |
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
7 changes: 7 additions & 0 deletions
7
its/ruling/projects/custom/defined-feature-language/default.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,7 @@ | ||
Feature: My feature - defined feature language default | ||
Blabla... | ||
|
||
Scenario: My scenario - defined feature language default | ||
Given Blabla given... | ||
When Blabla when... | ||
Then Blabla then... |
8 changes: 8 additions & 0 deletions
8
its/ruling/projects/custom/defined-feature-language/fr.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,8 @@ | ||
# language: fr | ||
Fonctionnalité: My feature - defined feature language french | ||
Blabla... | ||
|
||
Scénario: My scenario - defined feature language french | ||
Soit Blabla given... | ||
Lorsque Blabla when... | ||
Alors Blabla then... |
8 changes: 8 additions & 0 deletions
8
its/ruling/projects/custom/defined-feature-language/not-default.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,8 @@ | ||
# language: fr | ||
Fonctionnalité: My feature - defined feature language not default | ||
Blabla... | ||
|
||
Scénario: My scenario - defined feature language not default | ||
Soit Blabla given... | ||
Lorsque Blabla when... | ||
Alors Blabla then... |
7 changes: 7 additions & 0 deletions
7
its/ruling/projects/custom/defined-feature-language/not-fr.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,7 @@ | ||
Feature: My feature - defined feature language not french | ||
Blabla... | ||
|
||
Scenario: My scenario - defined feature language not french | ||
Given Blabla given... | ||
When Blabla when... | ||
Then Blabla then... |
11 changes: 11 additions & 0 deletions
11
its/ruling/tests/src/test/expected/gherkin-defined-feature-language.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,11 @@ | ||
{ | ||
'project:custom/defined-feature-language/fr.feature':[ | ||
1, | ||
], | ||
'project:custom/defined-feature-language/not-default.feature':[ | ||
1, | ||
], | ||
'project:custom/french.feature':[ | ||
1, | ||
], | ||
} |
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