-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from joesondow/custom-codenarc-rule-consecuti…
…ve-blank-lines Custom codenarc rule ConsecutiveBlankLines
- Loading branch information
Showing
10 changed files
with
173 additions
and
9 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
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 |
---|---|---|
|
@@ -287,5 +287,3 @@ class Relationships { | |
parts | ||
} | ||
} | ||
|
||
|
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
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
45 changes: 45 additions & 0 deletions
45
test/unit/com/netflix/asgard/codenarc/ConsecutiveBlankLinesRule.groovy
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,45 @@ | ||
/* | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.asgard.codenarc | ||
|
||
import org.codenarc.rule.AbstractRule | ||
import org.codenarc.source.SourceCode | ||
|
||
/** | ||
* Makes sure there are no consecutive lines that are either blank or whitespace only. | ||
*/ | ||
class ConsecutiveBlankLinesRule extends AbstractRule { | ||
|
||
String name = 'ConsecutiveBlankLines' | ||
int priority = 3 | ||
|
||
/** | ||
* Apply the rule to the given source, writing violations to the given list. | ||
* @param sourceCode The source to check | ||
* @param violations A list of Violations that may be added to. It can be an empty list | ||
*/ | ||
@Override | ||
void applyTo(SourceCode sourceCode, List violations) { | ||
|
||
List<String> lines = sourceCode.getLines() | ||
for (int index = 1; index < lines.size(); index++) { | ||
String line = lines[index] | ||
if (line.trim().isEmpty() && lines[index - 1].trim().isEmpty()) { | ||
violations.add(createViolation(index, line, "File $sourceCode.name has consecutive blank lines")) | ||
} | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
test/unit/com/netflix/asgard/codenarc/ConsecutiveBlankLinesRuleTest.groovy
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,103 @@ | ||
/* | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.asgard.codenarc | ||
|
||
import org.codenarc.rule.AbstractRuleTestCase | ||
import org.codenarc.rule.Rule | ||
import org.junit.Test | ||
|
||
@SuppressWarnings('ConsecutiveBlankLines') | ||
class ConsecutiveBlankLinesRuleTest extends AbstractRuleTestCase { | ||
|
||
@Test | ||
void testRuleProperties() { | ||
assert rule.priority == 3 | ||
assert rule.name == 'ConsecutiveBlankLines' | ||
} | ||
|
||
@Test | ||
void testSuccessScenario() { | ||
final SOURCE = ''' | ||
class MyClass { | ||
def go() { /* ... */ } | ||
def goSomewhere() { /* ... */ } | ||
def build() { /* ... */ } | ||
def buildSomething() { /* ... */ } | ||
} | ||
''' | ||
assertNoViolations(SOURCE) | ||
} | ||
|
||
@Test | ||
void testClassStartsWithDoubleBlankLines() { | ||
final SOURCE = ''' | ||
class MyClass { | ||
void go() { /* ... */ } | ||
} | ||
''' | ||
assertSingleViolation(SOURCE, 3, '', "File null has consecutive blank lines") | ||
} | ||
|
||
@Test | ||
void testFileStartsWithDoubleBlankLines() { | ||
final SOURCE = ''' | ||
class MyClass { | ||
void go() { /* ... */ } | ||
} | ||
''' | ||
assertSingleViolation(SOURCE, 1, '', "File null has consecutive blank lines") | ||
} | ||
|
||
@Test | ||
void testDoubleBlankLinesBetweenMethods() { | ||
final SOURCE = ''' | ||
class MyClass { | ||
void go() { /* ... */ } | ||
void stop() { /* ... */ } | ||
void run() { /* ... */ } | ||
} | ||
''' | ||
assertTwoViolations(SOURCE, 4, '', 7, '') | ||
} | ||
|
||
@Test | ||
void testTripleBlankLines() { | ||
final SOURCE = ''' | ||
class MyClass { | ||
void go() { /* ... */ } | ||
void stop() { /* ... */ } | ||
} | ||
''' | ||
assertTwoViolations(SOURCE, 4, '', 5, '') | ||
} | ||
|
||
protected Rule createRule() { | ||
new ConsecutiveBlankLinesRule() | ||
} | ||
} |