Skip to content

Commit

Permalink
Use ppx-expect test to test Contributions.pp
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Mar 13, 2024
1 parent 7372fd7 commit b5504b1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 48 deletions.
2 changes: 1 addition & 1 deletion bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "@[<v>%a@]@." Contributions.pp contribs
else Fmt.pr "%a@." Contributions.pp contribs

let mode = `Normal

Expand Down
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
(synopsis "Collect activity as markdown")
(depends
(alcotest :with-test)
(ppx-expect :with-test)
astring
curly
(fmt (>= 0.8.7))
Expand Down
1 change: 1 addition & 0 deletions get-activity-lib.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
90 changes: 90 additions & 0 deletions lib/contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "@[<v>%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 |}]
5 changes: 4 additions & 1 deletion lib/dune
Original file line number Diff line number Diff line change
@@ -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_jane)))
47 changes: 1 addition & 46 deletions test/lib/test_contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b5504b1

Please sign in to comment.