upstream: https://github.com/stedolan/crowbar
at main 40 lines 1.2 kB view raw
1open PPrint 2open Crowbar 3type t = (string * PPrint.document) 4let doc = fix (fun doc -> choose [ 5 const ("", empty); 6 const ("a", PPrint.char 'a'); 7 const ("123", string "123"); 8 const ("Hello", string "Hello"); 9 const ("awordwhichisalittlebittoolong", 10 string "awordwhichisalittlebittoolong"); 11 const ("", hardline); 12 map [range 10] (fun n -> ("", break n)); 13 map [range 10] (fun n -> ("", break n)); 14 map [doc; doc] 15 (fun (sa,da) (sb,db) -> (sa ^ sb, da ^^ db)); 16 map [range 10; doc] (fun n (s,d) -> (s, nest n d)); 17 map [doc] (fun (s, d) -> (s, group d)); 18 map [doc] (fun (s, d) -> (s, align d)) 19]) 20 21let check_doc (s, d) = 22 let b = Buffer.create 100 in 23 let w = 40 in 24 ToBuffer.pretty 1.0 w b d; 25 let text = Bytes.to_string (Buffer.to_bytes b) in 26 let ws = Re.(compile (rep (set " \t\n\r"))) in 27 let del_ws s = Re.replace_string ws ~by:"" s in 28 let mspace = Re.(compile (seq [compl [char ' ']; char ' '])) in 29 String.split_on_char '\n' text |> List.iter (fun s -> 30 if String.length s > w then 31 if Re.execp ~pos:w mspace s then assert false); 32 check_eq (del_ws s) (del_ws text) 33 34let suite = 35 ("pprint", 36 [ 37 test_case "pprint" [doc] check_doc; 38 ]) 39 40let () = run "crowbar" [ suite ]