Groovy Documentation

groovy.org.codenarc.rule
[Groovy] Class AbstractRuleTestCase

java.lang.Object
  org.codenarc.test.AbstractTestCase
      groovy.org.codenarc.rule.AbstractRuleTestCase

@SuppressWarnings('DuplicateLiteral')
abstract class AbstractRuleTestCase
extends AbstractTestCase

Abstract superclass for tests of Rule classes

Authors:
Chris Mair
Hamlet D'Arcy


Field Summary
protected static def CONSTRUCTOR_METHOD_NAME

protected static def DEFAULT_TEST_CLASS_NAMES

protected static def DEFAULT_TEST_FILES

protected Rule rule

protected String sourceCodeName

protected String sourceCodePath

 
Method Summary
protected List applyRuleTo(String source)

Apply the current Rule to the specified source (String) and return the resulting List of Violations.

protected void assertInlineViolations(String annotatedSource)

Apply the current Rule to the specified source (String) and assert that it results in the violations specified inline within the source.

protected void assertNoViolations(String source)

Apply the current Rule to the specified source (String) and assert that it results in no violations.

protected void assertSingleViolation(String source, Integer lineNumber = null, String sourceLineText = null, def messageText = null)

Apply the current Rule to the specified source (String) and assert that it results in a single violation with the specified line number and containing the specified source text.

protected void assertSingleViolation(String source, groovy.lang.Closure closure)

Apply the current Rule to the specified source (String) and assert that it results in a single violation and that the specified closure returns true.

protected void assertTwoViolations(String source, Integer lineNumber1, String sourceLineText1, Integer lineNumber2, String sourceLineText2)

Apply the current Rule to the specified source (String) and assert that it results in two violations with the specified line numbers and containing the specified source text values.

protected void assertTwoViolations(String source, Integer lineNumber1, String sourceLineText1, def msg1, Integer lineNumber2, String sourceLineText2, def msg2)

Apply the current Rule to the specified source (String) and assert that it results in two violations with the specified line numbers and containing the specified source text values.

protected void assertViolation(Violation violation, Integer lineNumber, String sourceLineText, def messageText = null)

Assert that the specified violation is for the current rule, and has expected line number and contains the specified source text and message text.

protected void assertViolations(String source, Map[] violationMaps)

Apply the current Rule to the specified source (String) and assert that it results in the violations specified in violationMaps.

protected Rule createRule()

Create and return a new instance of the Rule class to be tested.

protected static String inlineViolation(String violationMessage)

Prepares an inline violation with a given message, escaping all '#' characters and preventing accidental escaping of next inline violation's start when the message ends with a '\' character.

protected List manuallyApplyRule(String source)

Apply the current Rule to the specified source (String) and return the resulting List of Violations.

protected static String removeInlineViolations(String annotatedSource)

Removes all inline violations from a source.

void setUpAbstractRuleTestCase()

void testThatApplyToFilesMatchingValuesAreValidRegex()

void testThatInvalidCodeHasNoViolations()

void testThatUnrelatedCodeHasNoViolations()

Make sure that code unrelated to the rule under test causes no violations.

 

Field Detail

CONSTRUCTOR_METHOD_NAME

protected static final def CONSTRUCTOR_METHOD_NAME


DEFAULT_TEST_CLASS_NAMES

protected static final def DEFAULT_TEST_CLASS_NAMES


DEFAULT_TEST_FILES

protected static final def DEFAULT_TEST_FILES


rule

protected Rule rule


sourceCodeName

protected String sourceCodeName


sourceCodePath

protected String sourceCodePath


 
Method Detail

applyRuleTo

protected List applyRuleTo(String source)
Apply the current Rule to the specified source (String) and return the resulting List of Violations.
Parameters:
source - - the full source code to which the rule is applied, as a String


assertInlineViolations

protected void assertInlineViolations(String annotatedSource)
Apply the current Rule to the specified source (String) and assert that it results in the violations specified inline within the source.

Inline violations can be specified either by using the inlineViolation(java.lang.String) method or simply by prefixing a violation message with a '#'. Multiple inline violations per line are allowed.

One can prevent a '#' character from starting a violation message by escaping it with a '\' character (keep in mind that most of Groovy's string literal syntax demands the '\' to be escaped itself, as a '\\' sequence).

For every source line all text after the first non-escaped '#' character is part of some inline violation message (with the sole exception of the first line of a Groovy script beginning with a shebang). More precisely, every '#' character that is neither escaped nor part of a shebang starts an inline violation that spans to the end of its line or until next non-escaped '#' character.

See the inlineViolation(java.lang.String) method.
See the removeInlineViolations(java.lang.String) method.

Parameters:
source - - the full source code to which the rule is applied annotated with inline violations, as a String


assertNoViolations

