diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-08-16 11:41:53 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-08-16 11:41:53 -0700 |
commit | 63bce12e94eb4480823627d6328f74befa421bbf (patch) | |
tree | 9d4f6b762467e9af15c1d886e7f37caad9064911 /test/junit | |
parent | 01f2c2ac45a15312bd45193fd6302b7b01de9db7 (diff) | |
parent | 417718b7f68cb3a5c596a96cd351709a1ca18a7b (diff) | |
download | scala-63bce12e94eb4480823627d6328f74befa421bbf.tar.gz scala-63bce12e94eb4480823627d6328f74befa421bbf.tar.bz2 scala-63bce12e94eb4480823627d6328f74befa421bbf.zip |
Merge pull request #2830 from densh/topic/stats-parsing-2
updated SI-7331, SI-6843, SI-7731, parser entry point refactoring, assertThrows utility function
Diffstat (limited to 'test/junit')
4 files changed, 121 insertions, 4 deletions
diff --git a/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala b/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala new file mode 100644 index 0000000000..355771bf04 --- /dev/null +++ b/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala @@ -0,0 +1,67 @@ +package scala.tools.nsc +package symtab + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.AssertUtil.assertThrows +import scala.reflect.internal.util.OffsetPosition + +@RunWith(classOf[JUnit4]) +class CannotHaveAttrsTest { + object symbolTable extends SymbolTableForUnitTesting { + object CHA extends CannotHaveAttrs { + def canEqual(that: Any): Boolean = ??? + def productArity: Int = ??? + def productElement(n: Int): Any = ??? + } + val attrlessTrees = List(CHA, EmptyTree, emptyValDef, pendingSuperCall) + } + import symbolTable._ + + @Test + def canHaveAttrsIsFalse = + attrlessTrees.foreach { t => + assertFalse(t.canHaveAttrs) + } + + @Test + def defaultPosAssignment = + attrlessTrees.foreach { t => + assertEquals(t.pos, NoPosition) + t.pos = NoPosition + assertEquals(t.pos, NoPosition) + t.setPos(NoPosition) + assertEquals(t.pos, NoPosition) + } + + @Test + def defaultTpeAssignment = + attrlessTrees.foreach { t => + assertEquals(t.tpe, NoType) + t.tpe = NoType + assertEquals(t.tpe, NoType) + t.setType(NoType) + assertEquals(t.tpe, NoType) + } + + @Test + def nonDefaultPosAssignmentFails = { + val pos = new OffsetPosition(null, 0) + attrlessTrees.foreach { t => + assertThrows[IllegalArgumentException] { t.pos = pos } + assertThrows[IllegalArgumentException] { t.setPos(pos) } + } + } + + @Test + def nonDefaultTpeAssignmentFails = { + val tpe = typeOf[Int] + attrlessTrees.foreach { t => + assertThrows[IllegalArgumentException] { t.tpe = tpe } + assertThrows[IllegalArgumentException] { t.setType(tpe) } + } + } +} diff --git a/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala b/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala index 537cb93ef3..11e955a4bb 100644 --- a/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala +++ b/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala @@ -9,17 +9,15 @@ import org.junit.runners.JUnit4 @RunWith(classOf[JUnit4]) class SymbolTableTest { - private def createSymbolTable: SymbolTable = new SymbolTableForUnitTesting + object symbolTable extends SymbolTableForUnitTesting @Test def initDefinitions = { - val symbolTable = createSymbolTable symbolTable.definitions.init() } @Test def basicSubTypeCheck = { - val symbolTable = createSymbolTable symbolTable.definitions.init() val listClassTpe = symbolTable.definitions.ListClass.tpe val seqClassTpe = symbolTable.definitions.SeqClass.tpe @@ -32,7 +30,6 @@ class SymbolTableTest { */ @Test def customClassesSubTypeCheck: Unit = { - val symbolTable = createSymbolTable import symbolTable._ symbolTable.definitions.init() val rootClass = symbolTable.rootMirror.RootClass diff --git a/test/junit/scala/tools/testing/AssertThrowsTest.scala b/test/junit/scala/tools/testing/AssertThrowsTest.scala new file mode 100644 index 0000000000..a70519e63c --- /dev/null +++ b/test/junit/scala/tools/testing/AssertThrowsTest.scala @@ -0,0 +1,34 @@ +package scala.tools +package testing + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import AssertUtil.assertThrows + +@RunWith(classOf[JUnit4]) +class AssertThrowsTest { + class Foo extends Exception + class SubFoo extends Foo + class Bar extends Exception + + @Test + def catchFoo = assertThrows[Foo] { throw new Foo } + + @Test + def catchSubclass = assertThrows[Foo] { throw new SubFoo } + + @Test + def rethrowBar = + assertTrue("exception wasn't rethrown", { + try { + assertThrows[Foo] { throw new Bar } + false + } catch { + case bar: Bar => true + case e: Throwable => fail(s"expected Bar but got $e"); false + } + }) + +}
\ No newline at end of file diff --git a/test/junit/scala/tools/testing/AssertUtil.scala b/test/junit/scala/tools/testing/AssertUtil.scala new file mode 100644 index 0000000000..9efac64a97 --- /dev/null +++ b/test/junit/scala/tools/testing/AssertUtil.scala @@ -0,0 +1,19 @@ +package scala.tools +package testing + +/** This module contains additional higher-level assert statements + * that are ultimately based on junit.Assert primitives. + */ +object AssertUtil { + /** Check if exception T (or a subclass) was thrown during evaluation of f. + * If any other exception or throwable is found instead it will be re-thrown. + */ + def assertThrows[T <: Exception](f: => Any)(implicit manifest: Manifest[T]): Unit = + try f + catch { + case e: Exception => + val clazz = manifest.erasure.asInstanceOf[Class[T]] + if (!clazz.isAssignableFrom(e.getClass)) + throw e + } +}
\ No newline at end of file |