File: test_lwt_fmt.ml

package info (click to toggle)
lwt 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: ml: 22,030; ansic: 7,167; makefile: 92; python: 62
file content (62 lines) | stat: -rw-r--r-- 2,064 bytes parent folder | download | duplicates (4)
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
61
62
(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)



open Test
open Lwt.Infix

let testchan () =
  let b = Buffer.create 6 in
  let f buf ofs len =
    let bytes = Bytes.create len in
    Lwt_bytes.blit_to_bytes buf ofs bytes 0 len;
    Buffer.add_bytes b bytes;
    Lwt.return len
  in
  let oc = Lwt_io.make ~mode:Output f in
  let fmt = Lwt_fmt.of_channel oc in
  fmt, (fun () -> Buffer.contents b)

let suite = suite "lwt_fmt" [
    test "flushing" (fun () ->
        let fmt, f = testchan () in
        Lwt_fmt.fprintf fmt "%s%i%s%!" "bla" 3 "blo" >>= fun () ->
        Lwt.return (f () = {|bla3blo|})
      );
    test "with combinator" (fun () ->
        let fmt, f = testchan () in
        Lwt_fmt.fprintf fmt "%a%!" Format.pp_print_int 3 >>= fun () ->
        Lwt.return (f () = {|3|})
      );
    test "box" (fun () ->
        let fmt, f = testchan () in
        Lwt_fmt.fprintf fmt "@[<v2>%i@,%i@]%!" 1 2 >>= fun () ->
        Lwt.return (f () = "1\n  2")
      );
    test "boxsplit" (fun () ->
        let fmt, f = testchan () in
        Lwt_fmt.fprintf fmt "@[<v2>%i" 1 >>= fun () ->
        Lwt_fmt.fprintf fmt "@,%i@]" 2 >>= fun () ->
        Lwt_fmt.flush fmt >>= fun () ->
        Lwt.return (f () = "1\n  2")
      );
    test "box close with flush" (fun () ->
        let fmt, f = testchan () in
        Lwt_fmt.fprintf fmt "@[<v2>%i" 1 >>= fun () ->
        Lwt_fmt.fprintf fmt "@,%i" 2 >>= fun () ->
        Lwt_fmt.flush fmt >>= fun () ->
        Lwt.return (f () = "1\n  2")
      );

    test "stream"  (fun () ->
        let stream, fmt = Lwt_fmt.make_stream () in
        Lwt_fmt.fprintf fmt "@[<v2>%i@,%i@]%!" 1 2 >>= fun () ->
        Lwt.return (Lwt_stream.get_available stream = [
            String ("1", 0, 1);
            String ("\n", 0, 1);
            String  ("                                                                                ", 0, 2);
            String ("2", 0, 1);
            Flush])
      );
  ]