Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added first scalatests

  • Loading branch information...
commit e0336c33a754e35bc74e98901a6b279250e48694 1 parent 8cdebfb
@matthewfarwell authored
View
BIN  .cache
Binary file not shown
View
55 pom.xml
@@ -8,21 +8,35 @@
<version>0.0.1</version>
<repositories>
<repository>
+ <releases>
+ <enabled>false</enabled>
+ <updatePolicy>always</updatePolicy>
+ <checksumPolicy>warn</checksumPolicy>
+ </releases>
+ <snapshots>
+ <updatePolicy>never</updatePolicy>
+ <checksumPolicy>fail</checksumPolicy>
+ </snapshots>
+ <id>repo1</id>
+ <name>repo1</name>
+ <url>http://repo1.maven.org/maven2</url>
+ </repository>
+ <repository>
<id>java.net</id>
<name>java.net</name>
<url>http://download.java.net/maven/2/</url>
</repository>
-
- <repository>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- <id>scala-tools</id>
- <name>Scala Tools</name>
- <url>http://scala-tools.org/repo-releases/</url>
- </repository>
- </repositories>
-
+
+ <repository>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ <id>scala-tools</id>
+ <name>Scala Tools</name>
+ <url>http://scala-tools.org/repo-releases/</url>
+ </repository>
+ </repositories>
+
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
@@ -30,15 +44,24 @@
<version>2.9.1</version>
</dependency>
<dependency>
- <groupId>org.scala-lang</groupId>
- <artifactId>scala-compiler</artifactId>
- <version>2.9.1</version>
- </dependency>
- <dependency>
<groupId>org.scalariform</groupId>
<artifactId>scalariform_2.9.1</artifactId>
<version>0.1.1</version>
</dependency>
+ <dependency>
+ <groupId>org.scalatest</groupId>
+ <artifactId>scalatest_2.9.1</artifactId>
+ <version>1.6.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
<build>
View
4 src/main/scala/org/segl/scalastyle/Checker.scala
@@ -8,5 +8,7 @@ object ScalastyleChecker {
}
class ScalastyleChecker {
- def checkFiles(files: List[String]): List[Message] = StartWork() :: files.flatMap(file => Checker.verify(ScalastyleChecker.checkers, file)).toList ::: List(EndWork())
+ def checkFiles(files: List[String]): List[Message] = {
+ StartWork() :: files.flatMap(file => Checker.verifyFile(ScalastyleChecker.checkers, file)).toList ::: List(EndWork())
+ }
}
View
29 src/main/scala/org/segl/scalastyle/FileTabChecker.scala
@@ -9,21 +9,21 @@ import _root_.scalariform.lexer.Token
import _root_.scalariform.lexer.Tokens._
import scala.io.Source
-case class Lines(lines: List[String])
+case class Lines(lines: Array[String])
object Checker {
type CheckerClass = Class[_ <: Checker]
- def parseScalariform(file: String) = {
- val (hiddenTokenInfo, tokens) = ScalaLexer.tokeniseFull(Source.fromFile(file).mkString)
+ def parseScalariform(source: String) = {
+ val (hiddenTokenInfo, tokens) = ScalaLexer.tokeniseFull(source, true)
new ScalaParser(tokens.toArray).compilationUnitOrScript()
}
- private def parseLines(file: String): Lines = Lines(Source.fromFile(file).getLines.toList)
+ private def parseLines(source: String): Lines = Lines(source.split("\n"));
- def verify(classes: List[CheckerClass], file: String): List[Message] = {
- val lines = parseLines(file)
- val scalariformAst = parseScalariform(file)
+ def verifySource(classes: List[CheckerClass], file: String, source: String): List[Message] = {
+ lazy val lines = parseLines(source)
+ lazy val scalariformAst = parseScalariform(source)
classes.map(clazz => newInstance(clazz)).flatMap(c => c match {
case c: LinesChecker => c.verify(file, lines)
@@ -31,7 +31,8 @@ object Checker {
case _ => List[Message]()
})
}
-
+
+ def verifyFile(classes: List[CheckerClass], file: String): List[Message] = verifySource(classes, file, Source.fromFile(file).mkString)
def newInstance[T](clazz: Class[T]) = clazz.getConstructor().newInstance().asInstanceOf[T]
}
@@ -46,23 +47,27 @@ trait ScalariformChecker extends Checker {
class FileTabChecker extends LinesChecker {
def verify(file: String, lines: Lines): List[Message] = {
- for (
+ val errors = for (
line <- lines.lines.zipWithIndex;
if line._1.contains('\t')
) yield {
StyleError(file, "line.contains.tab", Some(line._2 + 1), Some(line._1.indexOf('\t')))
}
+
+ return errors.toList
}
}
class FileLineLengthChecker extends LinesChecker {
- override def verify(file: String, lines: Lines): List[Message] = {
- for (
+ def verify(file: String, lines: Lines): List[Message] = {
+ val errors = for (
line <- lines.lines.zipWithIndex;
if line._1.length() > 80
) yield {
StyleError(file, "line.size.limit", Some(line._2 + 1))
}
+
+ return errors.toList
}
}
@@ -83,4 +88,4 @@ class SpacesAfterPlusChecker extends ScalariformChecker {
return it.toList
}
-}
+}
View
53 src/test/scala/org/segl/scalastyle/FileTabCheckerTest.scala
@@ -0,0 +1,53 @@
+package org.segl.scalastyle
+
+import org.scalatest.junit.AssertionsForJUnit
+import org.junit.Assert.assertEquals;
+import org.junit.Assert.assertTrue;
+
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+
+trait CheckerTest {
+ def assertErrors[T <: Checker](list: List[Message], source: String)(implicit manifest: Manifest[T]) = {
+ assertEquals(list, Checker.verifySource(List(manifest.erasure.asInstanceOf[Class[T]]), null, source))
+ }
+}
+
+class FileTabCheckerTest extends AssertionsForJUnit with CheckerTest {
+ @Test def testZero() = {
+ val source = """
+package foobar
+import foobar
+ object Foobar {
+}
+""";
+
+ assertErrors[FileTabChecker](List(), source)
+ }
+
+ @Test def testOne() = {
+ val source = """
+package foobar
+import foobar
+ object Foobar {
+}
+""";
+
+ assertErrors[FileTabChecker](List(StyleError(null, "line.contains.tab", Some(4), Some(0), None)), source)
+ }
+
+ @Test def testTwo() = {
+ val source = """
+package foobar
+import foobar
+ object Foobar {
+}
+ object Barbar {
+}
+""";
+
+ assertErrors[FileTabChecker](List(StyleError(null, "line.contains.tab", Some(4), Some(0), None), StyleError(null, "line.contains.tab", Some(6), Some(0), None)), source)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.