Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A bug in Scalatest 'DoubleTolerance'?

I've encountered failing tests which in all of my understanding should pass. Am I missing something obvious, here?

import org.scalatest._

class xxxTests extends FlatSpec with ShouldMatchers {
  import math.{Pi => PI}

  "" should "(this should pass)" in {
    assert( 0.0 === 0.0 )  // ok

    (1e-100) should equal ((0.0) plusOrMinus 1e-5)    // FAILS!!!  "1.0E-100 did not equal DoubleTolerance(0.0,1.0E-5)"
    (1e-3) should not equal ((0.0) plusOrMinus 1e-5)    // ok
    (0.0) should equal ((0.0) plusOrMinus 1e-5)    // FAILS!!!  "0.0 did not equal DoubleTolerance(0.0,1.0E-5)"
  }
}

I've experienced this with both Scalatest 1.8 and 2.0M4.

like image 252
akauppi Avatar asked Oct 19 '12 08:10

akauppi


1 Answers

The problem: must use be, not equal.

Leaving here as a sign of my stupidity (ehem, lack of focus).

https://groups.google.com/forum/?fromgroups=#!msg/scalatest-users/pb54GzOej6I/C9714h_OW_UJ

You must use plusOrMinus with "be" not "equal". "equal" always compares for equality by invoking == on one object, passing in the other. "be" does different things depending on what object is being passed. So try:

0.5 must be (0.5 plusOrMinus 0.1)

like image 189
akauppi Avatar answered Sep 21 '22 03:09

akauppi