protected void assertNoViolations(String source)
Apply the current Rule to the specified source (String) and assert that it results in no violations.
Parameters:
source - - the full source code to which the rule is applied, as a String


assertSingleViolation

protected void assertSingleViolation(String source, Integer lineNumber = null, String sourceLineText = null, def messageText = null)
Apply the current Rule to the specified source (String) and assert that it results in a single violation with the specified line number and containing the specified source text.
Parameters:
source - - the full source code to which the rule is applied, as a String
lineNumber - - the expected line number in the resulting violation; defaults to null
sourceLineText - - the text expected within the sourceLine of the resulting violation; defaults to null
messageText - - the text expected within the message of the resulting violation; May be a String or List of Strings; Defaults to null;


assertSingleViolation

protected void assertSingleViolation(String source, groovy.lang.Closure closure)
Apply the current Rule to the specified source (String) and assert that it results in a single violation and that the specified closure returns true.
Parameters:
source - - the full source code to which the rule is applied, as a String; defaults to null
closure - - the closure to apply to the violation; takes a single Violation parameter


assertTwoViolations

protected void assertTwoViolations(String source, Integer lineNumber1, String sourceLineText1, Integer lineNumber2, String sourceLineText2)
Apply the current Rule to the specified source (String) and assert that it results in two violations with the specified line numbers and containing the specified source text values.
Parameters:
source - - the full source code to which the rule is applied, as a String
lineNumber1 - - the expected line number in the first violation
sourceLineText1 - - the text expected within the sourceLine of the first violation
lineNumber2 - - the expected line number in the second violation
sourceLineText2 - - the text expected within the sourceLine of the second violation


assertTwoViolations

@SuppressWarnings('ParameterCount')
protected void assertTwoViolations(String source, Integer lineNumber1, String sourceLineText1, def msg1, Integer lineNumber2, String sourceLineText2, def msg2)
Apply the current Rule to the specified source (String) and assert that it results in two violations with the specified line numbers and containing the specified source text values.
Parameters:
source - - the full source code to which the rule is applied, as a String
lineNumber1 - - the expected line number in the first violation
sourceLineText1 - - the text expected within the sourceLine of the first violation
msg1 - - the text expected within the message of the first violation; May be a String or List of Strings; Defaults to null;
lineNumber2 - - the expected line number in the second violation
sourceLineText2 - - the text expected within the sourceLine of the second violation
msg2 - - the text expected within the message of the second violation; May be a String or List of Strings; Defaults to null;


assertViolation

protected void assertViolation(Violation violation, Integer lineNumber, String sourceLineText, def messageText = null)
Assert that the specified violation is for the current rule, and has expected line number and contains the specified source text and message text.
Parameters:
violation - - the Violation
lineNumber - - the expected line number in the resulting violation
sourceLineText - - the text expected within the sourceLine of the resulting violation; may be null
messageText - - the text expected within the message of the resulting violation; May be a String or List of Strings; Defaults to null;


assertViolations

protected void assertViolations(String source, Map[] violationMaps)
Apply the current Rule to the specified source (String) and assert that it results in the violations specified in violationMaps.
Parameters:
source - - the full source code to which the rule is applied, as a String
violationMaps - - a list (array) of Maps, each describing a single violation. Each element in the map can contain a lineNumber, sourceLineText and messageText entries.


createRule

protected Rule createRule()
Create and return a new instance of the Rule class to be tested.
Returns:
a new Rule instance


inlineViolation

protected static String inlineViolation(String violationMessage)
Prepares an inline violation with a given message, escaping all '#' characters and preventing accidental escaping of next inline violation's start when the message ends with a '\' character.
Returns:
a String that will be interpreted as an inline violation by the assertInlineViolations(java.lang.String) method
Parameters:
violationMessage - message for the inline violation


manuallyApplyRule

protected List manuallyApplyRule(String source)
Apply the current Rule to the specified source (String) and return the resulting List of Violations.
Parameters:
source - - the full source code to which the rule is applied, as a String


removeInlineViolations

protected static String removeInlineViolations(String annotatedSource)
Removes all inline violations from a source.
Returns:
the given source with inline violations removed
Parameters:
annotatedSource - source possibly containing inline violations


setUpAbstractRuleTestCase

@Before
void setUpAbstractRuleTestCase()


testThatApplyToFilesMatchingValuesAreValidRegex

@Test
void testThatApplyToFilesMatchingValuesAreValidRegex()


testThatInvalidCodeHasNoViolations

@Test
void testThatInvalidCodeHasNoViolations()


testThatUnrelatedCodeHasNoViolations

@Test
void testThatUnrelatedCodeHasNoViolations()
Make sure that code unrelated to the rule under test causes no violations. Subclasses can skip this rule by defining a property named 'skipTestThatUnrelatedCodeHasNoViolations'.


 

Groovy Documentation