0
votes

I have a case where I have a list of rules that can disqualify a particular item (a command). Each of the rules is a high order function. (For reusability).

At the end I need to verify what rule caused the failure. How can I identify what rule caused the failure?

Is there a way that I can get the function name for where the function came from?

When I'm testing and I return back the function that caused the failure I'm getting a non-descriptive "" which doesn't correlate to what is actually returned.

An example of this:

   object Library { 
       def testIf5(v:Int): Boolean = { v==5} 
   }

   class CommandVerifier { 
      def doesItFail(value: Int, rules: List[(Int)=>Boolean]) {
           rules.find(r=> !r(value))
      }

   }

   val expected = testIf5
   val actaul = new CommandVerifier().doesItFail(5, List(testIf5))
   actual should be expected (expected) 
1
can you be a bit more specific about what you mean by "verify" and "identify"? Verify and identify in what way?Jordan Cutler
In the example I'm trying to do a comparison against the function that i expect to have been returned and what it actually was. I want to be able to make the comparision there.monksy
Also, it appears in my IDE it thinks the return type of .doesItFail when assigning it to actaul thinks it is Unit. Try specifying the return type to be Option[(Int) => Boolean]. def doesItFail(value: Int, rules: List[(Int)=>Boolean]): Option[(Int) => Boolean] = { rules.find(r => !r(value)) }Jordan Cutler
Also, if you want the function name at runtime, that won't be possible since functions don't have names at runtime. See stackoverflow.com/questions/44408880/… and stackoverflow.com/questions/33790311/…. I think you should be testing these rules independently.Jordan Cutler
That sounds like the answer right there.monksy

1 Answers

1
votes

If you want the function name at runtime, that won't be possible since functions don't have names at runtime. See scala get function name that was sent as param and How do I get the function name in Scala?. I think you should be testing these rules independently