|
| 1 | +Testing the instantiation of parameterised inline tests. |
| 2 | + |
| 3 | + $ cat >> dune-project <<EOF |
| 4 | + > (lang dune 3.20) |
| 5 | + > (using oxcaml 0.1) |
| 6 | + > EOF |
| 7 | + |
| 8 | +We first define a parameter signature: |
| 9 | + |
| 10 | + $ mkdir param |
| 11 | + $ echo 'val param : string' > param/param.mli |
| 12 | + $ cat > param/dune <<EOF |
| 13 | + > (library_parameter (name param)) |
| 14 | + > EOF |
| 15 | + |
| 16 | +Then a parameterised library, which uses inline tests: |
| 17 | + |
| 18 | + $ mkdir lib |
| 19 | + $ cat > lib/lib.ml <<EOF |
| 20 | + > let param = Param.param |
| 21 | + > let%test _ = Param.param = "impl" |
| 22 | + > EOF |
| 23 | + $ cat > lib/dune <<EOF |
| 24 | + > (library |
| 25 | + > (name lib) |
| 26 | + > (parameters param) |
| 27 | + > (inline_tests) |
| 28 | + > (preprocess (pps ppx_inline_test))) |
| 29 | + > EOF |
| 30 | + |
| 31 | +Running the test fails, because we did not specify an implementation for the |
| 32 | +parameter: |
| 33 | + |
| 34 | + $ dune runtest |
| 35 | + File "lib/dune", lines 1-5, characters 0-97: |
| 36 | + 1 | (library |
| 37 | + 2 | (name lib) |
| 38 | + 3 | (parameters param) |
| 39 | + 4 | (inline_tests) |
| 40 | + 5 | (preprocess (pps ppx_inline_test))) |
| 41 | + Error: Parameter "param" is missing. |
| 42 | + -> required by |
| 43 | + _build/default/lib/.lib.inline-tests/.t.eobjs/native/dune__exe__Main.cmx |
| 44 | + -> required by _build/default/lib/.lib.inline-tests/inline-test-runner.exe |
| 45 | + -> required by _build/default/lib/.lib.inline-tests/partitions-best |
| 46 | + -> required by alias lib/runtest-lib in lib/dune:4 |
| 47 | + -> required by alias lib/runtest in lib/dune:1 |
| 48 | + Hint: Pass an argument implementing param to the dependency, or add |
| 49 | + (parameters param) |
| 50 | + [1] |
| 51 | + |
| 52 | +We add an implementation: |
| 53 | + |
| 54 | + $ mkdir impl |
| 55 | + $ echo 'let param = "impl"' > impl/impl.ml |
| 56 | + $ cat > impl/dune <<EOF |
| 57 | + > (library |
| 58 | + > (name impl) |
| 59 | + > (implements param)) |
| 60 | + > EOF |
| 61 | + |
| 62 | +And specify that `(inline_tests)` should use it with `(arguments impl)`: |
| 63 | + |
| 64 | + $ cat > lib/dune <<EOF |
| 65 | + > (library |
| 66 | + > (name lib) |
| 67 | + > (parameters param) |
| 68 | + > (inline_tests (arguments impl)) |
| 69 | + > (preprocess (pps ppx_inline_test))) |
| 70 | + > EOF |
| 71 | + |
| 72 | +It should work: |
| 73 | + |
| 74 | + $ dune runtest |
| 75 | + |
| 76 | +We break the test to confirm that the inline test is running: |
| 77 | + |
| 78 | + $ cat > lib/lib.ml <<EOF |
| 79 | + > let param = "lib(" ^ Param.param ^ ")" |
| 80 | + > let%test _ = Param.param = "not impl" |
| 81 | + > EOF |
| 82 | + |
| 83 | + $ dune runtest |
| 84 | + File "lib/lib.ml", line 2, characters 0-37: <<Param.param = "not impl">> is false. |
| 85 | + |
| 86 | + FAILED 1 / 1 tests |
| 87 | + [1] |
| 88 | + |
| 89 | +Using another implementation: |
| 90 | + |
| 91 | + $ mkdir not_impl |
| 92 | + $ echo 'let param = "not impl"' > not_impl/not_impl.ml |
| 93 | + $ cat > not_impl/dune <<EOF |
| 94 | + > (library |
| 95 | + > (name not_impl) |
| 96 | + > (implements param)) |
| 97 | + > EOF |
| 98 | + |
| 99 | + $ cat > lib/dune <<EOF |
| 100 | + > (library |
| 101 | + > (name lib) |
| 102 | + > (parameters param) |
| 103 | + > (inline_tests (arguments not_impl)) |
| 104 | + > (preprocess (pps ppx_inline_test))) |
| 105 | + > EOF |
| 106 | + |
| 107 | +This now works: |
| 108 | + |
| 109 | + $ dune runtest |
| 110 | + |
| 111 | +Adding another library which has a dependency on the parameterised `lib`: |
| 112 | + |
| 113 | + $ mkdir lib2 |
| 114 | + $ cat > lib2/lib2_util.ml <<EOF |
| 115 | + > let lib_param = Lib.param |
| 116 | + > EOF |
| 117 | + $ cat > lib2/lib2.ml <<EOF |
| 118 | + > let%test _ = Lib2_util.lib_param = "lib(impl)" |
| 119 | + > EOF |
| 120 | + $ cat > lib2/dune <<EOF |
| 121 | + > (library |
| 122 | + > (name lib2) |
| 123 | + > (parameters param) |
| 124 | + > (libraries lib) |
| 125 | + > (inline_tests (arguments impl)) |
| 126 | + > (preprocess (pps ppx_inline_test))) |
| 127 | + > EOF |
| 128 | + |
| 129 | +(Note that the library has two files, which triggers the inline_test |
| 130 | +preprocessor to generate `.pp.ml` files, which influences how the parameterised |
| 131 | +libraries can read the ocamldep outputs since the filenames are not the |
| 132 | +unpreprocessed ones.) |
| 133 | + |
| 134 | +This should also work: |
| 135 | + |
| 136 | + $ dune runtest |
| 137 | + |
| 138 | +Using the wrong implementation should break the test again: |
| 139 | + |
| 140 | + $ cat > lib2/dune <<EOF |
| 141 | + > (library |
| 142 | + > (name lib2) |
| 143 | + > (parameters param) |
| 144 | + > (libraries lib) |
| 145 | + > (inline_tests (arguments not_impl)) |
| 146 | + > (preprocess (pps ppx_inline_test))) |
| 147 | + > EOF |
| 148 | + |
| 149 | + $ dune runtest |
| 150 | + File "lib2/lib2.ml", line 1, characters 0-46: <<Lib2_util.lib_param = "lib(impl)">> is false. |
| 151 | + |
| 152 | + FAILED 1 / 1 tests |
| 153 | + [1] |
0 commit comments