在specs2 scala

时间:2016-12-03 07:09:46

标签: scala tdd numberformatexception specs2

我无法理解为什么会发生这种情况,让我们假设代码来自Specs 2 GWT clauses,这是

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) }
}

即为每个测试语句提供NumberFormatExceptionsbt 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而不适用于DoubleStandarParsers适用于anIntaDouble

  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) }
}

但为什么我可以像aDouble一样轻松使用anInt

这肯定是我遗漏的一些东西,但可以看出为什么这不起作用。

0 个答案:

没有答案