HMock icon indicating copy to clipboard operation
HMock copied to clipboard

IO in expectAny expectation constructor predicate

Open tim2CF opened this issue 3 years ago • 1 comments

In this example I can see that PollChat_ expectation constructor accepts Predicate, in this case anything. Is there the good way to use IO-like expressions (with side effects) to create predicates (like anything)? Sometimes it would be super useful to call database/webservice hosted in local test environment to verify that argument is genuine. This especially useful in cases where code which is tested is using some sort of randomness involved, for example request should be always unique due dynamically generated nonce and signed on top of it with some private key. I hope I manged to explain the use case. Best regards!

        expectAny $
          PollChat_ anything
            |-> (User "A", "I love Haskell")
            |-> (User "B", "Lovin' the ass. candies")
            |-> (User "B", "!leave")

tim2CF avatar Sep 09 '22 19:09 tim2CF

Sorry to miss this! I'm looking now.

One thought here is that logically, predicates should be pure. You are describing a case where you have a predicate that's sort of "morally pure" in the sense that you don't care to track the I/O. Well, that's precisely what unsafePerformIO means, and you can certainly construct a Predicate using is from explainable-predicates along with unsafePerformIO.

The counter-argument would be, I suppose, that you really do care about the effect. In the example of looking something up in a database, you actually want to specifically check that it's in the database when the action is performed, and not at some other arbitrary time. So my initial reaction is probably wrong. I suppose this argues for either extending explainable-predicates to handle predicates with effects, or offering some HMock-specific alternative. I'm leaning towards the latter at the moment.

cdsmith avatar Oct 16 '22 17:10 cdsmith