Skip to content

Add type annotations #648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions jvm/src/test/scala-2.x/scala/xml/CompilerErrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import org.junit.Test

class CompilerErrors extends CompilerTesting {
@Test
def t7185() = {
def t7185(): Unit = {
// the error message here differs a bit by Scala version
import util.Properties.versionNumberString
val thing =
val thing: String =
if (versionNumberString.startsWith("2.12")) "method value"
else "method"
expectXmlError(s"""|overloaded $thing apply with alternatives:
Expand All @@ -20,7 +20,7 @@ class CompilerErrors extends CompilerTesting {
}

@Test
def t1878_typer() =
def t1878_typer(): Unit =
expectXmlError("_* may only come last",
"""|object Test extends App {
| // illegal - bug #1764
Expand All @@ -31,14 +31,14 @@ class CompilerErrors extends CompilerTesting {


@Test
def t1017() =
def t1017(): Unit =
expectXmlErrors(1, "not found: value foo",
"""|object Test {
| <x><x><x><x><x><x><x><x><x><x><x><x><x><x><x><x><x><x>{ foo }</x></x></x></x></x></x></x></x></x></x></x></x></x></x></x></x></x></x>
|}""")

@Test
def t1011() =
def t1011(): Unit =
expectXmlErrors(69, "not found: value entity",
"""|import scala.xml._;
|
Expand Down Expand Up @@ -177,9 +177,9 @@ class CompilerTesting {

def errorMessages(errorSnippet: String, compileOptions: String = "")(code: String): List[String] = {
import scala.tools.reflect._
val m = scala.reflect.runtime.currentMirror
val tb = m.mkToolBox(options = compileOptions) //: ToolBox[m.universe.type]
val fe = tb.frontEnd
val m: scala.reflect.runtime.universe.Mirror = scala.reflect.runtime.currentMirror
val tb: ToolBox[scala.reflect.runtime.universe.type] = m.mkToolBox(options = compileOptions) //: ToolBox[m.universe.type]
val fe: FrontEnd = tb.frontEnd

try {
tb.eval(tb.parse(code))
Expand All @@ -192,17 +192,17 @@ class CompilerTesting {

// note: `code` should have a | margin
// the import's needed because toolbox compiler does not accumulate imports like the real one (TODO: verify hypothesis)
def xmlErrorMessages(msg: String, code: String) =
def xmlErrorMessages(msg: String, code: String): List[String] =
errorMessages(msg)("import scala.xml.{TopScope => $scope}\n"+ code.stripMargin)

def expectXmlError(msg: String, code: String) = {
val errors = xmlErrorMessages(msg, code)
def expectXmlError(msg: String, code: String): Unit = {
val errors: List[String] = xmlErrorMessages(msg, code)
assert(errors exists (_ contains msg), errors mkString "\n")
}

def expectXmlErrors(msgCount: Int, msg: String, code: String) = {
val errors = xmlErrorMessages(msg, code)
val errorCount = errors.count(_ contains msg)
def expectXmlErrors(msgCount: Int, msg: String, code: String): Unit = {
val errors: List[String] = xmlErrorMessages(msg, code)
val errorCount: Int = errors.count(_ contains msg)
assert(errorCount == msgCount, s"$errorCount occurrences of \'$msg\' found -- expected $msgCount in:\n${errors mkString "\n"}")
}
}
24 changes: 12 additions & 12 deletions jvm/src/test/scala-2.x/scala/xml/XMLTestJVM2x.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import scala.xml.parsing.ConstructingParser

class XMLTestJVM2x {
@UnitTest
def t2354: Unit = {
val xml_good = "<title><![CDATA[Hello [tag]]]></title>"
val xml_bad = "<title><![CDATA[Hello [tag] ]]></title>"
def t2354(): Unit = {
val xml_good: String = "<title><![CDATA[Hello [tag]]]></title>"
val xml_bad: String = "<title><![CDATA[Hello [tag] ]]></title>"

val parser1 = ConstructingParser.fromSource(io.Source.fromString(xml_good), false)
val parser2 = ConstructingParser.fromSource(io.Source.fromString(xml_bad), false)
val parser1: ConstructingParser = ConstructingParser.fromSource(io.Source.fromString(xml_good), preserveWS = false)
val parser2: ConstructingParser = ConstructingParser.fromSource(io.Source.fromString(xml_bad), preserveWS = false)

parser1.document()
parser2.document()
}

@UnitTest
def t8253: Unit = {
def t8253(): Unit = {
// `identity(foo)` used to match the overly permissive match in SymbolXMLBuilder
// which was intended to more specifically match `_root_.scala.xml.Text(...)`

import reflect.runtime.universe._ // not using the XML library in compiler tests

val ns1 = "ns1"
val ns1: String = "ns1"
assertEquals(reify(ns1).tree.toString, q"ns1".toString)
assertEquals("<sample xmlns='ns1'/>",
"""|{
Expand Down Expand Up @@ -69,21 +69,21 @@ class XMLTestJVM2x {
}

@UnitTest
def t8466lift: Unit = {
def t8466lift(): Unit = {
import scala.reflect.runtime.universe._

implicit val liftXmlComment = Liftable[Comment] { comment =>
implicit val liftXmlComment: Liftable[Comment] = Liftable[Comment] { comment =>
q"new _root_.scala.xml.Comment(${comment.commentText})"
}
liftXmlComment(Comment("foo"))
assertEquals(q"${Comment("foo")}".toString, q"<!--foo-->".toString)
}

@UnitTest
def t8466unlift: Unit = {
def t8466unlift(): Unit = {
import scala.reflect.runtime.universe._

implicit val unliftXmlComment = Unliftable[Comment] {
implicit val unliftXmlComment: Unliftable[Comment] = Unliftable[Comment] {
case q"new _root_.scala.xml.Comment(${value: String})" => Comment(value)
}
unliftXmlComment.unapply(q"<!--foo-->")
Expand All @@ -92,7 +92,7 @@ class XMLTestJVM2x {
}

@UnitTest
def t9027: Unit = {
def t9027(): Unit = {
// used to be parsed as .println

import reflect.runtime._, universe._
Expand Down
24 changes: 12 additions & 12 deletions jvm/src/test/scala/scala/xml/AttributeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ import org.junit.Assert.assertNotEquals
class AttributeTestJVM {

@Test
def attributeOrder: Unit = {
val x = <x y="1" z="2"/>
def attributeOrder(): Unit = {
val x: Elem = <x y="1" z="2"/>
assertEquals("""<x y="1" z="2"/>""", x.toString)
}

@Test
def attributesFromString: Unit = {
val str = """<x y="1" z="2"/>"""
val doc = XML.loadString(str)
def attributesFromString(): Unit = {
val str: String = """<x y="1" z="2"/>"""
val doc: Elem = XML.loadString(str)
assertEquals(str, doc.toString)
}

@Test
def attributesAndNamespaceFromString: Unit = {
val str = """<x xmlns:w="w" y="1" z="2"/>"""
val doc = XML.loadString(str)
def attributesAndNamespaceFromString(): Unit = {
val str: String = """<x xmlns:w="w" y="1" z="2"/>"""
val doc: Elem = XML.loadString(str)
assertNotEquals(str, doc.toString)
val str2 = """<x y="1" z="2" xmlns:w="w"/>"""
val doc2 = XML.loadString(str2)
val str2: String = """<x y="1" z="2" xmlns:w="w"/>"""
val doc2: Elem = XML.loadString(str2)
assertEquals(str2, doc2.toString)
}

@Test(expected=classOf[SAXParseException])
def attributesFromStringWithDuplicate: Unit = {
val str = """<elem one="test" one="test1" two="test2" three="test3"></elem>"""
def attributesFromStringWithDuplicate(): Unit = {
val str: String = """<elem one="test" one="test1" two="test2" three="test3"></elem>"""
XML.loadString(str)
}
}
5 changes: 2 additions & 3 deletions jvm/src/test/scala/scala/xml/BillionLaughsAttackTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class BillionLaughsAttackTest {
* this is the limit imposed by the JDK.
*/
@Test(expected=classOf[org.xml.sax.SAXParseException])
def lolzTest: Unit = {
def lolzTest(): Unit = {
XML.loadString(lolz)
}

// Copied from https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
val lolz =
val lolz: String =
"""<?xml version="1.0"?>
|<!DOCTYPE lolz [
| <!ENTITY lol "lol">
Expand All @@ -32,5 +32,4 @@ class BillionLaughsAttackTest {
|]>
|<lolz>&lol9;</lolz>
|""".stripMargin

}
26 changes: 13 additions & 13 deletions jvm/src/test/scala/scala/xml/ReuseNodesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ object ReuseNodesTest {

class OriginalTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
override def transform(ns: Seq[Node]): Seq[Node] = {
val xs = ns.toStream map transform
val (xs1, xs2) = xs.zip(ns).span { case (x, n) => unchanged(n, x) }
val xs: Seq[Seq[Node]] = ns.toStream map transform
val (xs1: Seq[(Seq[Node], Node)], xs2: Seq[(Seq[Node], Node)]) = xs.zip(ns).span { case (x, n) => unchanged(n, x) }

if (xs2.isEmpty) ns
else xs1.map(_._2) ++ xs2.head._1 ++ transform(ns.drop(xs1.length + 1))
Expand All @@ -30,7 +30,7 @@ object ReuseNodesTest {

class ModifiedTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
override def transform(ns: Seq[Node]): Seq[Node] = {
val changed = ns flatMap transform
val changed: Seq[Node] = ns flatMap transform

if (changed.length != ns.length || changed.zip(ns).exists(p => p._1 != p._2)) changed
else ns
Expand All @@ -40,16 +40,16 @@ object ReuseNodesTest {

class AlternateTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
override def transform(ns: Seq[Node]): Seq[Node] = {
val xs = ns.toStream.map(transform)
val (xs1, xs2) = xs.zip(ns).span { case (x, n) => unchanged(n, x) }
val xs: Seq[Seq[Node]] = ns.toStream.map(transform)
val (xs1: Seq[(Seq[Node], Node)], xs2: Seq[(Seq[Node], Node)]) = xs.zip(ns).span { case (x, n) => unchanged(n, x) }

if (xs2.isEmpty) ns
else xs1.map(_._2) ++ xs2.head._1 ++ transform(ns.drop(xs1.length + 1))
}
override def transform(n: Node): Seq[Node] = super.transform(n)
}

def rewriteRule = new RewriteRule {
def rewriteRule: RewriteRule = new RewriteRule {
override def transform(n: Node): NodeSeq = n match {
case n if n.label == "change" => Elem(
n.prefix, "changed", n.attributes, n.scope, n.child.isEmpty, n.child : _*)
Expand All @@ -58,7 +58,7 @@ object ReuseNodesTest {
}

@DataPoints
def tranformers() = Array(
def tranformers(): Array[RuleTransformer] = Array(
new OriginalTranformr(rewriteRule),
new ModifiedTranformr(rewriteRule),
new AlternateTranformr(rewriteRule))
Expand All @@ -68,16 +68,16 @@ object ReuseNodesTest {
class ReuseNodesTest {

@Theory
def transformReferentialEquality(rt: RuleTransformer) = {
val original = <p><lost/></p>
val tranformed = rt.transform(original)
def transformReferentialEquality(rt: RuleTransformer): Unit = {
val original: Elem = <p><lost/></p>
val tranformed: Seq[Node] = rt.transform(original)
assertSame(original, tranformed)
}

@Theory
def transformReferentialEqualityOnly(rt: RuleTransformer) = {
val original = <changed><change><lost/><a><b><c/></b></a></change><a><b><c/></b></a></changed>
val transformed = rt.transform(original)
def transformReferentialEqualityOnly(rt: RuleTransformer): Unit = {
val original: Elem = <changed><change><lost/><a><b><c/></b></a></change><a><b><c/></b></a></changed>
val transformed: Seq[Node] = rt.transform(original)
recursiveAssert(original,transformed)
}

Expand Down
12 changes: 6 additions & 6 deletions jvm/src/test/scala/scala/xml/SerializationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import org.junit.Test

class SerializationTest {
@Test
def xmlLiteral: Unit = {
val n = <node/>
def xmlLiteral(): Unit = {
val n: Elem = <node/>
assertEquals(n, JavaByteSerialization.roundTrip(n))
}

@Test
def empty: Unit = {
def empty(): Unit = {
assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip(NodeSeq.Empty))
}

@Test
def unmatched: Unit = {
def unmatched(): Unit = {
assertEquals(NodeSeq.Empty, JavaByteSerialization.roundTrip(<xml/> \ "HTML"))
}

@Test
def implicitConversion: Unit = {
val parent = <parent><child></child><child/></parent>
def implicitConversion(): Unit = {
val parent: Elem = <parent><child></child><child/></parent>
val children: Seq[Node] = parent.child
val asNodeSeq: NodeSeq = children
assertEquals(asNodeSeq, JavaByteSerialization.roundTrip(asNodeSeq))
Expand Down
7 changes: 3 additions & 4 deletions jvm/src/test/scala/scala/xml/XMLSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class XMLSyntaxTestJVM {
@Test
def test3(): Unit = {
// this demonstrates how to handle entities
val s = io.Source.fromString("<a>&nbsp;</a>")
object parser extends xml.parsing.ConstructingParser(s, false /*ignore ws*/) {
val s: io.Source = io.Source.fromString("<a>&nbsp;</a>")
object parser extends xml.parsing.ConstructingParser(s, preserveWS = false /*ignore ws*/) {
override def replacementText(entityName: String): io.Source = {
entityName match {
case "nbsp" => io.Source.fromString("\u0160")
Expand All @@ -18,9 +18,8 @@ class XMLSyntaxTestJVM {
}
nextch() // !!important, to initialize the parser
}
val parsed = parser.element(TopScope) // parse the source as element
val parsed: NodeSeq = parser.element(TopScope) // parse the source as element
// alternatively, we could call document()
assertEquals("<a>Š</a>", parsed.toString)
}

}
Loading