OCaml client for the Slack Web API
1let decode_ok codec s =
2 match Json.of_string codec s with
3 | Ok v -> v
4 | Error e -> Alcotest.failf "decode error: %s" (Json.Error.to_string e)
5
6let roundtrip codec v =
7 let s = Json.to_string codec v in
8 match Json.of_string codec s with
9 | Ok v' -> v'
10 | Error e ->
11 Alcotest.failf "decode error on roundtrip: %s\njson: %s"
12 (Json.Error.to_string e) s
13
14let test_decode () =
15 let json =
16 {|{"real_name":"Alice Smith","display_name":"alice","email":"alice@example.com","title":"Engineer","team":"T1"}|}
17 in
18 let p = decode_ok Slack.User_profile.json json in
19 Alcotest.(check (option string))
20 "real_name" (Some "Alice Smith")
21 (Slack.User_profile.real_name p);
22 Alcotest.(check (option string))
23 "display_name" (Some "alice")
24 (Slack.User_profile.display_name p);
25 Alcotest.(check (option string))
26 "email" (Some "alice@example.com")
27 (Slack.User_profile.email p);
28 Alcotest.(check (option string))
29 "title" (Some "Engineer")
30 (Slack.User_profile.title p);
31 Alcotest.(check (option string))
32 "team" (Some "T1")
33 (Slack.User_profile.team p)
34
35let test_decode_minimal () =
36 let json = {|{}|} in
37 let p = decode_ok Slack.User_profile.json json in
38 Alcotest.(check (option string))
39 "no real_name" None
40 (Slack.User_profile.real_name p);
41 Alcotest.(check (option string))
42 "no display_name" None
43 (Slack.User_profile.display_name p);
44 Alcotest.(check (option string)) "no email" None (Slack.User_profile.email p)
45
46let test_decode_with_images () =
47 let json =
48 {|{"image_24":"https://a.com/24.jpg","image_512":"https://a.com/512.jpg"}|}
49 in
50 let p = decode_ok Slack.User_profile.json json in
51 Alcotest.(check (option string))
52 "image_24" (Some "https://a.com/24.jpg")
53 (Slack.User_profile.image_24 p);
54 Alcotest.(check (option string))
55 "image_512" (Some "https://a.com/512.jpg")
56 (Slack.User_profile.image_512 p)
57
58let test_roundtrip () =
59 let p =
60 Slack.User_profile.v ~real_name:"Alice" ~display_name:"alice"
61 ~email:"alice@example.com" ()
62 in
63 let p' = roundtrip Slack.User_profile.json p in
64 Alcotest.(check bool) "roundtrip" true (Slack.User_profile.equal p p')
65
66let test_constructor () =
67 let p = Slack.User_profile.v ~first_name:"Alice" ~last_name:"Smith" () in
68 Alcotest.(check (option string))
69 "first_name" (Some "Alice")
70 (Slack.User_profile.first_name p);
71 Alcotest.(check (option string))
72 "last_name" (Some "Smith")
73 (Slack.User_profile.last_name p);
74 Alcotest.(check (option string))
75 "unset email" None
76 (Slack.User_profile.email p)
77
78let test_equal () =
79 let a = Slack.User_profile.v ~real_name:"Alice" () in
80 let b = Slack.User_profile.v ~real_name:"Alice" () in
81 let c = Slack.User_profile.v ~real_name:"Bob" () in
82 Alcotest.(check bool) "equal" true (Slack.User_profile.equal a b);
83 Alcotest.(check bool) "not equal" false (Slack.User_profile.equal a c)
84
85let test_pp () =
86 let p = Slack.User_profile.v ~real_name:"Alice" () in
87 let s = Fmt.str "%a" Slack.User_profile.pp p in
88 Alcotest.(check string) "pp with real_name" "UserProfile(Alice)" s
89
90let test_pp_display_name () =
91 let p = Slack.User_profile.v ~display_name:"alice" () in
92 let s = Fmt.str "%a" Slack.User_profile.pp p in
93 Alcotest.(check string) "pp with display_name" "UserProfile(alice)" s
94
95let test_pp_unknown () =
96 let p = Slack.User_profile.v () in
97 let s = Fmt.str "%a" Slack.User_profile.pp p in
98 Alcotest.(check string) "pp unknown" "UserProfile(Unknown)" s
99
100let suite =
101 ( "user_profile",
102 [
103 Alcotest.test_case "decode" `Quick test_decode;
104 Alcotest.test_case "decode minimal" `Quick test_decode_minimal;
105 Alcotest.test_case "decode with images" `Quick test_decode_with_images;
106 Alcotest.test_case "roundtrip" `Quick test_roundtrip;
107 Alcotest.test_case "constructor" `Quick test_constructor;
108 Alcotest.test_case "equal" `Quick test_equal;
109 Alcotest.test_case "pp" `Quick test_pp;
110 Alcotest.test_case "pp display_name" `Quick test_pp_display_name;
111 Alcotest.test_case "pp unknown" `Quick test_pp_unknown;
112 ] )