title: In SML, how to assert that a particular exception is thrown?
body: <p>Without taking the effort to actually clone JUnit or something, I'm throwing together a few utility functions to help test some SML code.  I do know about QCheck, but it can't do this one thing either and isn't what I want generally.  (But if you know of another automated-testing framework for SML, please speak up.)</p>
  
  <p>I'd like to be able to assert that some function will throw an exception, e.g., given a function</p>
  
  <pre><code>fun broken x = raise Fail
  </code></pre>
  
  <p>I'd like to be able to write something like</p>
  
  <pre><code>throws ("ERROR: function is not broken enough!", fn () =&gt; broken 1, Fail)
  </code></pre>
  
  <p>and have it throw an error if the given function does <em>not</em> raise the expected exception.</p>
  
  <p>I tried to write a <code>throws</code> function with type <code>(string * exn * (unit -&gt; unit)) -&gt; unit</code> like so:</p>
  
  <pre><code>  fun throws (msg, e, func) = func ()
      handle e' =&gt; if e = e'
                       then ()
                       else raise ERROR (SOME msg)
  </code></pre>
  
  <p>But this generates a bunch of compile-time errors, apparently because ML
  does not define equality over exceptions:</p>
  
  <pre><code>sexp-tests.sml:54.31-57.49 Error: types of rules don't agree [equality type required]
    earlier rule(s): ''Z -&gt; unit
    this rule: exn -&gt; 'Y
    in rule:
      exn =&gt; raise exn
  sexp-tests.sml:54.31-57.49 Error: handler domain is not exn [equality type required]
    handler domain: ''Z
    in expression:
      func ()
      handle 
          e' =&gt; if e = e' then () else raise (ERROR &lt;exp&gt;)
      | exn =&gt; raise exn
  </code></pre>
  
  <p>As a workaround, I suspect I could just reuse an existing <code>assert</code> function I have:</p>
  
  <pre><code>assert ((broken 1; false) handle Fail =&gt; true | _ =&gt; false)
  </code></pre>
  
  <p>But it's a bit more thinking and typing.</p>
  
  <p>So, is there any way to write that <code>throws</code> function in SML?</p>