Skip to content

Commit

Permalink
Add further tests of NodeSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
ashawley committed Jan 31, 2020
1 parent e723935 commit df7d18e
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions shared/src/test/scala/scala/xml/NodeSeqTest.scala
Expand Up @@ -20,4 +20,84 @@ class NodeSeqTest {
val exp = NodeSeq.fromSeq(Seq(<a>Hello</a>, <b>Hi</b>))
assertEquals(exp, res)
}

@Test
def testAppendedAll: Unit = { // Bug #392.
val a: NodeSeq = <a>Hello</a>
val b = <b>Hi</b>
a :+ <b>Hi</b> match {
case res: Seq[Node] => assertEquals(2, res.size)
case res: NodeSeq => fail("Should be Seq[Node]") // Unreachable code?
}
val res: NodeSeq = a :+ b
val exp = NodeSeq.fromSeq(Seq(<a>Hello</a>, <b>Hi</b>))
assertEquals(exp, res)
}

@Test
def testPrepended: Unit = {
val a: NodeSeq = <a>Hello</a>
val b = <b>Hi</b>
a +: <b>Hi</b> match {
case res: Seq[NodeSeq] => assertEquals(2, res.size)
case res: NodeSeq => fail("Should be NodeSeq was Seq[Node]") // Unreachable code?
}
val res: Seq[NodeSeq] = a +: b
val exp = <a>Hello</a><b>Hi</b>
assertEquals(exp, res)
}

@Test
def testPrependedAll: Unit = {
val a: NodeSeq = <a>Hello</a>
val b = <b>Hi</b>
val c = <c>Hey</c>
a ++: <b>Hi</b> ++: <c>Hey</c> match {
case res: Seq[Node] => assertEquals(3, res.size)
case res: NodeSeq => fail("Should be Seq[Node]") // Unreachable code?
}
val res: NodeSeq = a ++: b ++: c
val exp = NodeSeq.fromSeq(Seq(<a>Hello</a>, <b>Hi</b>, <c>Hey</c>))
assertEquals(exp, res)
}

@Test
def testMap: Unit = {
val a: NodeSeq = <a>Hello</a>
val exp: NodeSeq = Seq(<b>Hi</b>)
assertEquals(exp, a.map(_ => <b>Hi</b>))
assertEquals(exp, for { _ <- a } yield { <b>Hi</b> })
}

@Test
def testFlatMap: Unit = {
val a: NodeSeq = <a>Hello</a>
val exp: NodeSeq = Seq(<b>Hi</b>)
assertEquals(exp, a.flatMap(_ => Seq(<b>Hi</b>)))
assertEquals(exp, for { b <- a; _ <- b } yield { <b>Hi</b> })
assertEquals(exp, for { b <- a; c <- b; _ <- c } yield { <b>Hi</b> })
}

@Test
def testStringProjection: Unit = {
val a =
<a>
<b>b</b>
<b>
<c d="d">
<e>e</e>
<e>e</e>
</c>
<c>c</c>
</b>
</a>
val res = for {
b <- a \ "b"
c <- b.child
e <- (c \ "e").headOption
} yield {
e.text.trim
}
assertEquals(Seq("e"), res)
}
}

0 comments on commit df7d18e

Please sign in to comment.