English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
invoke function from its name as string
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-03-13 (16:42)
From: Andrej Bauer <Andrej.Bauer@f...>
Subject: Re: [Caml-list] invoke function from its name as string
Ludovic Coquelle wrote:
> Thanks for this answer.
> Problem I'm trying to solve is the following:
> I use 'make_suite' which is a program that do regex matching on source
> code to extract a list of function that looks like OUnit tests; from
> this list, it write an ocaml source code file which is a test case
> that call all the previous functions found.
> (see: http://skydeck.com/blog/programming/unit-test-in-ocaml-with-ounit/)

I looked at the blog post. The idea is to interleave the source code 
with special test functions and extract those automatically. You have 
chosen to do this by searching the source code with regular expressions, 
looking for functions with a certain name. If I may be honest and will 
all due respect: this is a really horrible idea. It is fragile, 
sensitive to mistakes, you have no guarantee that all the test functions 
were actually found (say what if someone mispells the name of one of 
them slightly), and so on. It is just really bad.

How about the following solution, in which I naively assume that test 
functions are supposed to return bool, but this is easily fixed. Define 
a module "Test" somewhat like this:

(** The list of tests registered by the source code. *)
let tests = ref []

(** Register a function as a test. *)
let register name test =
   tests := (name, test) :: !tests

(** Run all tests, maybe we can combine this with OUnit? *)
let run_tests () =
     (fun (name,test) ->
        if not (test ()) then failwith ("FAILED: " ^ name))

In your source code, whenever you want to have a test you just write:

Test.register "some_name" (fun () ->
   (*test code here*)

This is essentially the same overhead as what you have in your current 
solution, except that it is robust, ocaml will check that it is ok, and 
you do not have to come up with names of test functions of the form 
test_... all the time. The names of tests are strings, they can be more 

To run your program, you do not do anything special. There will be a 
small initialization cost when the test functions are collected in the list.

Tu run the tests, you link your source code with something like


Test.run_tests ()


You can easily extend this idea to using OUnit inside test.ml or do 
whatever you like. The important thing is that you do not search the 
source code in a naive and fragile way that requires to programmer to 
follow arbitrary naming conventions.

Best regards,