forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format: Add functions for printing conditionally on line breaks
- Loading branch information
Showing
4 changed files
with
257 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
let set_margin n = | ||
Format.set_margin n; | ||
Format.set_max_indent (n - 1) | ||
|
||
open Format;; | ||
|
||
let test_print_string_if_newline m b = | ||
set_margin m; | ||
open_hovbox 2; | ||
print_string "before"; | ||
print_space (); | ||
if b then ( | ||
print_if_newline (); | ||
print_string "if newline" | ||
) else ( | ||
print_string_if_newline "if newline" | ||
); | ||
print_space (); | ||
print_string "after"; | ||
open_hvbox 2; | ||
print_space (); | ||
print_string "later"; | ||
close_box (); | ||
close_box (); | ||
print_newline () | ||
;; | ||
|
||
let print_if_newline_vs_print_string_if_newline m = | ||
print_string "margin = "; | ||
print_int m; | ||
print_newline (); | ||
print_endline "using print_if_newline:"; | ||
test_print_string_if_newline m true; | ||
print_endline "using print_string_if_newline:"; | ||
test_print_string_if_newline m false; | ||
print_newline () | ||
;; | ||
|
||
print_if_newline_vs_print_string_if_newline 17;; | ||
print_if_newline_vs_print_string_if_newline 18;; | ||
print_if_newline_vs_print_string_if_newline 20;; | ||
print_if_newline_vs_print_string_if_newline 30;; | ||
|
||
let test_print_break_or_string_if_newline m = | ||
print_string "margin = "; | ||
print_int m; | ||
print_newline (); | ||
set_margin m; | ||
open_hvbox 2; | ||
print_string "before"; | ||
print_space (); | ||
print_break_or_string_if_newline 1 0 "broke "; | ||
print_string "after"; | ||
print_space (); | ||
print_string "final"; | ||
close_box (); | ||
print_newline () | ||
;; | ||
|
||
test_print_break_or_string_if_newline 19;; | ||
test_print_break_or_string_if_newline 20;; | ||
|
||
print_newline ();; | ||
|
||
let test_print_fits_or_breaks m = | ||
print_string "margin = "; | ||
print_int m; | ||
print_newline (); | ||
set_margin m; | ||
open_hvbox 2; | ||
print_string "before"; | ||
print_space (); | ||
print_string "after"; | ||
print_space (); | ||
print_fits_or_breaks "fit" 1 0 "broke"; | ||
print_space (); | ||
print_string "final"; | ||
close_box (); | ||
print_newline () | ||
;; | ||
|
||
test_print_fits_or_breaks 22;; | ||
test_print_fits_or_breaks 23;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
margin = 17 | ||
using print_if_newline: | ||
before | ||
if newline | ||
after later | ||
using print_string_if_newline: | ||
before | ||
if newline | ||
after later | ||
|
||
margin = 18 | ||
using print_if_newline: | ||
before | ||
after | ||
later | ||
using print_string_if_newline: | ||
before | ||
after later | ||
|
||
margin = 20 | ||
using print_if_newline: | ||
before | ||
after | ||
later | ||
using print_string_if_newline: | ||
before after | ||
later | ||
|
||
margin = 30 | ||
using print_if_newline: | ||
before after later | ||
using print_string_if_newline: | ||
before after later | ||
|
||
margin = 19 | ||
before | ||
broke after | ||
final | ||
margin = 20 | ||
before after final | ||
|
||
margin = 22 | ||
before | ||
after | ||
broke | ||
final | ||
margin = 23 | ||
before after fit final | ||
|
||
All tests succeeded. |