From 8f6166fb7204d5119dddc2449760aa654fb86ba8 Mon Sep 17 00:00:00 2001 From: "Guillaume \"Liam\" Petiot" Date: Wed, 13 Mar 2024 19:22:09 +0000 Subject: [PATCH] Use ppx-expect to test Contributions.pp (#18) --- bin/main.ml | 2 +- dune-project | 1 + get-activity-lib.opam | 1 + lib/contributions.ml | 90 ++++++++++++++++++++++++++++++++++ lib/dune | 5 +- test/lib/test_contributions.ml | 47 +----------------- 6 files changed, 98 insertions(+), 48 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 6ee6275..46dd649 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -37,7 +37,7 @@ let show ~from ~user json = let contribs = Contributions.of_json ~from ~user json in if Contributions.is_empty contribs then Fmt.epr "(no activity found since %s)@." from - else Fmt.pr "@[%a@]@." Contributions.pp contribs + else Fmt.pr "%a@." Contributions.pp contribs let mode = `Normal diff --git a/dune-project b/dune-project index 987bc3d..382cddc 100644 --- a/dune-project +++ b/dune-project @@ -18,6 +18,7 @@ (synopsis "Collect activity as markdown") (depends (alcotest :with-test) + (ppx_expect :with-test) astring curly (fmt (>= 0.8.7)) diff --git a/get-activity-lib.opam b/get-activity-lib.opam index 041869c..a9abbcb 100644 --- a/get-activity-lib.opam +++ b/get-activity-lib.opam @@ -8,6 +8,7 @@ bug-reports: "https://github.com/tarides/get-activity/issues" depends: [ "dune" {>= "2.8"} "alcotest" {with-test} + "ppx_expect" {with-test} "astring" "curly" "fmt" {>= "0.8.7"} diff --git a/lib/contributions.ml b/lib/contributions.ml index 8694b70..f041b28 100644 --- a/lib/contributions.ml +++ b/lib/contributions.ml @@ -205,3 +205,93 @@ let pp f { activity; _ } = | [] -> Fmt.string f "(no activity)" | [ (_, items) ] -> pp_items f items | repos -> Fmt.(list ~sep:(cut ++ cut)) pp_repo f repos + +let pp fs t = Fmt.pf fs "@[%a@]" pp t + +let%expect_test "Contributions.pp" = + let contributions_example = { username = "me"; activity = Repo_map.empty } in + Format.printf "%a" pp contributions_example; + [%expect {| + (no activity) |}] + +let contributions_example = + { + username = "me"; + activity = + Repo_map.empty + |> Repo_map.add "gpetiot/js_of_ocaml" + [ + { + repo = "gpetiot/js_of_ocaml"; + kind = `New_repo; + date = "2024-03-01T10:43:33Z"; + url = "https://github.com/gpetiot/js_of_ocaml"; + title = "Title1"; + body = ""; + }; + ] + |> Repo_map.add "realworldocaml/mdx" + [ + { + repo = "realworldocaml/mdx"; + kind = `Review "APPROVED"; + date = "2024-03-05T11:43:04Z"; + url = + "https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244"; + title = "Title2"; + body = "xxx"; + }; + { + repo = "realworldocaml/mdx"; + kind = `PR; + date = "2024-03-04T17:20:11Z"; + url = "https://github.com/realworldocaml/mdx/pull/450"; + title = "Title3"; + body = "xxx"; + }; + ] + |> Repo_map.add "tarides/okra" + [ + { + repo = "tarides/okra"; + kind = `Review "APPROVED"; + date = "2024-02-28T11:09:41Z"; + url = + "https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361"; + title = "Title4"; + body = "xxx"; + }; + { + repo = "tarides/okra"; + kind = `Issue; + date = "2024-02-27T12:05:04Z"; + url = "https://github.com/tarides/okra/issues/165"; + title = "Title5"; + body = "xxx"; + }; + ]; + } + +let%expect_test "Contributions.pp" = + Format.printf "%a" pp contributions_example; + [%expect + {| + ### gpetiot/js_of_ocaml + + Created repository [gpetiot/js_of_ocaml](https://github.com/gpetiot/js_of_ocaml). + + ### realworldocaml/mdx + + APPROVED Title2 [#449](https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244). + xxx + + Title3 [#450](https://github.com/realworldocaml/mdx/pull/450). + xxx + + ### tarides/okra + + APPROVED Title4 [#166](https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361). + xxx + + Title5 [#165](https://github.com/tarides/okra/issues/165). + xxx |}] diff --git a/lib/dune b/lib/dune index f2a7f8e..c7e5189 100644 --- a/lib/dune +++ b/lib/dune @@ -1,4 +1,7 @@ (library (name get_activity) (public_name get-activity-lib) - (libraries astring curly fmt yojson)) + (libraries astring curly fmt yojson) + (inline_tests) + (preprocess + (pps ppx_expect))) diff --git a/test/lib/test_contributions.ml b/test/lib/test_contributions.ml index 565e9c0..044cc6a 100644 --- a/test/lib/test_contributions.ml +++ b/test/lib/test_contributions.ml @@ -440,49 +440,4 @@ let test_is_empty = ~expected:false; ] -let test_pp = - let make_test name ~input ~expected = - let name = Printf.sprintf "pp: %s" name in - let test_fun () = - let actual = Format.asprintf "%a" Contributions.pp input in - Alcotest.(check string) name expected actual - in - (name, `Quick, test_fun) - in - [ - make_test "empty" - ~input: - { Contributions.username = ""; activity = Contributions.Repo_map.empty } - ~expected:"(no activity)"; - make_test "not empty" - ~input:(contributions_example ~user:Viewer) - ~expected: - "### gpetiot/config.ml\n\ - Created repository \ - [gpetiot/config.ml](https://github.com/gpetiot/config.ml).\n\ - ### gpetiot/js_of_ocaml\n\ - Created repository \ - [gpetiot/js_of_ocaml](https://github.com/gpetiot/js_of_ocaml).\n\ - ### ocaml-ppx/ocamlformat\n\ - Represent the expr sequence as a list \ - [#2533](https://github.com/ocaml-ppx/ocamlformat/pull/2533). \n\ - xxx### realworldocaml/mdx\n\ - APPROVED Add upgrade instructions in the changelog for #446 \ - [#449](https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244). \n\ - xxx\n\ - Add an 'exec' label to execute include OCaml blocks \ - [#450](https://github.com/realworldocaml/mdx/pull/450). \n\ - xxx### tarides/get-activity\n\ - Add the PR/issues comments to the result of okra generate \ - [#8](https://github.com/tarides/get-activity/issues/8). \n\ - xxx### tarides/okra\n\ - APPROVED Make README.md more precise \ - [#166](https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361). \n\ - xxx\n\ - Make the `get-activity` package known to ocaml-ci \ - [#165](https://github.com/tarides/okra/issues/165). \n\ - xxx"; - ] - -let suite = - ("Contributions", test_request @ test_of_json @ test_is_empty @ test_pp) +let suite = ("Contributions", test_request @ test_of_json @ test_is_empty)