{"id":17994744,"url":"https://github.com/j14159/ppx_test_match","last_synced_at":"2025-04-04T05:24:38.270Z","repository":{"id":145428875,"uuid":"244045524","full_name":"j14159/ppx_test_match","owner":"j14159","description":"PPX rewriter to test pattern matches.","archived":false,"fork":false,"pushed_at":"2020-03-02T02:56:16.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-09T16:43:05.730Z","etag":null,"topics":["ocaml","ppx-rewriter","testing"],"latest_commit_sha":null,"homepage":null,"language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/j14159.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"code_of_conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-29T21:40:11.000Z","updated_at":"2020-03-02T02:56:18.000Z","dependencies_parsed_at":"2023-09-18T12:01:14.510Z","dependency_job_id":null,"html_url":"https://github.com/j14159/ppx_test_match","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j14159%2Fppx_test_match","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j14159%2Fppx_test_match/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j14159%2Fppx_test_match/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j14159%2Fppx_test_match/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/j14159","download_url":"https://codeload.github.com/j14159/ppx_test_match/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247125164,"owners_count":20887639,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ocaml","ppx-rewriter","testing"],"created_at":"2024-10-29T20:16:17.058Z","updated_at":"2025-04-04T05:24:38.251Z","avatar_url":"https://github.com/j14159.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"ppx\\_test\\_match\n=====\nA PPX rewriter to allow simpler testing of pattern matches rather than full equality.  Roughly modelled on [EUnit's](http://erlang.org/doc/apps/eunit/chapter.html) `?assertMatch` macro.\n\n**Warning:** this is still very early and incomplete.\n\nPlease note that this project is released with a Contributor Code of Conduct, version 1.4. By participating in this project you agree to abide by its terms.  Please see `code_of_conduct.md` in this repository for details.\n\n# Usage\nAdd this preprocessing step to your `dune` file's library, executable, or test:\n\n```\n(preprocess (pps ppx_test_match?))\n```\n\nSee `dune/test` for a more complete example or [the Dune documentation](https://dune.readthedocs.io/en/stable/concepts.html#preprocessing-spec).\n\nThis is still too early to be fully usable but the following are already possible:\n\n```ocaml\nlet some_ounit_test _ =\n  let example_tuple = (2, 3) in\n  (* This will pass:  *)\n  [%test_match? (x, y) when y \u003e x] example_tuple;\n  (* This will fail:  *)\n  [%test_match? (x, y) when x \u003e y] example_tuple;\n  (* This will pass:  *)\n  [%test_match? (2, _)] example_tuple\n\ntype some_rec = { x : string; y : float }\n\nlet some_other_test_with_records =\n  let example_rec = { x = \"hello\"; y = 3.14159 } in\n  (* This will pass:  *)\n  [%test_match? { x = \"hello\"; _ }] example_rec\n\ntype a_variant = A of int\n\nlet test_with_variant _ =\n  (* Will pass:  *)\n  [%test_match? (A a) when a \u003e 4] (A 5)\n```\n\nThe PPX rewriter will also rewrite duplicated variables as an equality check, for example:\n\n```ocaml\nlet some_other_ounit_test _ =\n  let t1 = (1.3, 1.3) in\n  let t2 = (1.0, 1.01) in\n  (* This will pass:  *)\n  [%test_match? (x, x)] t1;\n  (* This will fail:  *)\n  [%test_match? (x, x)] t2\n\ntype some_other_rec = { a : int; b : string; c : int }\n\nlet another_test _ =\n  (* This will pass:  *)\n  [%test_match? { a; c = a; _ }] { a = 1; b = \"abc\"; c = 1 }\n\ntype a_variant = A of int\n\nlet test_eq_with_variant _ =\n  (* Will pass:  *)\n  [%test_match? (a, A a)] (2, A 2)\n  (* Will fail:  *)\n  [%test_match? (a, A a) when a \u003c 5] (6, A 6)\n```\n\nOnly tuples, records, and variants support this rewriting at the moment.\n\n## Printer Argument\nYou can optionally supply a `printer` argument to `test_match` that will be used to format a non-matching expression on failure.  An example using [ppx_deriving](https://github.com/ocaml-ppx/ppx_deriving) to generate the printer:\n\n```ocaml\nlet test_with_printer _ =\n  (* This will fail with the following message in a `Failure` exception:\n\n       Failed to match (x, x) with value (1, 2)\n   *)\n  [%test_match? (x, x)] (1, 2) ~printer:[derive.show: (int * int)]\n```\n\n# Rewriting Details\nWhen `test_match?` sees something like `(x, x)`, it will synthesize the following replacement:\n\n```ocaml\n(x, _syn_0x) when x = _syn_0x\n```\n\nA more complex example, e.g. `((x, y), x) when y \u003e x)` will be rewritten as follows:\n\n```ocaml\n((x, y), _syn_0x) when y \u003e x \u0026\u0026 x = _syn_0x\n```\n\nMore occurrences of the same variable are sequentially named, e.g. `(a, a, a, b)` will be rewritten to:\n\n```ocaml\n(a, _syn_0a, _syn_1a, b)\n```\n\n# Licensing\nppx\\_test\\_match is released under the terms of the MIT license.  Please see `LICENSE.md` for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj14159%2Fppx_test_match","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fj14159%2Fppx_test_match","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj14159%2Fppx_test_match/lists"}