Prerequisite that streams a throwable then a non-throwable?

17 views
Skip to first unread message

Barry Wark

unread,
Mar 24, 2015, 10:57:38 PM3/24/15
to mi...@googlegroups.com
(apologies if this double-posts; I seem to be having trouble with Google Groups today)

I would like to test a fact that my function handles intermittent throw exceptions like this:

(fact "`my-fn` handles exceptions thrown by `might-throw`"
  (my-fn "foo") => "bar"
  (provided
    (might-throw "foo") =streams=> [(Exception.) "bar" ]))

But =streams=> doesn't seem to handle throwables. I could use

(fact "`my-fn` handles exceptions thrown by `might-throw`"
  (my-fn "foo") => "bar"
  (provided
    (might-throw "foo") =throws=> (Exception.)))

for the throwable, but when I add a second prerequisite like this

(fact "`my-fn` handles exceptions thrown by `might-throw`"
  (my-fn "foo") => "bar"
  (provided
    (might-throw "foo") =throws=> (Exception.)
    (might-throw "foo") => "bar"))

the second (non-throwing) version doesn't seem to get used (as I expected).

When something is hard in Midje, I assume I'm on the wrong path. What's a better way to test this behavior?

Barry

Brian Marick

unread,
Apr 17, 2015, 4:59:44 PM4/17/15
to mi...@googlegroups.com
Sorry for the delayed reply.

> I would like to test a fact that my function handles intermittent throw
> exceptions like this:
>
> (fact "`my-fn` handles exceptions thrown by `might-throw`"
> (my-fn "foo") => "bar"
> (provided
> (might-throw "foo") =streams=> [(Exception.) "bar" ]))

The way I would handle this is to decompose the problem into three
problems, tested independently. That means three functions:

1. One that converts a sequence of function calls into a lazy sequence
of values. [1]
2. One that converts an exception into some kind of default value.
3. One that composes the two.

I've put an example here:
https://github.com/marick/Midje/blob/master/test/as_documentation/prerequisites/streaming_with_exceptions.clj

Brian Marick

unread,
Apr 17, 2015, 5:38:59 PM4/17/15
to mi...@googlegroups.com


Brian Marick wrote:
> 1. One that converts a sequence of function calls into a lazy sequence
> of values. [1]

Forgot the shameless plug:

[1] As described in the "Pushing Bookkeeping into the Runtime: Laziness
and Immutability" chapter of my /Functional Programming for the
Object-Oriented Programmer/ https://leanpub.com/fp-oo
Reply all
Reply to author
Forward
0 new messages