From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id pA8Foltw009287 for ; Tue, 8 Nov 2011 16:50:47 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AosBACtPuU5KfVI0imdsb2JhbABDml6EQIMGAYY8gR4IIgEBAQoJDQcSBiGBcgEBAQMBEgITGQEbHQEDAQsGBQsNLiECEQEFARwGEyKHYAIGmgwKi2GCY4VvPYhwAgUKiEBjBIJakUeFMYEvg2CDAT2DcQ X-IronPort-AV: E=Sophos;i="4.69,477,1315173600"; d="scan'208,217";a="129204842" Received: from mail-ww0-f52.google.com ([74.125.82.52]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 08 Nov 2011 16:50:41 +0100 Received: by wwn31 with SMTP id 31so1066288wwn.9 for ; Tue, 08 Nov 2011 07:50:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :message-id:references:to:x-mailer; bh=WLciTN9IEXOWtIebR29nl+pilDKzZU2VwTXVkGJGATY=; b=jJJsM/Rc1L+6rwTwqpuZ4RjsBS4O9j6/7fJTX/wkiYmuetGgYeylo0at1+jAFAPPBj vHF96myMT1+ICYmQHUOQdT+47yEsEAl2hDnjsx/G/CFCs5SFO6FD2lsS9HpfZ9jwLCCW jqrztqa3B/mGb5kqVCI5Bmk6e/kXW80Vkt6G0= Received: by 10.180.74.197 with SMTP id w5mr13534406wiv.25.1320767441161; Tue, 08 Nov 2011 07:50:41 -0800 (PST) Received: from [192.168.0.13] (gou06-3-88-170-165-56.fbx.proxad.net. [88.170.165.56]) by mx.google.com with ESMTPS id v6sm1094469wiv.10.2011.11.08.07.50.38 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 08 Nov 2011 07:50:39 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: multipart/alternative; boundary=Apple-Mail-35-1010337094 From: Thomas Gazagnaire In-Reply-To: Date: Tue, 8 Nov 2011 16:50:36 +0100 Cc: Gabriel Scherer , caml-list@inria.fr Message-Id: References: To: Hans Ole Rafaelsen X-Mailer: Apple Mail (2.1084) Subject: Re: [Caml-list] Include question --Apple-Mail-35-1010337094 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii You can have a look at http://www.ocamlpro.com/code/2011-08-10-ocaml-pack-f= unctors.html It describes how to use either a patched version of OCaml or some external = tools to automatically functorize modules to solve your problems. -- Thomas On Nov 8, 2011, at 4:45 PM, Hans Ole Rafaelsen wrote: > I was hoping not to have to change the interfaces of the application. Mo= st of the application is already written and the testing was just an aftert= hought :-( >=20 > But it might not be that much that need to be changed. I'll give it a sho= t. >=20 > Thanks >=20 > On Tue, Nov 8, 2011 at 4:10 PM, Gabriel Scherer wrote: > If you want some module of your system to be parametrized by another > module (to be able to pass either a concrete module or a mockup > module), you should use a functor. >=20 > http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15 >=20 > in logic.ml > module Make (Net_lib : Net_interface) =3D struct > ... > end >=20 > in main.ml: > module Logic =3D Logic.Make(Net_lib) > .. >=20 > in main_mockup.ml: > module Logic =3D Logic.Make(Net_mockup) > ... >=20 > On Tue, Nov 8, 2011 at 4:03 PM, Hans Ole Rafaelsen = wrote: > > Hi, > > > > I'm trying to create a mockup module to replace a network module when d= oing > > testing. > > > > The application consists of basically 3 parts. Some user interaction. T= his > > calls some logic of the application, and the logic module might need to= call > > some other functions over the network. For testing I want to make a moc= kup > > of the network module, in addition to synthesize the user interaction. I > > want to test the logic module. The application has the following files: > > > > net_lib.ml: > > let util_fun a =3D a > > let foo a b =3D a + b > > let bar a b =3D a - b > > > > logic.ml: > > let state =3D ref 0 > > let get_state () =3D !state > > let do_op a b =3D > > incr state; > > (Net_lib.foo a b) * (Net_lib.bar (Net_lib.util_fun a) b ) > > > > main_appl.ml: > > let () =3D > > let a, b =3D Scanf.scanf "%d %d\n" (fun a b -> a,b) in > > Printf.printf "Foo %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b) > > > > let () =3D > > let a, b =3D Scanf.scanf "%d %d\n" (fun a b -> a,b) in > > Printf.printf "Foo %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b) > > > > > > For testing I have the following modules: > > net_lib_mockup.ml > > include Net_lib > > let foo a b =3D 1 > > let bar a b =3D 1 > > > > logic_mockup.ml: > > module Net_lib =3D Net_lib_mockup > > include Logic > > > > main_test.ml: > > module Logic =3D Logic_mockup > > > > let () =3D > > let a, b =3D (1, 1) in > > Printf.printf "Test %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b) > > > > let () =3D > > let a, b =3D (10, 1) in > > Printf.printf "Test %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b) > > > > The problem is that the "include Logic" has already 'bound' the functio= ns in > > Logic to the Net_lib module and will not use the Net_lib_mockup that I = try > > to use through a moudle alias. If i replace the 'include Logic' with the > > actual content of the logic.ml file, then the functions get bound to the > > Logic_mockup functions, and the test works as they should. > > > > Are there some trick to get the 'include Logic', in logic_mockup.ml, to= use > > to the Net_lib_mockup module and not the Net_lib module, so that I don't > > have to do copy and paste between the two files? > > > > > > Regards, > > > > Hans Ole Rafaelsen > > > > >=20 --Apple-Mail-35-1010337094 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=us-ascii You can have a look at http://www.ocamlpro.com/code/2011-08-10-ocaml-pack-functors.html

It describes how to use either a patched version of OCaml or some external tools to automatically functorize modules to solve your problems.

--
Thomas

On Nov 8, 2011, at 4:45 PM, Hans Ole Rafaelsen wrote:

I was hoping not to have to change the interfaces of the application.  Most of the application is already written and the testing was just an afterthought :-(

But it might not be that much that need to be changed. I'll give it a shot.

Thanks

On Tue, Nov 8, 2011 at 4:10 PM, Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
If you want some module of your system to be parametrized by another
module (to be able to pass either a concrete module or a mockup
module), you should use a functor.

 http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html#toc15

in logic.ml
 module Make (Net_lib : Net_interface) = struct
   ...
 end

in main.ml:
 module Logic = Logic.Make(Net_lib)
 ..

in main_mockup.ml:
 module Logic = Logic.Make(Net_mockup)
 ...

On Tue, Nov 8, 2011 at 4:03 PM, Hans Ole Rafaelsen <hrafaelsen@gmail.com> wrote:
> Hi,
>
> I'm trying to create a mockup module to replace a network module when doing
> testing.
>
> The application consists of basically 3 parts. Some user interaction. This
> calls some logic of the application, and the logic module might need to call
> some other functions over the network. For testing I want to make a mockup
> of the network module, in addition to synthesize the user interaction. I
> want to test the logic module. The application has the following files:
>
> net_lib.ml:
> let util_fun a = a
> let foo a b = a + b
> let bar a b = a - b
>
> logic.ml:
> let state = ref 0
> let get_state () = !state
> let do_op a b =
>   incr state;
>   (Net_lib.foo a b) * (Net_lib.bar (Net_lib.util_fun a) b )
>
> main_appl.ml:
> let () =
>   let a, b  = Scanf.scanf  "%d %d\n" (fun a b -> a,b) in
>   Printf.printf "Foo %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b)
>
> let () =
>   let a, b  = Scanf.scanf  "%d %d\n" (fun a b -> a,b) in
>   Printf.printf "Foo %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b)
>
>
> For testing I have the following modules:
> net_lib_mockup.ml
> include Net_lib
> let foo a b = 1
> let bar a b = 1
>
> logic_mockup.ml:
> module Net_lib = Net_lib_mockup
> include Logic
>
> main_test.ml:
> module Logic = Logic_mockup
>
> let () =
>   let a, b = (1, 1) in
>   Printf.printf "Test %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b)
>
> let () =
>   let a, b = (10, 1) in
>   Printf.printf "Test %d %d\n%!" (Logic.get_state ()) (Logic.do_op a b)
>
> The problem is that the "include Logic" has already 'bound' the functions in
> Logic to the Net_lib module and will not use the Net_lib_mockup that I try
> to use through a moudle alias. If i replace the 'include Logic' with the
> actual content of the logic.ml file, then the functions get bound to the
> Logic_mockup functions, and the test works as they should.
>
> Are there some trick to get the 'include Logic', in logic_mockup.ml, to use
> to the Net_lib_mockup module and not the Net_lib module, so that I don't
> have to do copy and paste between the two files?
>
>
> Regards,
>
> Hans Ole Rafaelsen
>
>


--Apple-Mail-35-1010337094--