File: t_format.ml

package info (click to toggle)
ocaml-containers 3.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,412 kB
  • sloc: ml: 33,221; sh: 122; makefile: 32
file content (158 lines) | stat: -rw-r--r-- 3,611 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
open CCFormat
module T = (val Containers_testlib.make ~__FILE__ ())
include T

let to_string_test s = CCFormat.sprintf_no_color "@[<h>%a@]%!" s ();;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "a b"
  (to_string_test (return "a@ b"))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  ", "
  (to_string_test (return ",@ "))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "and then"
  (to_string_test (return "@{<Red>and then@}@,"))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "a b"
  (to_string_test (return "@[<h>a@ b@]"))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "a\nb\nc"
  (sprintf_no_color "@[<v>%a@]%!" text "a b c")
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "a b\nc"
  (sprintf_no_color "@[<h>%a@]%!" text "a b\nc")
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "(a\n b\n c)"
  (sprintf_no_color "(@[<v>%a@])" string_lines "a\nb\nc")
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "foobar"
  (to_string_test (append (return "foo") (return "bar")))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "bar"
  (to_string_test (append (return "") (return "bar")))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "foo"
  (to_string_test (append (return "foo") (return "")))
;;

eq ~printer:(fun s -> CCFormat.sprintf "%S" s) "" (to_string_test @@ append_l [])
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "foobarbaz"
  (to_string_test @@ append_l (List.map return [ "foo"; "bar"; "baz" ]))
;;

eq
  ~printer:(fun s -> CCFormat.sprintf "%S" s)
  "3141"
  (to_string_test @@ append_l (List.map (const int) [ 3; 14; 1 ]))
;;

t @@ fun () ->
let buf1 = Buffer.create 42 in
let buf2 = Buffer.create 42 in
let f1 = Format.formatter_of_buffer buf1 in
let f2 = Format.formatter_of_buffer buf2 in
let fmt = tee f1 f2 in
Format.fprintf fmt "coucou@.";
assert_equal ~printer:CCFun.id "coucou\n" (Buffer.contents buf1);
assert_equal ~printer:CCFun.id "coucou\n" (Buffer.contents buf2);
true
;;

t @@ fun () ->
set_color_default true;
let s =
  sprintf "what is your %a? %a! No, %a! Ahhhhhhh@."
    (styling [ `FG `White; `Bold ] string)
    "favorite color"
    (styling [ `FG `Blue ] string)
    "blue"
    (styling [ `FG `Red ] string)
    "red"
in
assert_equal ~printer:CCFun.id
  "what is your \027[37;1mfavorite color\027[0m? \027[34mblue\027[0m! No, \
   \027[31mred\027[0m! Ahhhhhhh\n"
  s;
true
;;

t @@ fun () ->
set_color_default true;
let s =
  sprintf
    "what is your @{<White>favorite color@}? @{<blue>blue@}! No, @{<red>red@}! \
     Ahhhhhhh@."
in
assert_equal ~printer:CCFun.id
  "what is your \027[37;1mfavorite color\027[0m? \027[34mblue\027[0m! No, \
   \027[31mred\027[0m! Ahhhhhhh\n"
  s;
true
;;

t @@ fun () -> sprintf "yolo %s %d" "a b" 42 = "yolo a b 42";;
t @@ fun () -> sprintf "%d " 0 = "0 ";;
t @@ fun () -> sprintf_no_color "%d " 0 = "0 ";;

t @@ fun () ->
set_color_default true;
assert_equal "\027[31myolo\027[0m" (sprintf "@{<red>yolo@}");
assert_equal "yolo" (sprintf_no_color "@{<red>yolo@}");
true
;;

eq
  ~printer:CCFormat.(to_string (opt string))
  (Some "hello world")
  (ksprintf ~f:(fun s -> Some s) "hello %a" CCFormat.string "world")
;;

eq ~printer:(fun s -> s) "[1;2;3]" (to_string Dump.(list int) [ 1; 2; 3 ]);;
eq ~printer:(fun s -> s) "Some 1" (to_string Dump.(option int) (Some 1));;

eq
  ~printer:(fun s -> s)
  "[None;Some \"a b\"]"
  (to_string Dump.(list (option string)) [ None; Some "a b" ])
;;

eq
  ~printer:(fun s -> s)
  "[(Ok \"a b c\");(Error \"nope\")]"
  (to_string Dump.(list (result string)) [ Ok "a b c"; Error "nope" ])
;;

eq ANSI_codes.reset "\x1b[0m"