mercredi 25 septembre 2019

Testing exceptions in Scala

I want to test (in Intellij Idea testing framework) the following code:

def eval(t: Term): Term = t match {
    case t if isVal(t) => t
    case If(t1,t2,t3) if True == eval(t1) => eval(t2)
    case If(t1,t2,t3) if False == eval(t1) => eval(t3)
    case Succ(t) if isNum(eval(t)) => Succ(eval(t))
    case Pred(t) => eval(t) match {
      case Zero => Zero
      case Succ(v) if isNum(v) => v
      case _ => throw TermIsStuck(Pred(eval(t)))
    }
    case IsZero(t) => eval(t) match {
      case Zero => True
      case Succ(v) if isNum(v) =>False
      case _ => throw TermIsStuck(IsZero(eval(t)))
    }
    case _ => throw TermIsStuck(t)
  }

For a reference you can see this repository. So I wrote the following test:

 test("testEval") {

    Arithmetic.term(new Arithmetic.lexical.Scanner("if iszero pred pred 2 then if iszero 0 then true else false else false")) match {
      case Arithmetic.Success(res,next) => assert(eval(res) == True)
      case Arithmetic.Failure(msg,next) => assert(false)
    }

    Arithmetic.term(new Arithmetic.lexical.Scanner("pred succ succ succ false")) match {
      case Arithmetic.Success(res,next) => assert(Arithmetic.eval(res) == TermIsStuck(Succ(False)))
      case Arithmetic.Failure(msg,next) => assert(false)
    }

  }

Getting the error:

Succ(False)

fos.Arithmetic$TermIsStuck: Succ(False) ...

How can I test this catching the error? Indeed this test should be passed...

Aucun commentaire:

Enregistrer un commentaire