Source file test_notifier.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
open Core
open Async
open Vcaml
open Vcaml_test_helpers
module Notifier = Vcaml.Expert.Notifier

let%expect_test "Simple asynchronous notification" =
  let result =
    let result = Ivar.create () in
    with_client (fun client ->
      let open Deferred.Or_error.Let_syntax in
      let%bind channel = Nvim.get_channel_info client in
      let name = "async_func" in
      Ocaml_from_nvim.register_request_async
        (Connected client)
        ~name
        ~type_:Ocaml_from_nvim.Async.unit
        ~f:(fun ~client:_ -> Deferred.Or_error.return (Ivar.fill_exn result "Called!"));
      let%bind () =
        Notifier.notify
          client
          ~name:(`Viml "rpcnotify")
          ~type_:Notifier.Func.(Int @-> String @-> unit)
          channel.id
          name
      in
      Ivar.read result |> Deferred.ok)
  in
  let%bind result = with_timeout (Time_float.Span.of_int_sec 3) result in
  print_s [%sexp (result : [ `Result of string | `Timeout ])];
  [%expect {| (Result Called!) |}];
  return ()
;;

let%expect_test "Bad asynchronous notification" =
  let result =
    let result = Ivar.create () in
    with_client
      ~on_error:
        (`Call
          (fun error ->
            print_s [%sexp (error : Vcaml_error.t)];
            Ivar.fill_exn result "Received asynchronous failure message"))
      (fun client ->
        let%bind.Deferred.Or_error () =
          Notifier.notify client ~name:(`Viml "bad-function") ~type_:Notifier.Func.unit
        in
        Ivar.read result |> Deferred.ok)
  in
  let%bind result = with_timeout (Time_float.Span.of_int_sec 3) result in
  print_s [%sexp (result : [ `Result of string | `Timeout ])];
  [%expect
    {|
    (Nvim_error_event
     ((error_type Exception)
      (message "Vim:E117: Unknown function: bad-function")))
    (Result "Received asynchronous failure message")
    |}];
  return ()
;;