caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Cedric Cellier <rixed@happyleptic.org>
To: Bahman Movaqar <bahman@bahmanm.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] On variants, integers and testing
Date: Mon, 19 Mar 2018 11:41:46 +0100	[thread overview]
Message-ID: <20180319104145.GB80588@rxdmac.local> (raw)
In-Reply-To: <CACOw_MrR5M66=CB8Z=f7B+xiv5r8U87P3kE=p_8kyu8OCKCrjw@mail.gmail.com>

-[ Fri, Mar 16, 2018 at 12:12:56PM +0100, Bahman Movaqar ]----
> However, when it comes to testing, things are not that simple. Now that I
> have exported only 1 function in the .mli, I can only test that 1 function.
> Is this the proper way[2]?

This is one of the reasons some people don't like to use mli files.

Another approach is to define (and document) the signatures in ".ml" file
(named for instance "yadayada_intf.ml") and then the implementation can be
written and exposed fully, knowing that you can create another one and
constraint it to the signature later on. Example:

--- yadayada_intf.ml --- 8< --- 8< ---
(** This is library yadayada *)

module type S =
sig
  (** The type of yadayadas *)
  type t

  (** Create a yadayada from a xyz: *)
  val create : xyz -> t

  (** Call this to perform a foo with your yadayada created with [create]: *)
  val foo : t -> wooz
end
--- >8 --- >8 ---

Then you implement the actual thing in yadayada_impl.ml:

--- yadayada_impl.ml --- 8< --- 8< ---
(* Implementation for yadayada *)

type t = ...
let create x = ...
let foo t = ...
--- >8 --- >8 ---

That implementation you can test from "yadayada_tests.ml" for instance, since
none of it is hidden.

Then, you can provide users with a simplified module Yadayada.M, defined in
"yadayada.ml":

--- yadayada.ml --- 8< --- 8< ---
module M = Yadayada_impl : Yadayada_intf.S
--- >8 --- >8 ---

Plenty of variations are possible of course, but the names "t" for the main
type, "S" for the main signature and "M" for the main module (or "Make" if it's
a functor) are common.

The drawback of this technique is that it tends to lead toward a design with
plenty of modules and functors, which makes reading the code more complex (and
is also harder to inline efficiently).

Note that for inline tests you have the qtest
(https://github.com/vincent-hugot/qtest) tool that I find quite nice for inline
tests as long as you do not try to shoehorn it into the large functional test
framework it is not designed to be.


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2018-03-19 10:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-16 11:12 Bahman Movaqar
2018-03-19 10:41 ` Cedric Cellier [this message]
2018-03-19 10:44   ` Cedric Cellier
2018-03-19 13:27 ` Yawar Amin
2018-03-19 13:49   ` Yawar Amin
2018-03-19 21:24 ` SP
2018-03-21 14:36   ` Lukasz Stafiniak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180319104145.GB80588@rxdmac.local \
    --to=rixed@happyleptic.org \
    --cc=bahman@bahmanm.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).