|  | 
|  | 1 | +package scala.xml | 
|  | 2 | + | 
|  | 3 | +import org.junit.Test | 
|  | 4 | +import org.junit.Ignore | 
|  | 5 | +import org.junit.runner.RunWith | 
|  | 6 | +import org.junit.runners.JUnit4 | 
|  | 7 | +import org.junit.Assert.assertTrue | 
|  | 8 | +import org.junit.Assert.assertFalse | 
|  | 9 | +import org.junit.Assert.assertEquals | 
|  | 10 | + | 
|  | 11 | +class XMLSyntaxTest { | 
|  | 12 | + | 
|  | 13 | +  private def handle[A](x: Node): A = { | 
|  | 14 | +    x.child(0).asInstanceOf[Atom[A]].data | 
|  | 15 | +  } | 
|  | 16 | + | 
|  | 17 | +  @Test | 
|  | 18 | +  def test1(): Unit = { | 
|  | 19 | +    val xNull = <hello>{null}</hello> // these used to be Atom(unit), changed to empty children | 
|  | 20 | +    assertTrue(xNull.child sameElements Nil) | 
|  | 21 | + | 
|  | 22 | +    val x0 = <hello>{}</hello> // these used to be Atom(unit), changed to empty children | 
|  | 23 | +    val x00 = <hello>{ }</hello> //  dto. | 
|  | 24 | +    val xa = <hello>{ "world" }</hello> | 
|  | 25 | + | 
|  | 26 | +    assertTrue(x0.child sameElements Nil) | 
|  | 27 | +    assertTrue(x00.child sameElements Nil) | 
|  | 28 | +    assertEquals("world", handle[String](xa)) | 
|  | 29 | + | 
|  | 30 | +    val xb = <hello>{ 1.5 }</hello> | 
|  | 31 | +    assertEquals(1.5, handle[Double](xb), 0.0) | 
|  | 32 | + | 
|  | 33 | +    val xc = <hello>{ 5 }</hello> | 
|  | 34 | +    assertEquals(5, handle[Int](xc)) | 
|  | 35 | + | 
|  | 36 | +    val xd = <hello>{ true }</hello> | 
|  | 37 | +    assertEquals(true, handle[Boolean](xd)) | 
|  | 38 | + | 
|  | 39 | +    val xe = <hello>{ 5:Short }</hello> | 
|  | 40 | +    assertEquals((5:Short), handle[Short](xe)) | 
|  | 41 | + | 
|  | 42 | +    val xf = <hello>{ val x = 27; x }</hello> | 
|  | 43 | +    assertEquals(27, handle[Int](xf)) | 
|  | 44 | + | 
|  | 45 | +    val xg = <hello>{ List(1,2,3,4) }</hello> | 
|  | 46 | +    assertEquals("<hello>1 2 3 4</hello>", xg.toString) | 
|  | 47 | +    assertFalse(xg.child.map(_.isInstanceOf[Text]).exists(identity)) | 
|  | 48 | + | 
|  | 49 | +    val xh = <hello>{ for(x <- List(1,2,3,4) if x % 2 == 0) yield x }</hello> | 
|  | 50 | +    assertEquals("<hello>2 4</hello>", xh.toString) | 
|  | 51 | +    assertFalse(xh.child.map(_.isInstanceOf[Text]).exists(identity)) | 
|  | 52 | +  } | 
|  | 53 | + | 
|  | 54 | +  /** see SVN r13821 (emir): support for <elem key={x:Option[Seq[Node]]} />, | 
|  | 55 | +   *  so that Options can be used for optional attributes. | 
|  | 56 | +   */ | 
|  | 57 | +  @Test | 
|  | 58 | +  def test2(): Unit = { | 
|  | 59 | +    val x1: Option[Seq[Node]] = Some(<b>hello</b>) | 
|  | 60 | +    val n1 = <elem key={x1} />; | 
|  | 61 | +    assertEquals(x1, n1.attribute("key")) | 
|  | 62 | + | 
|  | 63 | +    val x2: Option[Seq[Node]] = None | 
|  | 64 | +    val n2 = <elem key={x2} />; | 
|  | 65 | +    assertEquals(x2, n2.attribute("key")) | 
|  | 66 | +  } | 
|  | 67 | + | 
|  | 68 | +} | 
0 commit comments