Skip to content

Commit 7ec4b50

Browse files
authored
Update letop-punning tests, add note to CONTRIBUTING (#2748)
* Fix redundancy in letop-punning tests * Notes on adding a test in CONTRIBUTING * CHANGES entry * Edits per @EmileTrotignon * Typo repair * Edits per @Julow
1 parent 1609f04 commit 7ec4b50

21 files changed

+127
-69
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ profile. This started with version 0.26.0.
3636
end
3737
```
3838

39+
### Internal
40+
41+
- Added information on writing tests to `CONTRIBUTING.md` (#2838, @WardBrian)
3942

4043
### Changed
4144

CONTRIBUTING.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,57 @@ We heartily welcome pull requests. To ensure an effective contribution, please a
1919
1. For significant, invasive, or output-affecting changes, consider opening an issue for discussion before committing substantial time and effort.
2020
2. If you're new to the project, starting with the [Good first issues](https://github.com/ocaml-ppx/ocamlformat/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3A%22Good-first-issue+%3Agreen_heart%3A%22) can be beneficial.
2121
3. Fork the repository and create your branch from `main`.
22-
4. If you've added code that should be tested, supplement it with tests located in the `test/` directory.
23-
5. Ensure that the test suite passes (see [Running the tests](#running-the-tests) for instructions).
22+
4. Ensure that the test suite passes (see [Running the tests](#running-the-tests) for instructions). If there are changes in the tests,
23+
verify that they are expected. If no existing test output changed, you likely need to add new tests (see [Adding a test](#adding-a-test)).
24+
25+
### Adding a Test
26+
27+
#### Golden Tests
28+
29+
The majority of tests for `ocamlformat` are so called "golden" or "expect" tests. These
30+
tests all consist of running `ocamlformat` on `.ml` files and comparing the
31+
output to expected output stored in reference files. The `dune` configuration
32+
for this is automatically generated for you based on the files present in these
33+
directories the first time `dune runtest` is executed.
34+
35+
These are separated into `test/failing` and `test/passing` directories.
36+
37+
To add a test showing currently incorrect behavior, add a `.ml` file to
38+
`test/failing/tests`. If command line arguments are needed, create a
39+
corresponding `.ml.opts` file with the same name. The output of these tests
40+
will be stored in `.ml.broken-ref` files.
41+
42+
To add a test showing correct behavior, add a `.ml` file to `test/passing/tests`
43+
or update an existing file there. These tests are similar to the failing tests,
44+
including the use of the `.ml.opts` files for command line arguments, however the
45+
same file will be tested against all of the built-in configurations (e.g.
46+
`default`, `janestreet`, etc.), with outputs appearing in sub folders named
47+
`refs.<configuration>`.
48+
49+
In both cases, if multiple sets of options are desired, you can create multiple
50+
`.ml.opts` files with names provided after a `-`, for example `mytest.ml` can have
51+
`mytest-foo.ml.opts` and `mytest-bar.ml.opts`. This will create multiple test targets.
52+
53+
The first time you [run the tests](#running-the-tests), the `dune.inc` file will
54+
be updated. Once you have promoted this, subsequent runs will show you any
55+
differences between the expected and actual outputs, which can be accepted with
56+
`dune promote`.
57+
58+
#### cram-Style Tests
59+
60+
The remaining tests are not designed to test the formatting output of
61+
`ocamlformat`, but rather the command line interface behavior, such as error
62+
messages and help text.
63+
64+
These are found in `test/cli` and are written using
65+
[cram testing](https://dune.readthedocs.io/en/latest/reference/cram.html).
66+
Each test is a `.t` file that contains shell commands and their expected
67+
outputs. To add a new test, create a new `.t` file, or add new commands (lines
68+
beginning with ` $`) to an existing file.
69+
70+
#### Unit Tests
71+
72+
Unit tests are located in the `test/unit` directory and are written using [Alcotest](https://github.com/mirage/alcotest).
2473

2574
### Running the Tests
2675

test/failing/dune.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@
181181
(package ocamlformat)
182182
(action (diff tests/js_test.ml.broken-ref js_test.ml.output)))
183183

184+
(rule
185+
(deps tests/.ocamlformat )
186+
(package ocamlformat)
187+
(action
188+
(with-outputs-to letop_broken.ml.output
189+
(with-accepted-exit-codes 1
190+
(run %{bin:ocamlformat} --ocaml-version=4.14 --letop-punning=always %{dep:tests/letop_broken.ml})))))
191+
192+
(rule
193+
(alias runtest)
194+
(package ocamlformat)
195+
(action (diff tests/letop_broken.ml.broken-ref letop_broken.ml.output)))
196+
184197
(rule
185198
(deps tests/.ocamlformat )
186199
(package ocamlformat)

test/failing/tests/letop_broken.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let ( let* ) x f = f x
2+
3+
let ( and* ) a b = (a, b)
4+
5+
let x = 1
6+
7+
and y = 2
8+
9+
and z = 3
10+
11+
12+
13+
let r = let* (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
14+
and* (* 5 *) y (* 6 *) in (x, y)
15+
16+
let s = let%foo (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
17+
and (* 5 *) y (* 6 *) in (x, y)
18+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ocamlformat: Cannot process "tests/letop_broken.ml".
2+
Please report this bug at https://github.com/ocaml-ppx/ocamlformat/issues.
3+
BUG: comment changed.
4+
File "tests/letop_broken.ml", line 13, characters 33-40:
5+
Error: comment (* 3 *) dropped.
6+
BUG: comment changed.
7+
File "tests/letop_broken.ml", line 13, characters 43-50:
8+
Error: comment (* 4 *) dropped.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--ocaml-version=4.14 --letop-punning=always

test/passing/gen/dune.inc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,55 +3708,55 @@
37083708
(deps .ocamlformat)
37093709
(package ocamlformat)
37103710
(action
3711-
(with-stdout-to let_punning.ml.stdout
3712-
(with-stderr-to let_punning.ml.stderr
3713-
(run %{bin:ocamlformat} --name let_punning.ml --margin-check %{dep:../tests/let_punning.ml})))))
3711+
(with-stdout-to let_punning-denied.ml.stdout
3712+
(with-stderr-to let_punning-denied.ml.stderr
3713+
(run %{bin:ocamlformat} --name let_punning-denied.ml --margin-check --letop-punning=never --ocaml-version=4.14.0 %{dep:../tests/let_punning.ml})))))
37143714

37153715
(rule
37163716
(alias runtest)
37173717
(package ocamlformat)
3718-
(action (diff let_punning.ml.ref let_punning.ml.stdout)))
3718+
(action (diff let_punning-denied.ml.ref let_punning-denied.ml.stdout)))
37193719

37203720
(rule
37213721
(alias runtest)
37223722
(package ocamlformat)
3723-
(action (diff let_punning.ml.err let_punning.ml.stderr)))
3723+
(action (diff let_punning-denied.ml.err let_punning-denied.ml.stderr)))
37243724

37253725
(rule
37263726
(deps .ocamlformat)
37273727
(package ocamlformat)
37283728
(action
3729-
(with-stdout-to let_punning_denied.ml.stdout
3730-
(with-stderr-to let_punning_denied.ml.stderr
3731-
(run %{bin:ocamlformat} --name let_punning_denied.ml --margin-check --letop-punning=never --ocaml-version=4.14.0 %{dep:../tests/let_punning_denied.ml})))))
3729+
(with-stdout-to let_punning-preferred.ml.stdout
3730+
(with-stderr-to let_punning-preferred.ml.stderr
3731+
(run %{bin:ocamlformat} --name let_punning-preferred.ml --margin-check --letop-punning=always --ocaml-version=4.14.0 %{dep:../tests/let_punning.ml})))))
37323732

37333733
(rule
37343734
(alias runtest)
37353735
(package ocamlformat)
3736-
(action (diff let_punning_denied.ml.ref let_punning_denied.ml.stdout)))
3736+
(action (diff let_punning-preferred.ml.ref let_punning-preferred.ml.stdout)))
37373737

37383738
(rule
37393739
(alias runtest)
37403740
(package ocamlformat)
3741-
(action (diff let_punning_denied.ml.err let_punning_denied.ml.stderr)))
3741+
(action (diff let_punning-preferred.ml.err let_punning-preferred.ml.stderr)))
37423742

37433743
(rule
37443744
(deps .ocamlformat)
37453745
(package ocamlformat)
37463746
(action
3747-
(with-stdout-to let_punning_preferred.ml.stdout
3748-
(with-stderr-to let_punning_preferred.ml.stderr
3749-
(run %{bin:ocamlformat} --name let_punning_preferred.ml --margin-check --letop-punning=always --ocaml-version=4.14.0 %{dep:../tests/let_punning_preferred.ml})))))
3747+
(with-stdout-to let_punning.ml.stdout
3748+
(with-stderr-to let_punning.ml.stderr
3749+
(run %{bin:ocamlformat} --name let_punning.ml --margin-check %{dep:../tests/let_punning.ml})))))
37503750

37513751
(rule
37523752
(alias runtest)
37533753
(package ocamlformat)
3754-
(action (diff let_punning_preferred.ml.ref let_punning_preferred.ml.stdout)))
3754+
(action (diff let_punning.ml.ref let_punning.ml.stdout)))
37553755

37563756
(rule
37573757
(alias runtest)
37583758
(package ocamlformat)
3759-
(action (diff let_punning_preferred.ml.err let_punning_preferred.ml.stderr)))
3759+
(action (diff let_punning.ml.err let_punning.ml.stderr)))
37603760

37613761
(rule
37623762
(deps .ocamlformat)

test/passing/refs.ahrefs/let_punning_denied.ml.ref renamed to test/passing/refs.ahrefs/let_punning-denied.ml.ref

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let p =
1515
x, y, z
1616

1717
let q =
18-
let%foo x = x
18+
let%foo x
1919
and y = y
20-
and z = z in
20+
and z in
2121
x, y, z

test/passing/refs.ahrefs/let_punning_preferred.ml.ref renamed to test/passing/refs.ahrefs/let_punning-preferred.ml.ref

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let p =
1515
x, y, z
1616

1717
let q =
18-
let%foo x = x
18+
let%foo x
1919
and y = y
20-
and z = z in
20+
and z in
2121
x, y, z

test/passing/refs.ahrefs/let_punning.ml.ref

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ and y = 2
99
and z = 3
1010

1111
let p =
12-
let* x
12+
let* x = x
1313
and* y
14-
and* z in
14+
and* z = z in
1515
x, y, z
1616

1717
let q =
18-
let%foo x = x
18+
let%foo x
1919
and y = y
20-
and z = z in
20+
and z in
2121
x, y, z

0 commit comments

Comments
 (0)