使用scala测试比较嵌套列表

使用scala测试比较嵌套列表,scala,scalatest,Scala,Scalatest,如何使用scala测试比较两个列表 val actual = Array(Row("1", "2"), Row("3", "4")) val fail_expected = Array(Row("1", "2"), Row("3", "2")) val pass_expected = Array(Row("3", "4"), Row("1", "2")) 我试过了 actual should contain theSameElementsAs pass_expected 但它不起作用,它说这

如何使用scala测试比较两个列表

val actual = Array(Row("1", "2"), Row("3", "4"))
val fail_expected = Array(Row("1", "2"), Row("3", "2"))
val pass_expected = Array(Row("3", "4"), Row("1", "2"))
我试过了

actual should contain theSameElementsAs pass_expected
但它不起作用,它说这两个阵列是不同的,而实际上它们是相同的

我正在将funsuite与scala测试一起使用。

请参阅。您需要通过以下任一方式实现元素的相等性,即
类:

  • 标准Scala方式,如:

    class Row(val elems: String*) {
    
        def canEqual(other: Any): Boolean = other.isInstanceOf[Row]
    
        override def equals(other: Any): Boolean = other match {
          case that: Row =>
            (that canEqual this) &&
              elems == that.elems
          case _ => false
        }
    
        override def hashCode(): Int = {
          val state = Seq(elems)
          state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
        }
    }
    object Row {
        def apply(elems: String*) = new Row(elems: _*)
    }
    
    或者干脆让它成为一个案例类:

    case class Row(elems: String*)
    
  • 或者提供隐式的
    相等[Row]
    实现:

    import org.scalactic.Equality
    implicit object RowEquals extends Equality[Row] {
        override def areEqual(a: Row, b: Any): Boolean = b match {
          case r: Row => a.elems == r.elems
          case _ => false
        }
    }
    
    • 请参阅。您需要通过以下任一方式实现元素的相等性,即
      类:

      • 标准Scala方式,如:

        class Row(val elems: String*) {
        
            def canEqual(other: Any): Boolean = other.isInstanceOf[Row]
        
            override def equals(other: Any): Boolean = other match {
              case that: Row =>
                (that canEqual this) &&
                  elems == that.elems
              case _ => false
            }
        
            override def hashCode(): Int = {
              val state = Seq(elems)
              state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
            }
        }
        object Row {
            def apply(elems: String*) = new Row(elems: _*)
        }
        
        或者干脆让它成为一个案例类:

        case class Row(elems: String*)
        
      • 或者提供隐式的
        相等[Row]
        实现:

        import org.scalactic.Equality
        implicit object RowEquals extends Equality[Row] {
            override def areEqual(a: Row, b: Any): Boolean = b match {
              case r: Row => a.elems == r.elems
              case _ => false
            }
        }
        

        • 一种快速但不是最好的解决方案是将数组转换为字符串。您可以使用
          mkstring
          方法来实现:

          简言之:

           actual.mkstring(",") == pass_expected.mkstring(",")
          

          一个快速但不是最好的解决方案是将数组转换为字符串。您可以使用
          mkstring
          方法来实现:

          简言之:

           actual.mkstring(",") == pass_expected.mkstring(",")
          

          将数组转换为列表,即
          val-actual=List(行(“1”、“2”)、行(“3”、“4”))
          将数组转换为列表,即
          val-actual=List(行(“1”、“2”)、行(“3”、“4”)