caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Exceptions Handling and Unit Testing.
@ 2004-09-20 20:21 chris.danx
  2004-09-20 20:27 ` chris.danx
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: chris.danx @ 2004-09-20 20:21 UTC (permalink / raw)
  To: Caml Mailing List

Hi,

As I've been writing test code, I've noticed some patterns which occur 
over and over again.  All of the patterns have to do with construction. 
  The tests currently follow one of two patterns.

The first deals with conditions on which construction of an object 
should fail, and looks like this

let some_construction_test some_op input exception error_msg =
   try
     let x = some_op input
     in
       OUnit.assert_failure error_msg
   with
     exception -> ()


The second deals with asserting a set of conditions are true after a 
successful construction and would like something like this.

let
   some_test some_op input (conditions : ('a -> 'b -> bool) * ('a * 'b * 
string) list) =
   let p = some_op input
   in
     check_conditions p conditions ...

The second can be coded easily in Ocaml and is included just to set the 
scene as it where.  Ideally I'd like to implement the first pattern but 
don't know how.  The problem is the exception!  It should work ok for an 
exception (constructor) that has no arguments, but doesn't seem to work 
for exceptions with arguments.  OUnit already provides assert_failure 
which takes an expression and checks it raises a specific exception 
however it has the same problem.

Handling

exception Rtfm_error of string;;

is a problem but

exception Rtfm_error;;

is not.

Am I missing something?  Is this possible?  I looked to see if the 
exception could be caught and checked at runtime, but the support in the 
OCaml libraries for exceptions seems weak.  The exception can be caught 
via a catch all pattern, but there seems to be no way to determine if it 
is the desired exception (a success) or another exception (an error).


Cheers,
Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Exceptions Handling and Unit Testing.
  2004-09-20 20:21 [Caml-list] Exceptions Handling and Unit Testing chris.danx
@ 2004-09-20 20:27 ` chris.danx
  2004-09-20 20:59 ` art yerkes
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: chris.danx @ 2004-09-20 20:27 UTC (permalink / raw)
  To: chris.danx; +Cc: Caml Mailing List

chris.danx wrote:

> OUnit already provides assert_failure which takes an expression ...
> however it has the same problem.

Oops!  That should be assert_raises, not assert_failure!

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Exceptions Handling and Unit Testing.
  2004-09-20 20:21 [Caml-list] Exceptions Handling and Unit Testing chris.danx
  2004-09-20 20:27 ` chris.danx
@ 2004-09-20 20:59 ` art yerkes
  2004-09-21  8:24 ` Damien Doligez
  2004-09-21 23:23 ` chris.danx
  3 siblings, 0 replies; 5+ messages in thread
From: art yerkes @ 2004-09-20 20:59 UTC (permalink / raw)
  To: caml-list

On Mon, 20 Sep 2004 21:21:26 +0100
"chris.danx" <chris.danx@ntlworld.com> wrote:

> let some_construction_test some_op input exception error_msg =
>    try
>      let x = some_op input
>      in
>        OUnit.assert_failure error_msg
>    with
>      exception -> ()

with is an ordinary pattern:

exception Foo of int ;;
exception Bar ;;
let f x = if x < 1 then raise (Foo 7) else if x > 100 then raise Bar else x - 1 ;;
let g x = try f x with Foo n -> n ;;

# g 3 ;;
- : int = 2

# g (-1) ;;
- : int = 7

# g 101 ;;
Exception: Bar.

-- 
Hey, Adam Smith, keep your invisible hands to yourself!

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Exceptions Handling and Unit Testing.
  2004-09-20 20:21 [Caml-list] Exceptions Handling and Unit Testing chris.danx
  2004-09-20 20:27 ` chris.danx
  2004-09-20 20:59 ` art yerkes
@ 2004-09-21  8:24 ` Damien Doligez
  2004-09-21 23:23 ` chris.danx
  3 siblings, 0 replies; 5+ messages in thread
From: Damien Doligez @ 2004-09-21  8:24 UTC (permalink / raw)
  To: chris.danx; +Cc: Caml Mailing List

On Sep 20, 2004, at 22:21, chris.danx wrote:

> let some_construction_test some_op input exception error_msg =
>   try
>     let x = some_op input
>     in
>       OUnit.assert_failure error_msg
>   with
>     exception -> ()

Turn your pattern into a function:

   let some_construction_test some_op input (check_exception: exn -> 
unit) error_msg =
   try
       let x = some_op input
       in
         OUnit.assert_failure error_msg
     with
       e -> check_exception e

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Exceptions Handling and Unit Testing.
  2004-09-20 20:21 [Caml-list] Exceptions Handling and Unit Testing chris.danx
                   ` (2 preceding siblings ...)
  2004-09-21  8:24 ` Damien Doligez
@ 2004-09-21 23:23 ` chris.danx
  3 siblings, 0 replies; 5+ messages in thread
From: chris.danx @ 2004-09-21 23:23 UTC (permalink / raw)
  To: chris.danx; +Cc: Caml Mailing List

Oops!  I keep clicking reply instead of reply all! :(

Thanks for all the suggestions.  It looks like I was generalising the 
wrong thing (concentrating on handling the exception, rather than the 
identification of a test repeated for different inputs).

Instead I created a function which does the test and catches a specific 
exception but with the test data supplied as parameters.  This enables 
constructing tests as closures from sample cases (each case has a test 
name, sample data and a message should the test fail), with the 
extension to lists of sample cases being a trivial application of 
List.map.  Cases are added by putting them in a list.

Simple really. *slaps himself for being stupid*


Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-09-21 23:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-20 20:21 [Caml-list] Exceptions Handling and Unit Testing chris.danx
2004-09-20 20:27 ` chris.danx
2004-09-20 20:59 ` art yerkes
2004-09-21  8:24 ` Damien Doligez
2004-09-21 23:23 ` chris.danx

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).