ScottishTapWater
ScottishTapWater

Reputation: 4846

How to structure project for testing in dune

I've created an OCaml project using dune with the intent of running through these exercises:

dune init project ocaml_exercises

I've then created two lib files, last.ml and last_two.ml which contain one function a piece:

let rec last list =
  match list with
  | [] -> None
  | [x] -> Some x
  | _ :: tail -> last tail

let rec last_two list =
    match list with
    | [] -> None
    | [_] -> None
    | [x; y] -> Some (x, y)
    | _ :: tail -> last_two tail

Now, I'd like to test these, and I notice that dune has auto-created me a tests folder, great! So I've created two files in my test project: last_tests.ml and last_two_tests.ml. Resulting in a project dir that looks like this:

.
├ (build folder)
├── bin
│  ├── dune
│  └── main.ml
├── dune-project
├── lib
│  ├── dune
│  ├── last.ml
│  └── last_two.ml
├── ocaml_exercises.opam
└── test
   ├── dune
   ├── last_tests.ml
   └── last_two_tests.ml

So here's where my problem arises, I can't for the life of me work out how to correctly reference the functions in /lib/last[_two].ml from test/last[_two]_tests.ml.

My dune files look like:

// bin/dune
(executable
 (public_name ocaml_exercises)
 (name main)
 (modules Main)
 (libraries Ocaml_exercises))

//lib/dune
(library
 (name ocaml_exercises)
 (public_name ocaml_exercises)
 (modules Last Last_two))

//test/dune
(test
 (name ocaml_exercises)
 (libraries ocaml_exercises)
 (modules Last_tests Last_two_tests))

However, when I try to run dune test, I get:

File "bin/dune", line 4, characters 10-25: 4 | (modules ocaml_exercises) ^^^^^^^^^^^^^^^ Error: Module Ocaml_exercises doesn't exist. File "test/dune", line 2, characters 7-22: 2 | (name ocaml_exercises) ^^^^^^^^^^^^^^^ Error: The name "Ocaml_exercises" is not listed in the (modules) field of this stanza.

I'm somewhat confused here, my understanding was that:

Where am I going wrong here?

Upvotes: 0

Views: 653

Answers (1)

Richard Huxton
Richard Huxton

Reputation: 22972

Yes, the module-name is capitalised. No you don't capitalise a library (which could contain several modules).

So... you probably want in test/dune something like:

(libraries alcotest lib)

And then in your test files you refer to Lib.Last for the module.

Upvotes: 0

Related Questions