package dedent

  1. Overview
  2. Docs

Source file test_dedent.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
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
open! Base
open! Stdio
open! Import

(* We make spaces and newlines visible to make the behavior clearer, and so that
   changes in behavior cause diffs that need to be reviewed. *)
let make_whitespace_visible : string -> string =
  let module Search_pattern = String.Search_pattern in
  let replacements : (string -> string) list =
    [ "\n", "\\n"; " ", "_" ]
    |> List.map ~f:(fun (from, with_) ->
      let search_pattern = Search_pattern.create from in
      fun in_ -> Search_pattern.replace_all search_pattern ~in_ ~with_)
  in
  fun input -> List.fold replacements ~init:input ~f:(fun ac f -> f ac)
;;

let test input =
  let output = Dedent.string input in
  print_endline (output |> make_whitespace_visible)
;;

let%expect_test "single-line empty strings are empty" =
  test {||};
  [%expect {| |}];
  test {| |};
  [%expect {| |}];
  test {|  |};
  [%expect {| |}]
;;

let%expect_test "single-line strings ignore leading and trailing whitespace" =
  test {|a|};
  [%expect {| a |}];
  test {| a|};
  [%expect {| a |}];
  test {|a |};
  [%expect {| a |}];
  test {| a |};
  [%expect {| a |}];
  test {|  a  |};
  [%expect {| a |}]
;;

let%expect_test "multi-line empty strings drop the lines with the delimiters" =
  test {|
|};
  [%expect {| |}];
  test {|

|};
  [%expect {| |}];
  test {|


|};
  [%expect {| \n |}]
;;

let%expect_test "multi-line strings drops lines with delimiters" =
  test {|
a
|};
  [%expect {| a |}]
;;

let%expect_test "multi-line strings support leading and trailing blank lines" =
  test {|

a
|};
  [%expect {| \na |}];
  test {|
a

|};
  [%expect {| a\n |}];
  test {|

a

|};
  [%expect {| \na\n |}]
;;

let%expect_test "closing delimiter can be in any column" =
  test {|a
|};
  [%expect {| a |}];
  test {|a
 |};
  [%expect {| a |}];
  test {|a
  |};
  [%expect {| a |}]
;;

let%expect_test "strip trailing whitespace" =
  test "a \nb ";
  [%expect {| a\nb |}];
  test "a  \nb  ";
  [%expect {| a\nb |}]
;;

let%expect_test "drop min indentation" =
  test {|
a
 b
|};
  [%expect {| a\n_b |}];
  test {|
 a
  b
|};
  [%expect {| a\n_b |}];
  test {|
  a
   b
|};
  [%expect {| a\n_b |}];
  test {|
  a
  b
    |};
  [%expect {| a\nb |}];
  test {|
  a
 b
    |};
  [%expect {| _a\nb |}];
  test {|
  a
b
    |};
  [%expect {| __a\nb |}];
  (* This was added so [dedent] can be used to clean up doc comments, where it is not
     typical to start with a blank line. We could have instead used location information
     on the doc comment to figure out the first line's indentation, but we didn't want to
     build that behavior into the ppx. This lets us mostly achieve the same effect by
     inspecting the string contents alone. *)
  test {| a
          b
|};
  [%expect {| a\nb |}];
  (* Bad behavior, but also uncommon. Let's tell people to use line prefixes for that. *)
  test {|    deliberately indented more
         normal indentation
|};
  [%expect {| deliberately_indented_more\nnormal_indentation |}];
  (* Here's how you get the deliberate indentation of the first line. *)
  test {|>      deliberately indented more
         > normal indentation
|};
  [%expect {| _____deliberately_indented_more\nnormal_indentation |}]
;;

let%expect_test "drop line prefix" =
  test {| > a |};
  [%expect {| a |}];
  test {|
> a
|};
  [%expect {| a |}];
  test {|
> a
> b
|};
  [%expect {| a\nb |}]
;;

let%expect_test "line prefix must be followed by a space" =
  test {| >a |};
  [%expect {| >a |}]
;;

let%expect_test "drop line prefix after dropping indentation" =
  test {|
 > a
 >  b
|};
  [%expect {| a\n_b |}]
;;

let%expect_test "only drop line prefix if it is present on all lines" =
  test {|
> a
b
|};
  [%expect {| >_a\nb |}];
  test {|
> a
 > b
|};
  [%expect {| >_a\n_>_b |}]
;;
OCaml

Innovation. Community. Security.