Skip to content
Browse files

Some corrections, mostly to align with conventions

renamed RegexChecker parameter expression -> regex
removed println in RegexCheckerTest I've added the test to the standard config file for scalastyle
made private methods in RegexChecker private
removed vals for lines & columns in RegexCheckerTest, it makes it easier to read
  • Loading branch information...
1 parent 6eb424d commit 4b78adee9352e23c840a1e019edc77713c5949cc @matthewfarwell matthewfarwell committed
View
5 lib/scalastyle_config.xml
@@ -73,4 +73,9 @@
<check level="warning" class="org.scalastyle.scalariform.NoFinalizeChecker" enabled="true"></check>
<check level="warning" class="org.scalastyle.scalariform.CovariantEqualsChecker" enabled="true"></check>
<check level="warning" class="org.scalastyle.scalariform.StructuralTypeChecker" enabled="true"></check>
+ <check level="warning" class="org.scalastyle.file.RegexChecker" enabled="true">
+ <parameters>
+ <parameter name="regex">println</parameter>
+ </parameters>
+ </check>
</scalastyle>
View
2 src/main/resources/scalastyle_definition.xml
@@ -62,7 +62,7 @@
<checker class="org.scalastyle.scalariform.StructuralTypeChecker" id="structural.type" defaultLevel="warning" />
<checker class="org.scalastyle.file.RegexChecker" id="regex" defaultLevel="warning" >
<parameters>
- <parameter name="expression" type="string" default="" />
+ <parameter name="regex" type="string" default="" />
</parameters>
</checker>
</scalastyle-definition>
View
4 src/main/resources/scalastyle_messages.properties
@@ -112,6 +112,6 @@ package.object.name.regex.description = The package object names must match this
regex.message = Regular expression matched ''{0}''
regex.label = Regular expression
regex.description = Checks that a regular expression cannot be matched, if found reports this
-regex.expression.label = Regular expression
-regex.expression.description = Standard Scala regular expression syntax including multiline
+regex.regex.label = Regular expression
+regex.regex.description = Standard Scala regular expression syntax including multiline
View
4 src/main/scala/org/scalastyle/Main.scala
@@ -58,12 +58,16 @@ object Main {
val configuration = ScalastyleConfiguration.readFromXml(config.config.get)
val messages = new ScalastyleChecker().checkFiles(configuration, Directory.getFiles(config.directories.map(s => new File(s)): _*))
+ // scalastyle:off regex
+
val outputResult = new TextOutput().output(messages)
if (!config.quiet) println("Processed " + outputResult.files + " file(s)")
if (!config.quiet) println("Found " + outputResult.errors + " errors")
if (!config.quiet) println("Found " + outputResult.warnings + " warnings")
if (!config.quiet) println("Finished in " + (now - start) + " ms")
+ // scalastyle:on regex
+
outputResult.errors > 0 || (config.warningsaserrors && outputResult.warnings > 0)
}
}
View
2 src/main/scala/org/scalastyle/Output.scala
@@ -54,6 +54,7 @@ case class OutputResult(files: Int, errors: Int, warnings: Int)
class TextOutput[T <: FileSpec](verbose: Boolean = false, quiet: Boolean = false) extends Output[T] {
private val messageHelper = new MessageHelper(this.getClass().getClassLoader())
+ // scalastyle:off regex
override def message(m: Message[T]): Unit = m match {
case StartWork() => if (verbose) println("Starting scalastyle")
case EndWork() =>
@@ -68,6 +69,7 @@ class TextOutput[T <: FileSpec](verbose: Boolean = false, quiet: Boolean = false
println("error" + print("file", file.name) + print("message", message) + print("line", line) + print("column", column))
}
}
+ // scalastyle:on regex
private def print(s: String, no: Option[Int]): String = if (no.isDefined) print(s, "" + no.get) else ""
private def print(s: String, value: String): String = " " + s + "=" + value
View
6 src/main/scala/org/scalastyle/file/RegexChecker.scala
@@ -25,7 +25,7 @@ class RegexChecker extends FileChecker {
def verify(lines: Lines): List[ScalastyleError] = {
val file = (for (line <- lines.lines) yield line.text).mkString("\n")
- val regExpStr = getString("expression", DefaultRegEx)
+ val regExpStr = getString("regex", DefaultRegEx)
val regExp = new Regex(regExpStr)
val allMatches = regExp.findAllIn(file)
var errorList: List[ColumnError] = Nil
@@ -41,7 +41,7 @@ class RegexChecker extends FileChecker {
errorList.reverse
}
- def findCorrespondingLine(location: Int, data: String, lines: Lines): Int = {
+ private[this] def findCorrespondingLine(location: Int, data: String, lines: Lines): Int = {
var line = 0
var found = false
@@ -57,7 +57,7 @@ class RegexChecker extends FileChecker {
line
}
- def findColumnPosition(location: Int, lines: Lines, matchedLine: Int): Int = {
+ private[this] def findColumnPosition(location: Int, lines: Lines, matchedLine: Int): Int = {
location - (lines.lines(matchedLine).start)
}
}
View
45 src/test/scala/org/scalastyle/file/RegexCheckerTest.scala
@@ -19,6 +19,8 @@ package org.scalastyle.file
import org.junit.Test
import org.scalatest.junit.AssertionsForJUnit
+// scalastyle:off magic.number
+
class RegexCheckerTest extends AssertionsForJUnit with CheckerTest {
val key = "regex"
val classUnderTest = classOf[RegexChecker]
@@ -30,73 +32,60 @@ package foobar
class foobar {
def aMethod: String = {
- println("SHOULD NOT BE HERE")
+ ("SHOULD NOT BE HERE")
"TEST STRING";
}
}
"""
- // scalastyle:off magic.number
- private val LineOne = 1
- private val LineThree = 3
- private val LineFive = 5
- private val LineSeven = 7
- private val LineNine = 9
- private val LineTen = 10
- private val LineTwelve = 12
- private val ColumnZero: Int = 0
- private val ColumnTwo: Int = 2
- private val ColumnSeventeen: Int = 17
- // scalastyle:on magic.number
-
@Test
def testSimpleCheck() {
- assertErrors(List(columnError(LineSeven, ColumnTwo, List("def"))), source,
- Map("expression" -> "def"))
+ assertErrors(List(columnError(7, 2, List("def"))), source,
+ Map("regex" -> "def"))
}
@Test
def testNoSemiColon() {
- assertErrors(List(columnError(LineNine, ColumnSeventeen, List(";"))), source,
- Map("expression" -> ";"))
+ assertErrors(List(columnError(9, 17, List(";"))), source,
+ Map("regex" -> ";"))
}
@Test
def testStartOfLineIsZeroColumn() {
- assertErrors(List(columnError(LineFive, ColumnZero, List("class"))), source,
- Map("expression" -> "class"))
+ assertErrors(List(columnError(5, 0, List("class"))), source,
+ Map("regex" -> "class"))
}
@Test
def testCanMatchLastCharInFile() {
- assertErrors(List(columnError(LineTwelve, ColumnZero, List("(?m)^}$"))), source, Map("expression" -> "(?m)^}$"))
+ assertErrors(List(columnError(12, 0, List("(?m)^}$"))), source, Map("regex" -> "(?m)^}$"))
}
@Test
def testCanMatchFirstCharInFile() {
- assertErrors(List(columnError(LineOne, ColumnZero, List("(?m)^//$"))), source, Map("expression" -> "(?m)^//$"))
+ assertErrors(List(columnError(1, 0, List("(?m)^//$"))), source, Map("regex" -> "(?m)^//$"))
}
@Test
def testNoDoubleBlankLines() {
- assertErrors(List(columnError(LineThree, ColumnZero, List("(?m)^\\s*$(\\r|)\\n^\\s*$(\\r|)\\n"))), source,
- Map("expression" -> "(?m)^\\s*$(\\r|)\\n^\\s*$(\\r|)\\n"))
+ assertErrors(List(columnError(3, 0, List("(?m)^\\s*$(\\r|)\\n^\\s*$(\\r|)\\n"))), source,
+ Map("regex" -> "(?m)^\\s*$(\\r|)\\n^\\s*$(\\r|)\\n"))
}
@Test
def testMultipleMatchesReportMultipleErrors() {
- assertErrors(List(columnError(LineTen, ColumnTwo, List("}")), columnError(LineTwelve, ColumnZero, List("}"))), source,
- Map("expression" -> "}"))
+ assertErrors(List(columnError(10, 2, List("}")), columnError(12, 0, List("}"))), source,
+ Map("regex" -> "}"))
}
@Test
def testCannotFindMatch() {
- assertErrors(List(), source, Map("expression" -> "^SHOULD$"))
+ assertErrors(List(), source, Map("regex" -> "^SHOULD$"))
}
@Test
def testSingleMatchWithBoundsCheck() {
- assertErrors(List(columnError(LineTwelve, ColumnZero, List("(?m)^}$"))), source, Map("expression" -> "(?m)^}$"))
+ assertErrors(List(columnError(12, 0, List("(?m)^}$"))), source, Map("regex" -> "(?m)^}$"))
}
}

0 comments on commit 4b78ade

Please sign in to comment.
Something went wrong with that request. Please try again.