File: tests.ml

package info (click to toggle)
ocaml-pp 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: ml: 595; makefile: 31
file content (333 lines) | stat: -rw-r--r-- 8,270 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
open StdLabels
open Pp.O

let print pp = Format.printf "%a@." Pp.to_fmt pp
let many n pp = Array.make n pp |> Array.to_list |> Pp.concat ~sep:Pp.space
let xs n = many n (Pp.char 'x')
let ys n = many n (Pp.char 'y')

let%expect_test _ =
  let hello_xs n = Pp.text "Hello" ++ Pp.space ++ xs n in
  print (Pp.box ~indent:2 (hello_xs 200));
  [%expect
    {|
Hello x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x
|}];
  print (Pp.hbox (hello_xs 50));
  [%expect
    {|
Hello x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
|}];
  print (Pp.vbox ~indent:2 (hello_xs 5));
  [%expect {|
Hello
  x
  x
  x
  x
  x
|}];
  print (Pp.hvbox ~indent:2 (hello_xs 5));
  [%expect {|
Hello x x x x x
|}];
  print (Pp.hvbox ~indent:2 (hello_xs 50));
  [%expect
    {|
Hello
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
  x
|}];
  print (Pp.hovbox ~indent:2 (hello_xs 200));
  [%expect
    {|
Hello x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x
|}]

let%expect_test "verbatimf" =
  print (Pp.verbatimf "ident%d" 42);
  [%expect {| ident42 |}]

(* Difference between box and hovbox *)
let%expect_test _ =
  let pp f = f (xs 50 ++ Pp.break ~nspaces:2 ~shift:(-1) ++ xs 10) in
  print (pp (Pp.box ~indent:2));
  [%expect
    {|
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x
 x x x x x x x x x x
|}];
  print (pp (Pp.hovbox ~indent:2));
  [%expect
    {|
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x  x x x x x x x x x x
|}]

let enum_x_and_y = Pp.enumerate [ xs; ys ] ~f:(fun f -> f 50)

let%expect_test _ =
  print enum_x_and_y;
  [%expect
    {|
- x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
  x x x x x x x x x x x x
- y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y
  y y y y y y y y y y y y
|}]

let%expect_test _ =
  print
    (Pp.enumerate
       [ Pp.enumerate [ "abc"; "def" ] ~f:Pp.text; enum_x_and_y ]
       ~f:(fun x -> x));
  [%expect
    {|
- - abc
  - def
- - x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
    x x x x x x x x x x x x x
  - y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y
    y y y y y y y y y y y y y
|}]

let%expect_test _ =
  print (Pp.verbatim "....." ++ Pp.box ~indent:2 (xs 50));
  [%expect
    {|
    .....x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
           x x x x x x x x x x x x x x |}]

let error_example_1 =
  Pp.vbox
    (Pp.box (Pp.text "Error: something went wrong!")
    ++ Pp.cut
    ++ Pp.box (Pp.text "Here are a few things you can do:")
    ++ Pp.cut
    ++ Pp.enumerate
         ~f:(fun x -> x)
         [ Pp.text
             "read the documentation, double check the way you are using this \
              software to make sure you are not doing something wrong, and \
              hopefully fix the problem on your side and move on"
         ; Pp.text
             "strace furiously the program to try and understand why exactly \
              it is trying to do what it is doing"
         ; Pp.text "report an issue upstream"
         ; Pp.text "if all else fails"
           ++ Pp.cut
           ++ Pp.enumerate ~f:Pp.text
                [ "scream loudly at your computer"
                ; "take a break from your keyboard"
                ; "clear your head and try again"
                ]
         ])

let%expect_test _ =
  print error_example_1;
  [%expect
    {|
    Error: something went wrong!
    Here are a few things you can do:
    - read the documentation, double check the way you are using this software to
      make sure you are not doing something wrong, and hopefully fix the problem
      on your side and move on
    - strace furiously the program to try and understand why exactly it is trying
      to do what it is doing
    - report an issue upstream
    - if all else fails
      - scream loudly at your computer
      - take a break from your keyboard
      - clear your head and try again |}]

(* Wrap the formatted lines *)
let%expect_test _ =
  print
    (Pp.hovbox ~indent:2
       (Array.make 50 (Pp.char 'x')
       |> Array.to_list
       |> Pp.concat
            ~sep:(Pp.custom_break ~fits:("", 2, "") ~breaks:(" \\", -1, ""))));
  [%expect
    {|
x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x \
 x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x
|}]

let%expect_test "comparison" =
  let x = error_example_1
  and y = Pp.hovbox ~indent:2 (xs 200) in
  let print x = Printf.printf "comparison result: %d\n" x in
  print (Pp.compare (fun _ _ -> 0) x y);
  print (Pp.compare (fun _ _ -> 0) x x);
  print (Pp.compare (fun _ _ -> 0) y x);
  [%expect
    {|
    comparison result: -1
    comparison result: 0
    comparison result: 1 |}]

(* The differnces between [Pp.paragraph], [Pp.text], [Pp.verbatim] and box +
   [Pp.text] when inside a [Pp.vbox]. *)
let%expect_test "paragraph" =
  let lorem =
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum \
     euismod, nisl eget aliquam ultricies."
  in
  let seperator text _ =
    Pp.vbox
    @@ Pp.seq Pp.space
         (Pp.hbox
         @@ Pp.textf "-< %s >-%s" text
              (String.init (20 - String.length text) ~f:(fun _ -> '-')))
  in
  print @@ Pp.vbox
  @@ Pp.concat_map ~sep:Pp.space
       ~f:(fun f -> f lorem)
       [ seperator "verbatim"
       ; Pp.verbatim
       ; seperator "text"
       ; Pp.text
       ; seperator "paragraph"
       ; Pp.paragraph
       ; seperator "hovbox + text"
       ; (fun x -> Pp.hovbox (Pp.text x))
       ; seperator "hvbox + text"
       ; (fun x -> Pp.hvbox (Pp.text x))
       ; seperator "hbox + text"
       ; (fun x -> Pp.hbox (Pp.text x))
       ; seperator "vbox + text"
       ; (fun x -> Pp.vbox (Pp.text x))
       ; seperator "box + text"
       ; (fun x -> Pp.box (Pp.text x))
       ];
  [%expect
    {|
    -< verbatim >-------------
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod, nisl eget aliquam ultricies.

    -< text >-----------------
    Lorem
    ipsum
    dolor
    sit
    amet,
    consectetur
    adipiscing
    elit.
    Vestibulum
    euismod,
    nisl
    eget
    aliquam
    ultricies.

    -< paragraph >------------
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod,
    nisl eget aliquam ultricies.

    -< hovbox + text >--------
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod,
    nisl eget aliquam ultricies.

    -< hvbox + text >---------
    Lorem
    ipsum
    dolor
    sit
    amet,
    consectetur
    adipiscing
    elit.
    Vestibulum
    euismod,
    nisl
    eget
    aliquam
    ultricies.

    -< hbox + text >----------
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod, nisl eget aliquam ultricies.

    -< vbox + text >----------
    Lorem
    ipsum
    dolor
    sit
    amet,
    consectetur
    adipiscing
    elit.
    Vestibulum
    euismod,
    nisl
    eget
    aliquam
    ultricies.

    -< box + text >-----------
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod,
    nisl eget aliquam ultricies. |}]

let%expect_test "paragraphf" =
  print (Pp.paragraphf "Hello World%s" "!");
  [%expect {| Hello World! |}]