在specs2 scala中使用aDouble时出现NumberFormatException
我不明白为什么会发生这种情况,让我们假设代码来自在specs2 scala中使用aDouble时出现NumberFormatException,scala,tdd,numberformatexception,specs2,Scala,Tdd,Numberformatexception,Specs2,我不明白为什么会发生这种情况,让我们假设代码来自 class GWTSpec extends Specification with GWT with StandardRegexStepParsers { def is = s2""" A given-when-then example for a calculator ${calculator1.start} Given the following number: 1 And a seco
class GWTSpec extends Specification with GWT with StandardRegexStepParsers { def is = s2"""
A given-when-then example for a calculator ${calculator1.start}
Given the following number: 1
And a second number: 2
And a third number: 6
When I use this operator: +
Then I should get: 9
And it should be >: 0 ${calculator1.end}
Now with the multiplication
"""
val anOperator = readAs(".*: (.)$").and((s: String) => s)
val calculator1 =
Scenario("calculator1")
.given(anInt)
.given(anInt)
.given(anInt)
.when(anOperator) { case op :: i :: j :: k :: _ => if (op == "+") i+j+k else i*j*k }
.andThen(anInt) { case expected :: sum :: _ => sum === expected }
.andThen(anInt) { case expected :: sum :: _ => sum must be_>(expected) }
}
这很好,但是,如果我想用双人的呢?例如:
class GWTSpec extends Specification with GWT with StandardRegexStepParsers { def is = s2"""
A given-when-then example for a calculator ${calculator1.start}
Given the following number: 1.0
And a second number: 2.0
And a third number: 6.0
When I use this operator: +
Then I should get: 9.0
And it should be >: 0.0 ${calculator1.end}
"""
val anOperator = readAs(".*: (.)$").and((s: String) => s)
val calculator1 =
Scenario("calculator1")
.given(aDouble)
.given(aDouble)
.given(aDouble)
.when(anOperator) { case op :: i :: j :: k :: _ => if (op == "+") i+j+k else i*j*k }
.andThen(aDouble) { case expected :: sum :: _ => sum === expected }
.andThen(aDouble) { case expected :: sum :: _ => sum must be_>(expected) }
}
即为每个测试语句提供NumberFormatException
,sbt testOnly GWTSpec的输出为:
> testOnly GWTSpec
[info] GWTSpec
[info] A given-when-then example for a calculator
[info]
[info] Given the following number: 1.0
[error] !
[error] java.lang.NumberFormatException: For input string: "And a third number: 6.0" (FloatingDecimal.java:2043)
[error] sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
[error] sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
[info] And a second number: 2.0
[error] !
[error] java.lang.NumberFormatException: For input string: "And a second number: 2.0" (FloatingDecimal.java:2043)
[error] sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
[error] sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
[info] And a third number: 6.0
[error] !
[error] java.lang.NumberFormatException: For input string: "Given the following number: 1.0" (FloatingDecimal.java:2043)
[error] sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
[error] sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
[info] When I use this operator: +
[info] o o Then I should get: 9.0
[info] o And it should be >: 0.0
[info]
[info] Total for specification GWTSpec
[info] Finished in 478 ms
[info] 2 examples, 6 expectations, 0 failure, 3 errors, 3 skipped
[info]
[error] Error: Total 5, Failed 0, Errors 3, Passed 0, Skipped 2
[error] Error during tests:
[error] GWTSpec
但是我不明白为什么它与Int
一起工作,而与Double
不一起工作,anInt
和aDouble
的标准参数是:
private val wholeNumber = """-?\d+"""
private val decimalNumber = """(\d+(\.\d*)?|\d*\.\d+)"""
def anInt = groupAs(wholeNumber).and((_: String).trim.toInt)
def aDouble = groupAs("-?"+decimalNumber).and((_: String).trim.toDouble)
为了工作,我必须声明一个新的RegExParser
,如下所示:
class GWTSpec extends Specification with GWT with StandardRegexStepParsers { def is = s2"""
A given-when-then example for a calculator ${calculator1.start}
Given the following number: 1.0
And a second number: 2
And a third number: 6
When I use this operator: +
Then I should get: 9.0
And it should be >: 0.0 ${calculator1.end}
"""
val anOperator = readAs(".*: (.)$").and((s: String) => s)
val myD = readAs(".*: (\\d+(\\.\\d*)?|\\d*\\.\\d+)$").and((s: String) => s.toDouble)
val calculator1 =
Scenario("calculator1")
.given(myD)
.given(myD)
.given(myD)
.when(anOperator) { case op :: i :: j :: k :: _ => if (op == "+") i+j+k else i*j*k }
.andThen(myD) { case expected :: sum :: _ => sum === expected }
.andThen(myD) { case expected :: sum :: _ => sum must be_>(expected) }
}
但是为什么我可以像使用一个it一样简单地使用aduble
这肯定是我缺少的一些东西,但我可以理解为什么这不起作用。这在3.8.6
上对我来说很好。我也在使用3.8.6。不知道发生了什么这在3.8.6
上对我来说很好。我也在使用3.8.6。我不知道发生了什么