Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 仅比较case类中的字符串部分_Scala_Specs2 - Fatal编程技术网

Scala 仅比较case类中的字符串部分

Scala 仅比较case类中的字符串部分,scala,specs2,Scala,Specs2,是否可以只比较case类属性中字符串的一部分。 使用scala specs2库 有可能写这样的匹配器吗 CaseClass(property=Prop(“超长字符串”))必须是qualto(CaseClass(property=Prop(“%long%”)) 这样试试看 下面是一个工作示例 import scala.language.experimental.macros import org.specs2._ import org.specs2.matcher.MatcherMacros c

是否可以只比较case类属性中字符串的一部分。 使用scala specs2库

有可能写这样的匹配器吗

CaseClass(property=Prop(“超长字符串”))必须是qualto(CaseClass(property=Prop(“%long%”))
这样试试看

下面是一个工作示例

import scala.language.experimental.macros
import org.specs2._
import org.specs2.matcher.MatcherMacros

class QuickStartSpec extends Specification with MatcherMacros { def is = s2"""
 The 'Case class matchers' should
   match on a part of a string                                   $e1
                                                                 """
   def e1 = {
    case class Foo(property: String)
    val foo = Foo(property = "very long string")
    foo must matchA[Foo].property(_ must =~("long"))
  }
}
在哪里

使用标准匹配器

myCaseInst.property.myString must be matching(".*long.*")

不确定我是否缺少某些导入,或者是什么原因,但使用此模块我得到编译错误:
类型不匹配,预期:Matcher[this.foo],实际:Any
@EdgarasBuchvalovas我已使用导入编辑了答案。
libraryDependencies ++= Seq(
  "org.specs2" %% "specs2-core" % "4.6.0" % "test",
  "org.specs2" %% "specs2-matcher-extra" % "4.6.0" % "test"
),
myCaseInst.property.myString must be matching(".*long.*")