作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下用于测试 IdentifierRe
的代码和 IntPart
:
// Code under test
val IdentifierRe = "([a-z]+)([0-9]+)".r
object IntPart {
def unapply(arg: String): Option[Int] = Some(arg.toInt)
}
// Test cases
"foo123" should matchPattern { case IdentifierRe("foo", IntPart(123)) => }
"foo 123" should not fullyMatch IdentifierRe
warning: method unapplySeq in class Regex is deprecated (since 2.11.0): extracting a match result from anything but a CharSequence or Match is deprecated
matchPattern
接受
PartialFunction[Any, _]
,导致弃用
Regex#unapplySeq(Any)
用于提取。我可以解决它:
"foo123" match {
case IdentifierRe("foo", IntPart(123)) => succeed
case _ => fail
}
"foo123" should fullyMatch regex (IdentifierRe withGroups("foo", "123"))
IntPart
的同时,是否有更简洁的方法来避免警告?测试用例中的提取器?这个想法是
IdentifierRe
和
IntPart
经常一起用于模式匹配,我们想在测试用例中模仿它。
最佳答案
考虑定义 custom matcher像这样
def matchRegexPattern(right: PartialFunction[String, _]): Matcher[String] =
(left: String) =>
MatchResult(
right.isDefinedAt(left),
s"$left does not match regex pattern",
s"$left does match regex pattern "
)
Any
与
String
在
PartialFunction[String, _]
应该注意警告。现在像这样模式匹配
"foo123" should matchRegexPattern { case IdentifierRe("foo", IntPart(123)) => }
关于regex - 避免在 ScalaTest 中针对正则表达式的 matchPattern 上的弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57984595/
我是一名优秀的程序员,十分优秀!