Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests and replace exceptions by result types for the requests #10

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
(name get-activity-lib)
(synopsis "Collect activity as markdown")
(depends
(alcotest :with-test)
astring
curly
fmt
Expand Down
1 change: 1 addition & 0 deletions get-activity-lib.opam
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ homepage: "https://github.com/tarides/get-activity"
bug-reports: "https://github.com/tarides/get-activity/issues"
depends: [
"dune" {>= "2.8"}
"alcotest" {with-test}
"astring"
"curly"
"fmt"
Expand Down
75 changes: 75 additions & 0 deletions test/lib/alcotest_ext.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
open Alcotest

module Msg = struct
type 'a t = [ `Msg of 'a ]

let pp f fs (`Msg s : 'a t) = Format.fprintf fs "%a" f s
let eq f (`Msg s1 : 'a t) (`Msg s2 : 'a t) = f s1 s2

let testable t =
let pp = pp (Alcotest.pp t) in
let eq = eq (Alcotest.equal t) in
testable pp eq
end

let msg = Msg.testable
let string_msg = msg string
let or_msg x = result x string_msg

module Yojson = struct
type t = Yojson.Safe.t

let pp = Yojson.Safe.pp
let eq = Yojson.Safe.equal
let testable : t testable = testable pp eq
end

let yojson = Yojson.testable

module Curly = struct
module Meth = struct
type t = Curly.Meth.t

let pp = Curly.Meth.pp

let eq (x : t) (y : t) =
let x = Format.asprintf "%a" Curly.Meth.pp x in
let y = Format.asprintf "%a" Curly.Meth.pp y in
String.equal x y
end

module Header = struct
type t = Curly.Header.t

let pp = Curly.Header.pp

let eq (x : t) (y : t) =
let x = Format.asprintf "%a" Curly.Header.pp x in
let y = Format.asprintf "%a" Curly.Header.pp y in
String.equal x y
end
end

module Request = struct
type t = Get_activity.Graphql.request

let pp fs (x : t) =
Format.fprintf fs
"@[<hv 2>{@;\
meth = %a;@;\
url = %S@;\
headers =@ %a@;\
body =@ @[<hv 0>%a@];@]@;\
}"
Curly.Meth.pp x.meth x.url Curly.Header.pp x.headers Yojson.pp x.body

let eq (x : t) (y : t) =
Curly.Meth.eq x.meth y.meth
&& String.equal x.url y.url
&& Curly.Header.eq x.headers y.headers
&& Yojson.eq x.body y.body

let testable = Alcotest.testable pp eq
end

let request = Request.testable
8 changes: 8 additions & 0 deletions test/lib/alcotest_ext.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
val msg : 'a Alcotest.testable -> [ `Msg of 'a ] Alcotest.testable
val string_msg : [ `Msg of string ] Alcotest.testable

val or_msg :
'a Alcotest.testable -> ('a, [ `Msg of string ]) result Alcotest.testable

val yojson : Yojson.Safe.t Alcotest.testable
val request : Get_activity.Graphql.request Alcotest.testable
4 changes: 4 additions & 0 deletions test/lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(test
(name main)
(package get-activity-lib)
(libraries get-activity-lib alcotest))
8 changes: 8 additions & 0 deletions test/lib/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let () =
Alcotest.run "get-activity-lib"
[
Test_token.suite;
Test_period.suite;
Test_graphql.suite;
Test_contributions.suite;
]
Loading
Loading