diff options
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/StringContextTest.scala | 51 | ||||
-rw-r--r-- | test/junit/scala/collection/IndexedSeqOptimizedTest.scala | 16 | ||||
-rw-r--r-- | test/junit/scala/collection/IteratorTest.scala | 28 | ||||
-rw-r--r-- | test/junit/scala/reflect/internal/NamesTest.scala | 95 | ||||
-rw-r--r-- | test/junit/scala/reflect/internal/ScopeTest.scala | 54 | ||||
-rw-r--r-- | test/junit/scala/reflect/internal/TypesTest.scala | 35 | ||||
-rw-r--r-- | test/junit/scala/reflect/internal/util/SourceFileTest.scala | 5 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/backend/jvm/BTypesTest.scala | 104 | ||||
-rw-r--r-- | test/junit/scala/tools/testing/AssertUtil.scala | 17 |
9 files changed, 399 insertions, 6 deletions
diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala new file mode 100644 index 0000000000..5abfe90cd1 --- /dev/null +++ b/test/junit/scala/StringContextTest.scala @@ -0,0 +1,51 @@ + +package scala + +import org.junit.Test +import org.junit.Assert._ +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.AssertUtil._ + +@RunWith(classOf[JUnit4]) +class StringContextTest { + + import StringContext._ + + @Test def noEscape() = { + val s = "string" + val res = processEscapes(s) + assertEquals(s, res) + } + @Test def tabbed() = { + val s = """a\tb""" + val res = processEscapes(s) + assertEquals("a\tb", res) + } + @Test def quoted() = { + val s = """hello, \"world\"""" + val res = processEscapes(s) + assertEquals("""hello, "world"""", res) + } + @Test def octal() = { + val s = """\123cala""" + val res = treatEscapes(s) + assertEquals("Scala", res) + } + @Test def doubled() = { + val s = """\123cala\123yntax""" + val res = treatEscapes(s) + assertEquals("ScalaSyntax", res) + } + @Test def badly() = assertThrows[InvalidEscapeException] { + val s = """Scala\""" + val res = treatEscapes(s) + assertEquals("Scala", res) + } + @Test def noOctal() = assertThrows[InvalidEscapeException] { + val s = """\123cala""" + val res = processEscapes(s) + assertEquals("Scala", res) + } +} diff --git a/test/junit/scala/collection/IndexedSeqOptimizedTest.scala b/test/junit/scala/collection/IndexedSeqOptimizedTest.scala new file mode 100644 index 0000000000..e5382907af --- /dev/null +++ b/test/junit/scala/collection/IndexedSeqOptimizedTest.scala @@ -0,0 +1,16 @@ +package scala.collection + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Assert._ +import org.junit.Test + +@RunWith(classOf[JUnit4]) +class IndexedSeqOptimizedTest { + + @Test + def notThrowsAnExceptionInLastIndexOf() { + assertEquals(0, (Array(2): collection.mutable.WrappedArray[Int]).lastIndexWhere(_ => true, 1)) + assertEquals(2, "abc123".lastIndexWhere(_.isLetter, 6)) + } +} diff --git a/test/junit/scala/collection/IteratorTest.scala b/test/junit/scala/collection/IteratorTest.scala new file mode 100644 index 0000000000..b7a9805c9f --- /dev/null +++ b/test/junit/scala/collection/IteratorTest.scala @@ -0,0 +1,28 @@ + +package scala.collection + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class IteratorTest { + + @Test + def groupedIteratorShouldNotAskForUnneededElement(): Unit = { + var counter = 0 + val it = new Iterator[Int] { var i = 0 ; def hasNext = { counter = i; true } ; def next = { i += 1; i } } + val slidingIt = it sliding 2 + slidingIt.next + assertEquals("Counter should be one, that means we didn't look further than needed", 1, counter) + } + + @Test def groupedIteratorIsLazyWhenPadded(): Unit = { + var counter = 0 + def it = new Iterator[Int] { var i = 0 ; def hasNext = { counter = i; true } ; def next = { i += 1; i } } + val slidingIt = it sliding 2 withPadding -1 + slidingIt.next + assertEquals("Counter should be one, that means we didn't look further than needed", 1, counter) + } +} diff --git a/test/junit/scala/reflect/internal/NamesTest.scala b/test/junit/scala/reflect/internal/NamesTest.scala new file mode 100644 index 0000000000..549c10abed --- /dev/null +++ b/test/junit/scala/reflect/internal/NamesTest.scala @@ -0,0 +1,95 @@ +package scala.reflect.internal + +import scala.tools.testing.AssertUtil._ +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test +import org.junit.Assert._ +import scala.tools.nsc.symtab.SymbolTableForUnitTesting + +@RunWith(classOf[JUnit4]) +class NamesTest { + object symbolTable extends SymbolTableForUnitTesting + import symbolTable._ + + val h1 = newTermName("hai") + val h2 = newTermName("hai") + val f = newTermName("fisch") + + val h1y = h1.toTypeName + val h2y = newTypeName("hai") + val fy = newTypeName("fisch") + + val uy = newTypeName("uhu") + val u = uy.toTermName // calling toTermName after constructing a typeName. This tests the fact + // that creating a typeName always also first creates a termName. There is + // an assertion for that in toTermName. + + @Test + def termNamesAreHashConsed() { + assertTrue(h1 eq h2) + assertEquals(h1, h2) + assertTrue(h1 ne f) + assertTrue(h1 != f) + } + + @Test + def termNamesNotEqualsTypeNames() { + assert(h1 ne h1y) + assert(h1 != h1y) + assert(h2 ne h2y) + assert(h2 != h2y) + } + + @Test + def termNamesTypeNamesSameRange() { + assert(h1.start == h1y.start && h1.length == h1y.length) + assert(h2.start == h2y.start && h2.length == h2y.length) + assert(u.start == uy.start && u.length == uy.length) + } + + @Test + def testLookupTypeName() { + assert(lookupTypeName("hai".toCharArray) eq h1y) + assert(lookupTypeName("fisch".toCharArray) eq fy) + assert(lookupTypeName("uhu".toCharArray) eq uy) + + assertThrows[AssertionError](lookupTypeName("dog".toCharArray), _ contains "not yet created") + val d = newTermName("dog") + assertThrows[AssertionError](lookupTypeName("dog".toCharArray), _ contains "not yet created") + val dy = d.toTypeName + assert(lookupTypeName("dog".toCharArray) eq dy) + } + + @Test + def emptyName() { + val z = newTermName("") + val zy = z.toTypeName + assertEquals(z.toString, "") + assertEquals(zy.toString, "") + assert(z eq newTermName("")) + assert(zy eq newTypeName("")) + } + + @Test + def subNameTest() { + val i = f.subName(1, f.length) + assert(i.start == (f.start + 1) && i.length == (f.length - 1)) + assert(f.subName(0, f.length) eq f) + + val iy = fy.subName(1, fy.length) + assert(iy.start == (fy.start + 1) && iy.length == (fy.length - 1)) + assert(fy.subName(0, fy.length) eq fy) + + assert(f.subName(1,1) eq newTermName("")) + assert(f.subName(1, 0) eq newTermName("")) + + assertThrows[IllegalArgumentException](f.subName(0 - f.start - 1, 1)) + } + + @Test + def stringEqualsTest() { + assert(h1 string_== h2) + assert(h1 string_== h1y) + } +} diff --git a/test/junit/scala/reflect/internal/ScopeTest.scala b/test/junit/scala/reflect/internal/ScopeTest.scala new file mode 100644 index 0000000000..5166620189 --- /dev/null +++ b/test/junit/scala/reflect/internal/ScopeTest.scala @@ -0,0 +1,54 @@ +package symtab + +import scala.tools.nsc.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.tools.nsc.symtab.SymbolTableForUnitTesting + +@RunWith(classOf[JUnit4]) +class ScopeTest { + object symbolTable extends SymbolTableForUnitTesting + + import symbolTable._ + + @Test + def testNestedScopeSmall(): Unit = testNestedScope(0) + @Test + def testNestedScopeLarge(): Unit = testNestedScope(64) // exceeding MIN_HASH + + private def testNestedScope(initSize: Int) { + def sym(termName: String): Symbol = NoSymbol.newValue(TermName(termName)) + val foo = sym("foo") + val bar = sym("bar") + + val outerElems = List.tabulate(initSize)(i => sym(i.toString)) + val outer = newScopeWith(outerElems ++ List(foo, bar) : _*) + assertTrue(outer.containsName(foo.name)) + assertTrue(outer.containsName(bar.name)) + + val baz = sym("baz") + val nested = newNestedScope(outer) + + // Entries from the outer scope are entered in the nested. + assertTrue(outer.containsName(foo.name)) + assertTrue(outer.containsName(bar.name)) + + // Nested scopes structurally share ScopeEntry-s with the outer. + assertSame(outer.lookupEntry(foo.name), nested.lookupEntry(foo.name)) + nested.enter(baz) + + // Symbols entered in the nested scope aren't visible in the outer. + assertTrue(nested.containsName(baz.name)) + assertTrue(!outer.containsName(baz.name)) + + // Unlinking a symbol in the inner scope doesn't modify the outer + nested.unlink(bar) + assert(!nested.containsName(bar.name)) + assert(outer.containsName(bar.name)) + } +} diff --git a/test/junit/scala/reflect/internal/TypesTest.scala b/test/junit/scala/reflect/internal/TypesTest.scala new file mode 100644 index 0000000000..95194ef0a4 --- /dev/null +++ b/test/junit/scala/reflect/internal/TypesTest.scala @@ -0,0 +1,35 @@ +package scala.reflect.internal + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import scala.tools.nsc.symtab.SymbolTableForUnitTesting + +@RunWith(classOf[JUnit4]) +class TypesTest { + + object symbolTable extends SymbolTableForUnitTesting + import symbolTable._, definitions._ + + @Test + def testRefinedTypeSI8611(): Unit = { + def stringNarrowed = StringTpe.narrow + assert(stringNarrowed != stringNarrowed) + assert(!(stringNarrowed =:= stringNarrowed)) + + def boolWithString = refinedType(BooleanTpe :: StringTpe :: Nil, NoSymbol) + assert(boolWithString != boolWithString) + assert(boolWithString =:= boolWithString) + + val boolWithString1 = boolWithString + val boolWithString1narrow1 = boolWithString1.narrow + val boolWithString1narrow2 = boolWithString1.narrow + // Two narrowings of the same refinement end up =:=. This was the root + // cause of SI-8611. See `narrowUniquely` in `Logic` for the workaround. + assert(boolWithString1narrow1 =:= boolWithString1narrow2) + val uniquelyNarrowed1 = refinedType(boolWithString1narrow1 :: Nil, NoSymbol) + val uniquelyNarrowed2 = refinedType(boolWithString1narrow2 :: Nil, NoSymbol) + assert(uniquelyNarrowed1 =:= uniquelyNarrowed2) + } +} diff --git a/test/junit/scala/reflect/internal/util/SourceFileTest.scala b/test/junit/scala/reflect/internal/util/SourceFileTest.scala index 903e705ba2..cad23eba14 100644 --- a/test/junit/scala/reflect/internal/util/SourceFileTest.scala +++ b/test/junit/scala/reflect/internal/util/SourceFileTest.scala @@ -17,6 +17,11 @@ class SourceFileTest { assertFalse(file.isEndOfLine(Int.MaxValue)) } + @Test def si8630_lineToString(): Unit = { + val code = "abc " + assertEquals(code, new BatchSourceFile("", code).lineToString(0)) + } + @Test def si8205_lineToString(): Unit = { assertEquals("", lineContentOf("", 0)) diff --git a/test/junit/scala/tools/nsc/backend/jvm/BTypesTest.scala b/test/junit/scala/tools/nsc/backend/jvm/BTypesTest.scala new file mode 100644 index 0000000000..b592d06501 --- /dev/null +++ b/test/junit/scala/tools/nsc/backend/jvm/BTypesTest.scala @@ -0,0 +1,104 @@ +package scala.tools.nsc +package backend.jvm + +import scala.tools.testing.AssertUtil._ +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test +import scala.tools.asm.Opcodes +import org.junit.Assert._ + +@RunWith(classOf[JUnit4]) +class BTypesTest { + val g: Global = new Global(new Settings()) + + val btypes = new BTypes[g.type](g) { + def chrs = g.chrs + override type BTypeName = g.TypeName + override def createNewName(s: String) = g.newTypeName(s) + } + + import btypes._ + + val jls = "java/lang/String" + val jlo = "java/lang/Object" + + val o = ClassBType(jlo) + val s = ClassBType(jls) + val oArr = ArrayBType(o) + val method = MethodBType(List(oArr, INT, DOUBLE, s), UNIT) + + @Test + def classBTypesEquality() { + val s1 = ClassBType(jls) + val s2 = ClassBType(jls) + val o = ClassBType(jlo) + assertEquals(s1, s2) + assertEquals(s1.hashCode, s2.hashCode) + assert(s1 != o) + assert(s2 != o) + } + + @Test + def classBTypeRequiresInternalName() { + assertThrows[AssertionError](ClassBType(s"L$jls;"), _ contains "Descriptor instead of internal name") + } + + @Test + def typedOpcodes() { + assert(UNIT.typedOpcode(Opcodes.IALOAD) == Opcodes.IALOAD) + assert(INT.typedOpcode(Opcodes.IALOAD) == Opcodes.IALOAD) + assert(BOOL.typedOpcode(Opcodes.IALOAD) == Opcodes.BALOAD) + assert(BYTE.typedOpcode(Opcodes.IALOAD) == Opcodes.BALOAD) + assert(CHAR.typedOpcode(Opcodes.IALOAD) == Opcodes.CALOAD) + assert(SHORT.typedOpcode(Opcodes.IALOAD) == Opcodes.SALOAD) + assert(FLOAT.typedOpcode(Opcodes.IALOAD) == Opcodes.FALOAD) + assert(LONG.typedOpcode(Opcodes.IALOAD) == Opcodes.LALOAD) + assert(DOUBLE.typedOpcode(Opcodes.IALOAD) == Opcodes.DALOAD) + assert(ClassBType(jls).typedOpcode(Opcodes.IALOAD) == Opcodes.AALOAD) + + assert(UNIT.typedOpcode(Opcodes.IRETURN) == Opcodes.RETURN) + assert(BOOL.typedOpcode(Opcodes.IRETURN) == Opcodes.IRETURN) + assert(CHAR.typedOpcode(Opcodes.IRETURN) == Opcodes.IRETURN) + assert(BYTE.typedOpcode(Opcodes.IRETURN) == Opcodes.IRETURN) + assert(SHORT.typedOpcode(Opcodes.IRETURN) == Opcodes.IRETURN) + assert(INT.typedOpcode(Opcodes.IRETURN) == Opcodes.IRETURN) + assert(FLOAT.typedOpcode(Opcodes.IRETURN) == Opcodes.FRETURN) + assert(LONG.typedOpcode(Opcodes.IRETURN) == Opcodes.LRETURN) + assert(DOUBLE.typedOpcode(Opcodes.IRETURN) == Opcodes.DRETURN) + assert(ClassBType(jls).typedOpcode(Opcodes.IRETURN) == Opcodes.ARETURN) + } + + @Test + def descriptors() { + assert(o.descriptor == "Ljava/lang/Object;") + assert(s.descriptor == "Ljava/lang/String;") + assert(oArr.descriptor == "[Ljava/lang/Object;") + assert(method.descriptor == "([Ljava/lang/Object;IDLjava/lang/String;)V") + } + + @Test + def toAsmTypeTest() { + for (t <- List(o, s, oArr, method, INT, UNIT, DOUBLE)) { + assertEquals(o.descriptor, o.toASMType.getDescriptor) + } + } + + @Test + def parseMethodDescriptorTest() { + val descriptors = List( + "()V", + "(ID)I", + "([[I[D)[D", + s"(L$jls;[L$jlo;)[[L$jls;", + s"(IL$jlo;)L$jls;" + ) + for (d <- descriptors) { + assertEquals(d, MethodBType(d).descriptor) + } + + // class types in method descriptor need surrounding 'L' and ';' + assertThrows[MatchError](MethodBType("(java/lang/String)V"), _ == "j (of class java.lang.Character)") + assertThrows[AssertionError](MethodBType("I"), _ contains "Not a valid method descriptor") + } +} diff --git a/test/junit/scala/tools/testing/AssertUtil.scala b/test/junit/scala/tools/testing/AssertUtil.scala index 9efac64a97..9a97c5114f 100644 --- a/test/junit/scala/tools/testing/AssertUtil.scala +++ b/test/junit/scala/tools/testing/AssertUtil.scala @@ -5,15 +5,20 @@ package testing * 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. + /** + * Check if throwable T (or a subclass) was thrown during evaluation of f, and that its message + * satisfies the `checkMessage` predicate. + * If any other exception will be re-thrown. */ - def assertThrows[T <: Exception](f: => Any)(implicit manifest: Manifest[T]): Unit = + def assertThrows[T <: Throwable](f: => Any, + checkMessage: String => Boolean = s => true) + (implicit manifest: Manifest[T]): Unit = { try f catch { - case e: Exception => - val clazz = manifest.erasure.asInstanceOf[Class[T]] + case e: Throwable if checkMessage(e.getMessage) => + val clazz = manifest.runtimeClass if (!clazz.isAssignableFrom(e.getClass)) throw e } -}
\ No newline at end of file + } +} |