File: test_easy_format.ml

package info (click to toggle)
easy-format 1.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: ml: 1,223; makefile: 15
file content (122 lines) | stat: -rw-r--r-- 2,486 bytes parent folder | download | duplicates (3)
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
open Easy_format

let make_data list_param label_param atom_param =
  let obj_param = ("{", ",", "}", list_param) in
  let array_param = ("[", ",", "]", list_param) in
  let at s = Atom (s, atom_param) in
  let obj =
    List (
      obj_param,
      [
	Label (
	  (at "x:", label_param),
	  at "y"
	);
	Label (
	  (at "y:", label_param),
	  List (obj_param, [Label ((at "z:", label_param), at "aaa")])
	);
	Label (
	(at "a:", label_param),
	  List (
	    array_param,
	    [
	      at "abc";
	      at "\"long long long......................................\
                    ....................................................\"";
	    ]
	  )
	);
	Label (
	  (at "\"a long label ..................\
                   .............................\":",
	   label_param),
	  List (
	    array_param,
	    [
	      at "123";
	      at "456";
	      at "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	    ]
	  )
	)
      ]
    )
  in

  let array =
    List (array_param, [ at "a"; at "b"; at "c"; at "d" ])
  in

  Label (
    (at "abc:", label_param),
    List (
      array_param,
      [
	obj; array; obj;
	at "xyz";
      ]
    )
  )

(* List.init achieves the same but requires ocaml >= 4.06. *)
let list_init n f = Array.to_list (Array.init n f)

(* Test stack overflow *)
let () =
  let data =
    List ( ("[", ",", "]", list),
           list_init 1_000_000 (fun _i -> Atom ("x", atom))
         )
  in

  let (_: string) = Easy_format.Pretty.to_string data in
  ()

let () =
  let x1 = make_data list label atom in
  let x2 =
    make_data
      { list with
	  space_after_opening = false;
	  space_after_separator = false;
	  space_before_closing = false;
	  stick_to_label = false;
	  align_closing = false }
      { label with
	  space_after_label = true }
      atom
  in
  let x3 =
    make_data
      { list with
	  space_after_opening = false;
	  space_before_separator = true;
	  space_after_separator = true;
	  separators_stick_left = false;
	  space_before_closing = false;
	  stick_to_label = true;
	  align_closing = true }
      { label with
	  space_after_label = true }
      atom
  in
  let x4 =
    make_data { list with stick_to_label = false } label atom
  in

  Easy_format.Pretty.to_stdout x1;
  print_newline ();

  Easy_format.Pretty.to_stdout x2;
  print_newline ();

  Easy_format.Pretty.to_stdout x3;
  print_newline ();

  Easy_format.Pretty.to_stdout x4;
  print_newline ();

  Easy_format.Compact.to_stdout x1;
  print_newline ()