From 417718b7f68cb3a5c596a96cd351709a1ca18a7b Mon Sep 17 00:00:00 2001 From: Den Shabalin Date: Thu, 15 Aug 2013 14:06:05 +0200 Subject: addresses feedback regarding new junit tests 1. don't recreate symbol table manually and just let JUnit do it automatically behind the scenes 2. minor changes to assertThrow's description 3. add one more test case to check that exception's subclasses are catched too 4. refine rethrow test to ensure that the exception wasn't swallowed --- .../scala/tools/nsc/symtab/CannotHaveAttrsTest.scala | 17 +++++++---------- .../scala/tools/nsc/symtab/SymbolTableTest.scala | 5 +---- test/junit/scala/tools/testing/AssertThrowsTest.scala | 19 ++++++++++++++----- test/junit/scala/tools/testing/AssertUtil.scala | 4 ++-- 4 files changed, 24 insertions(+), 21 deletions(-) (limited to 'test/junit') diff --git a/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala b/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala index 5867a9030b..355771bf04 100644 --- a/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala +++ b/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala @@ -11,7 +11,7 @@ import scala.reflect.internal.util.OffsetPosition @RunWith(classOf[JUnit4]) class CannotHaveAttrsTest { - class CustomSymbolTable extends SymbolTableForUnitTesting { + object symbolTable extends SymbolTableForUnitTesting { object CHA extends CannotHaveAttrs { def canEqual(that: Any): Boolean = ??? def productArity: Int = ??? @@ -19,17 +19,16 @@ class CannotHaveAttrsTest { } val attrlessTrees = List(CHA, EmptyTree, emptyValDef, pendingSuperCall) } - def withCtx(body: CustomSymbolTable => Unit) = body(new CustomSymbolTable) + import symbolTable._ @Test - def canHaveAttrsIsFalse = withCtx { st => import st._ + def canHaveAttrsIsFalse = attrlessTrees.foreach { t => assertFalse(t.canHaveAttrs) } - } @Test - def defaultPosAssignment = withCtx { st => import st._ + def defaultPosAssignment = attrlessTrees.foreach { t => assertEquals(t.pos, NoPosition) t.pos = NoPosition @@ -37,10 +36,9 @@ class CannotHaveAttrsTest { t.setPos(NoPosition) assertEquals(t.pos, NoPosition) } - } @Test - def defaultTpeAssignment = withCtx { st => import st._ + def defaultTpeAssignment = attrlessTrees.foreach { t => assertEquals(t.tpe, NoType) t.tpe = NoType @@ -48,10 +46,9 @@ class CannotHaveAttrsTest { t.setType(NoType) assertEquals(t.tpe, NoType) } - } @Test - def nonDefaultPosAssignmentFails = withCtx { st => import st._ + def nonDefaultPosAssignmentFails = { val pos = new OffsetPosition(null, 0) attrlessTrees.foreach { t => assertThrows[IllegalArgumentException] { t.pos = pos } @@ -60,7 +57,7 @@ class CannotHaveAttrsTest { } @Test - def nonDefaultTpeAssignmentFails = withCtx { st => import st._ + def nonDefaultTpeAssignmentFails = { val tpe = typeOf[Int] attrlessTrees.foreach { t => assertThrows[IllegalArgumentException] { t.tpe = 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 index ecc46244bd..a70519e63c 100644 --- a/test/junit/scala/tools/testing/AssertThrowsTest.scala +++ b/test/junit/scala/tools/testing/AssertThrowsTest.scala @@ -10,16 +10,25 @@ 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 = - try assertThrows[Foo] { throw new Bar } - catch { - case bar: Bar => - case e: Throwable => fail(s"expected Bar but got $e") - } + 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 index 9efa9327a7..9efac64a97 100644 --- a/test/junit/scala/tools/testing/AssertUtil.scala +++ b/test/junit/scala/tools/testing/AssertUtil.scala @@ -5,8 +5,8 @@ package testing * that are ultimately based on junit.Assert primitives. */ object AssertUtil { - /** Check if exception T was thrawn during evaluation of f. If any - * other exception or throwable is found instead it will be re-thrown. + /** 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 -- cgit v1.2.3