diff --git a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/xpath/Xpath.scala b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/xpath/Xpath.scala index e56e9430..59be21b4 100644 --- a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/xpath/Xpath.scala +++ b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/xpath/Xpath.scala @@ -15,13 +15,18 @@ import ru.tinkoff.tcb.bson.BsonEncoder import ru.tinkoff.tcb.bson.BsonKeyDecoder import ru.tinkoff.tcb.bson.BsonKeyEncoder -final case class Xpath(raw: String, toXPathExpr: XPathExpression) { +/* + * toXPathExpr inside extra braces is important. It's exclude this value + * from hash and equals method. XPathExpression doesn't have correct equals + * method, it is comparable only by reference. + */ +final case class Xpath(raw: String)(val toXPathExpr: XPathExpression) { override def toString: String = raw } object Xpath { def fromString(pathStr: String): Try[Xpath] = - Try(xPathFactory.newXPath().compile(pathStr)).map(Xpath(pathStr, _)) + Try(xPathFactory.newXPath().compile(pathStr)).map(Xpath(pathStr)) def unapply(str: String): Option[Xpath] = fromString(str).toOption diff --git a/backend/mockingbird/src/test/scala/ru/tinkoff/tcb/xpath/XpathSpec.scala b/backend/mockingbird/src/test/scala/ru/tinkoff/tcb/xpath/XpathSpec.scala new file mode 100644 index 00000000..8932aadb --- /dev/null +++ b/backend/mockingbird/src/test/scala/ru/tinkoff/tcb/xpath/XpathSpec.scala @@ -0,0 +1,15 @@ +package ru.tinkoff.tcb.xpath + +import org.scalatest.funsuite.AnyFunSuite +import org.scalatest.matchers.should.Matchers + +class XpathSpec extends AnyFunSuite with Matchers { + + test("Xpath equals") { + val xp1 = Xpath.fromString("//user/@id") + val xp2 = Xpath.fromString("//user/@id") + + xp1 shouldBe xp2 + } + +}