正则表达式模式平等

flu*_*y03 5 scala equality scalatest

在ScalaTest中,我有以下检查:

"abc".r shouldBe "abc".r
Run Code Online (Sandbox Code Playgroud)

但它并不平等.我不明白.

abc was not equal to abc
ScalaTestFailureLocation: com.ing.cybrct.flink.clickstream.ConfigsTest$$anonfun$6 at (ConfigsTest.scala:97)
Expected :abc
Actual   :abc
Run Code Online (Sandbox Code Playgroud)

And*_*kin 9

虽然可以确定两个正则表达式是否接受相同的语言,但它似乎相当复杂,而且并非对日常正则表达式使用都非常有用.因此,编译的正则表达式模式上的相等只是引用相等:

val x = "abc".r
val y = "abc".r
x == y
// res0: Boolean = false
Run Code Online (Sandbox Code Playgroud)

  • @ fluency03而不是调用`toString()`,你可以问`x.regex == y.regex`,因为`.regex`已经包含了生成编译模式的字符串表示.这将是一种"廉价"的内涵平等,在某种程度上接近"有趣的"外延平等.对于`regex`,`equals`只是重定向到`eq`,这是参考相等. (2认同)

ste*_*ino 5

shouldBeScalatest 3.0.5中的方法将相等性检查委托给areEqualComparingArraysStructurally方法:

def shouldBe(right: Any): Assertion = {
  if (!areEqualComparingArraysStructurally(leftSideValue, right)) {
    val (leftee, rightee) = Suite.getObjectsForFailureMessage(leftSideValue, right)
    val localPrettifier = prettifier // Grabbing a local copy so we don't attempt to serialize AnyShouldWrapper (since first param to indicateFailure is a by-name)
    indicateFailure(FailureMessages.wasNotEqualTo(localPrettifier, leftee, rightee), None, pos)
  }
  else indicateSuccess(FailureMessages.wasEqualTo(prettifier, leftSideValue, right))
}
Run Code Online (Sandbox Code Playgroud)

而这又简单地将相等性检查(如您所料)委托给==操作员:

private[scalatest] def areEqualComparingArraysStructurally(left: Any, right: Any): Boolean = {
  // Prior to 2.0 this only called .deep if both sides were arrays. Loosened it
  // when nearing 2.0.M6 to call .deep if either left or right side is an array.
  // TODO: this is the same algo as in scalactic.DefaultEquality. Put that one in
  // a singleton and use it in both places.
  left match {
    case leftArray: Array[_] =>
      right match {
        case rightArray: Array[_] => leftArray.deep == rightArray.deep
        case _ => leftArray.deep == right
      }
    case _ => {
      right match {
        case rightArray: Array[_] => left == rightArray.deep
        case _ => left == right
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在Scala中,至少在JVM上,==只需调用equals,如果不被覆盖,则检查比较变量是否指向同一个对象.case classes是特殊的,因为编译器会覆盖equals您比较构造函数参数.

您可以使用以下方法轻松测试它(但您可以想象,这同样适用==于您自己使用):

package org.example

import org.scalatest.{FlatSpec, Matchers}

final class MyClass(val a: Int)

final case class MyCaseClass(a: Int)

final class TestSpec extends FlatSpec with Matchers {

  "equality on case classes" should "succeed" in {

    new MyCaseClass(1) shouldBe new MyCaseClass(1)

  }

  "equality on non-case classes" should "fail" in {

    new MyClass(1) shouldNot be(new MyClass(1))

  }

  "equality between Regex objects" should "fail" in {

    "abc".r shouldNot be("abc".r)

  }

}
Run Code Online (Sandbox Code Playgroud)

r方法所做的是实例化一个新Regex对象,该对象不是case class并且不会覆盖相等定义,从而产生您看到的结果.