From 9a0db27487d3d28ca3286975c0c8cf13b5f27fc6 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Tue, 28 Aug 2018 11:54:28 -0400 Subject: [PATCH] Fix failure for empty attribute match Running the following invalid attribute search, properly throws an IllegalArgumentException. scala> \ "@" java.lang.IllegalArgumentException: @ scala> \@ "" java.lang.IllegalArgumentException: @ There's no such thing as an empty attribute. However, when the improper matching value is used against more than just one element, no error is thrown, just an empty NodeSeq is returned: scala> .child \ "@" res1: scala.xml.NodeSeq = NodeSeq() It should be a failure. Similarly, the attribute search method, is similarly affected. scala> .child \@ "" res1: scala.xml.NodeSeq = NodeSeq() This was identified while writing ScalaCheck property tests. --- shared/src/main/scala/scala/xml/NodeSeq.scala | 1 + .../test/scala/scala/xml/AttributeTest.scala | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/shared/src/main/scala/scala/xml/NodeSeq.scala b/shared/src/main/scala/scala/xml/NodeSeq.scala index 07e6fe87d..18c8295b2 100644 --- a/shared/src/main/scala/scala/xml/NodeSeq.scala +++ b/shared/src/main/scala/scala/xml/NodeSeq.scala @@ -116,6 +116,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S that match { case "" => fail case "_" => makeSeq(!_.isAtom) + case "@" => fail case _ if (that(0) == '@' && this.length == 1) => atResult case _ => makeSeq(_.label == that) } diff --git a/shared/src/test/scala/scala/xml/AttributeTest.scala b/shared/src/test/scala/scala/xml/AttributeTest.scala index 0b3891496..285d24795 100644 --- a/shared/src/test/scala/scala/xml/AttributeTest.scala +++ b/shared/src/test/scala/scala/xml/AttributeTest.scala @@ -154,4 +154,23 @@ class AttributeTest { assertEquals(List(Group(Seq(Text("1"))), Group(Seq(Text("2")))), barList) } + @Test(expected=classOf[IllegalArgumentException]) + def invalidAttributeFailForOne: Unit = { + \ "@" + } + + @Test(expected=classOf[IllegalArgumentException]) + def invalidAttributeFailForMany: Unit = { + .child \ "@" + } + + @Test(expected=classOf[IllegalArgumentException]) + def invalidEmptyAttributeFailForOne: Unit = { + \@ "" + } + + @Test(expected=classOf[IllegalArgumentException]) + def invalidEmptyAttributeFailForMany: Unit = { + .child \@ "" + } }