Skip to content

Commit 8f6166f

Browse files
author
Guillaume "Liam" Petiot
authored
Use ppx-expect to test Contributions.pp (#18)
1 parent 7372fd7 commit 8f6166f

File tree

6 files changed

+98
-48
lines changed

6 files changed

+98
-48
lines changed

Diff for: bin/main.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let show ~from ~user json =
3737
let contribs = Contributions.of_json ~from ~user json in
3838
if Contributions.is_empty contribs then
3939
Fmt.epr "(no activity found since %s)@." from
40-
else Fmt.pr "@[<v>%a@]@." Contributions.pp contribs
40+
else Fmt.pr "%a@." Contributions.pp contribs
4141

4242
let mode = `Normal
4343

Diff for: dune-project

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
(synopsis "Collect activity as markdown")
1919
(depends
2020
(alcotest :with-test)
21+
(ppx_expect :with-test)
2122
astring
2223
curly
2324
(fmt (>= 0.8.7))

Diff for: get-activity-lib.opam

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bug-reports: "https://github.com/tarides/get-activity/issues"
88
depends: [
99
"dune" {>= "2.8"}
1010
"alcotest" {with-test}
11+
"ppx_expect" {with-test}
1112
"astring"
1213
"curly"
1314
"fmt" {>= "0.8.7"}

Diff for: lib/contributions.ml

+90
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,93 @@ let pp f { activity; _ } =
205205
| [] -> Fmt.string f "(no activity)"
206206
| [ (_, items) ] -> pp_items f items
207207
| repos -> Fmt.(list ~sep:(cut ++ cut)) pp_repo f repos
208+
209+
let pp fs t = Fmt.pf fs "@[<v>%a@]" pp t
210+
211+
let%expect_test "Contributions.pp" =
212+
let contributions_example = { username = "me"; activity = Repo_map.empty } in
213+
Format.printf "%a" pp contributions_example;
214+
[%expect {|
215+
(no activity) |}]
216+
217+
let contributions_example =
218+
{
219+
username = "me";
220+
activity =
221+
Repo_map.empty
222+
|> Repo_map.add "gpetiot/js_of_ocaml"
223+
[
224+
{
225+
repo = "gpetiot/js_of_ocaml";
226+
kind = `New_repo;
227+
date = "2024-03-01T10:43:33Z";
228+
url = "https://github.com/gpetiot/js_of_ocaml";
229+
title = "Title1";
230+
body = "";
231+
};
232+
]
233+
|> Repo_map.add "realworldocaml/mdx"
234+
[
235+
{
236+
repo = "realworldocaml/mdx";
237+
kind = `Review "APPROVED";
238+
date = "2024-03-05T11:43:04Z";
239+
url =
240+
"https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244";
241+
title = "Title2";
242+
body = "xxx";
243+
};
244+
{
245+
repo = "realworldocaml/mdx";
246+
kind = `PR;
247+
date = "2024-03-04T17:20:11Z";
248+
url = "https://github.com/realworldocaml/mdx/pull/450";
249+
title = "Title3";
250+
body = "xxx";
251+
};
252+
]
253+
|> Repo_map.add "tarides/okra"
254+
[
255+
{
256+
repo = "tarides/okra";
257+
kind = `Review "APPROVED";
258+
date = "2024-02-28T11:09:41Z";
259+
url =
260+
"https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361";
261+
title = "Title4";
262+
body = "xxx";
263+
};
264+
{
265+
repo = "tarides/okra";
266+
kind = `Issue;
267+
date = "2024-02-27T12:05:04Z";
268+
url = "https://github.com/tarides/okra/issues/165";
269+
title = "Title5";
270+
body = "xxx";
271+
};
272+
];
273+
}
274+
275+
let%expect_test "Contributions.pp" =
276+
Format.printf "%a" pp contributions_example;
277+
[%expect
278+
{|
279+
### gpetiot/js_of_ocaml
280+
281+
Created repository [gpetiot/js_of_ocaml](https://github.com/gpetiot/js_of_ocaml).
282+
283+
### realworldocaml/mdx
284+
285+
APPROVED Title2 [#449](https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244).
286+
xxx
287+
288+
Title3 [#450](https://github.com/realworldocaml/mdx/pull/450).
289+
xxx
290+
291+
### tarides/okra
292+
293+
APPROVED Title4 [#166](https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361).
294+
xxx
295+
296+
Title5 [#165](https://github.com/tarides/okra/issues/165).
297+
xxx |}]

Diff for: lib/dune

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
(library
22
(name get_activity)
33
(public_name get-activity-lib)
4-
(libraries astring curly fmt yojson))
4+
(libraries astring curly fmt yojson)
5+
(inline_tests)
6+
(preprocess
7+
(pps ppx_expect)))

Diff for: test/lib/test_contributions.ml

+1-46
Original file line numberDiff line numberDiff line change
@@ -440,49 +440,4 @@ let test_is_empty =
440440
~expected:false;
441441
]
442442

443-
let test_pp =
444-
let make_test name ~input ~expected =
445-
let name = Printf.sprintf "pp: %s" name in
446-
let test_fun () =
447-
let actual = Format.asprintf "%a" Contributions.pp input in
448-
Alcotest.(check string) name expected actual
449-
in
450-
(name, `Quick, test_fun)
451-
in
452-
[
453-
make_test "empty"
454-
~input:
455-
{ Contributions.username = ""; activity = Contributions.Repo_map.empty }
456-
~expected:"(no activity)";
457-
make_test "not empty"
458-
~input:(contributions_example ~user:Viewer)
459-
~expected:
460-
"### gpetiot/config.ml\n\
461-
Created repository \
462-
[gpetiot/config.ml](https://github.com/gpetiot/config.ml).\n\
463-
### gpetiot/js_of_ocaml\n\
464-
Created repository \
465-
[gpetiot/js_of_ocaml](https://github.com/gpetiot/js_of_ocaml).\n\
466-
### ocaml-ppx/ocamlformat\n\
467-
Represent the expr sequence as a list \
468-
[#2533](https://github.com/ocaml-ppx/ocamlformat/pull/2533). \n\
469-
xxx### realworldocaml/mdx\n\
470-
APPROVED Add upgrade instructions in the changelog for #446 \
471-
[#449](https://github.com/realworldocaml/mdx/pull/449#pullrequestreview-1916654244). \n\
472-
xxx\n\
473-
Add an 'exec' label to execute include OCaml blocks \
474-
[#450](https://github.com/realworldocaml/mdx/pull/450). \n\
475-
xxx### tarides/get-activity\n\
476-
Add the PR/issues comments to the result of okra generate \
477-
[#8](https://github.com/tarides/get-activity/issues/8). \n\
478-
xxx### tarides/okra\n\
479-
APPROVED Make README.md more precise \
480-
[#166](https://github.com/tarides/okra/pull/166#pullrequestreview-1905972361). \n\
481-
xxx\n\
482-
Make the `get-activity` package known to ocaml-ci \
483-
[#165](https://github.com/tarides/okra/issues/165). \n\
484-
xxx";
485-
]
486-
487-
let suite =
488-
("Contributions", test_request @ test_of_json @ test_is_empty @ test_pp)
443+
let suite = ("Contributions", test_request @ test_of_json @ test_is_empty)

0 commit comments

Comments
 (0)