Skip to content

Commit 990c26e

Browse files
Fix padding by adding hpad & vpad in Cram.t
1 parent 750c722 commit 990c26e

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

lib/cram.ml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ module Log = (val Logs.src_log src : Logs.LOG)
2020
open Astring
2121
open Misc
2222

23-
type t = { command : string list; output : Output.t list; exit_code : int }
23+
type t = {
24+
command : string list;
25+
output : Output.t list;
26+
exit_code : int;
27+
vpad : int;
28+
}
2429

2530
let dump_line ppf = function
2631
| #Output.t as o -> Output.dump ppf o
@@ -37,10 +42,20 @@ let dump ppf (t : t) =
3742
Fmt.(Dump.list Output.dump)
3843
t.output t.exit_code
3944

45+
let pp_vpad ppf t =
46+
let rec loop = function
47+
| 0 -> ()
48+
| n ->
49+
Fmt.pf ppf "\n";
50+
loop (Int.pred n)
51+
in
52+
loop t.vpad
53+
4054
let pp_command ?(pad = 0) ppf (t : t) =
4155
match t.command with
4256
| [] -> ()
4357
| l ->
58+
pp_vpad ppf t;
4459
let sep ppf () = Fmt.pf ppf "\\\n%a> " pp_pad pad in
4560
Fmt.pf ppf "%a$ %a\n" pp_pad pad Fmt.(list ~sep string) l
4661

@@ -52,7 +67,7 @@ let pp ?pad ppf (t : t) =
5267
pp_lines (Output.pp ?pad) ppf t.output;
5368
pp_exit_code ?pad ppf t.exit_code
5469

55-
let pad_of_lines = function
70+
let hpad_of_lines = function
5671
| [] -> 0
5772
| h :: _ ->
5873
let i = ref 0 in
@@ -61,22 +76,29 @@ let pad_of_lines = function
6176
done;
6277
!i
6378

64-
let of_lines t =
65-
let pad = pad_of_lines t in
79+
let of_lines ~syntax ~(loc : Location.t) t =
80+
let pos = loc.loc_start in
81+
let hpad =
82+
match syntax with Syntax.Mli -> pos.pos_cnum + 2 | _ -> hpad_of_lines t
83+
in
6684
let unpad line =
67-
if String.is_empty line then line
68-
else if String.length line < pad then
69-
Fmt.failwith "invalid padding: %S" line
70-
else String.with_index_range line ~first:pad
85+
match syntax with
86+
| Syntax.Mli -> String.trim line
87+
| _ ->
88+
if String.is_empty line then line
89+
else if String.length line < hpad then
90+
Fmt.failwith "invalid padding: %S" line
91+
else String.with_index_range line ~first:hpad
7192
in
7293
let lines = List.map unpad t in
7394
let lines =
7495
Lexer_cram.token (Lexing.from_string (String.concat ~sep:"\n" lines))
7596
in
97+
let vpad = match syntax with Syntax.Mli -> 1 | _ -> 0 in
7698
Log.debug (fun l ->
77-
l "Cram.of_lines (pad=%d) %a" pad Fmt.(Dump.list dump_line) lines);
99+
l "Cram.of_lines (pad=%d) %a" hpad Fmt.(Dump.list dump_line) lines);
78100
let mk command output exit_code =
79-
{ command; output = List.rev output; exit_code }
101+
{ command; output = List.rev output; exit_code; vpad }
80102
in
81103
let rec command_cont acc = function
82104
| `Command_cont c :: t -> command_cont (c :: acc) t
@@ -101,8 +123,8 @@ let of_lines t =
101123
match lines with
102124
| `Command_first cmd :: t ->
103125
let cmd, t = command_cont [ cmd ] t in
104-
(pad, aux cmd [] [] t)
105-
| `Command cmd :: t -> (pad, aux [ cmd ] [] [] t)
126+
(hpad, aux cmd [] [] t)
127+
| `Command cmd :: t -> (hpad, aux [ cmd ] [] [] t)
106128
| [] -> (0, [])
107129
| `Output line :: _ ->
108130
if String.length line > 0 && line.[0] = '$' then

lib/cram.mli

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
(** Cram tests *)
1818

19-
type t = { command : string list; output : Output.t list; exit_code : int }
19+
type t = {
20+
command : string list;
21+
output : Output.t list;
22+
exit_code : int;
23+
vpad : int;
24+
}
2025
(** The type for cram tests. *)
2126

2227
(** {2 Accessors} *)
@@ -34,7 +39,7 @@ val command_line : t -> string
3439

3540
(** {2 Parser} *)
3641

37-
val of_lines : string list -> int * t list
42+
val of_lines : syntax:Syntax.t -> loc:Location.t -> string list -> int * t list
3843
(** [of_lines l] parses the commands [l]. It returns the optional
3944
whitespace padding. *)
4045

lib/test/mdx_test.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let run_cram_tests ?syntax t ?root ppf temp_file pad tests =
118118
output;
119119
Cram.pp_exit_code ~pad ppf n)
120120
tests;
121-
Block.pp_footer ?syntax ppf t
121+
match syntax with Some Syntax.Mli -> () | _ -> Block.pp_footer ?syntax ppf t
122122

123123
let eval_test ?block ?root c cmd =
124124
Log.debug (fun l -> l "eval_test %a" Fmt.(Dump.list (Fmt.fmt "%S")) cmd);
@@ -305,7 +305,11 @@ let run_exn ~non_deterministic ~silent_eval ~record_backtrace ~syntax ~silent
305305
with_non_det non_deterministic non_det ~command:print_block
306306
~output:det ~det
307307
| Cram { language = _; non_det } ->
308-
let pad, tests = Cram.of_lines t.contents in
308+
let pad, tests =
309+
Cram.of_lines
310+
~syntax:(Option.value ~default:Normal syntax)
311+
~loc:t.loc t.contents
312+
in
309313
with_non_det non_deterministic non_det ~command:print_block
310314
~output:(fun () ->
311315
print_block ();

0 commit comments

Comments
 (0)