Skip to content

Commit

Permalink
Add [%%ignore] to expect_test runner and fix quoter tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Nov 20, 2024
1 parent 5444b6e commit fb3c5bf
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 550 deletions.
17 changes: 12 additions & 5 deletions test/expect/expect_lexer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ type version_range =
| From of version
| Between of version * version

type versioned_expect = version_range * string
(*[[%%ignore]], [[%%expect{|...|}] or [%%expect_in 5.3 {|...|}]*)
type expect_block =
| Ignore
| Regular
| Versioned of (version_range * string) list

val split_file :
file_contents:string ->
Lexing.lexbuf ->
(Lexing.position * string * versioned_expect list) list
type chunk = {
phrases : string;
phrases_start : Lexing.position;
expect : expect_block;
}

val split_file : file_contents:string -> Lexing.lexbuf -> chunk list
102 changes: 62 additions & 40 deletions test/expect/expect_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ type version_range =
| From of version (* ex: [%%expect_in 5.3 + *)
| Between of version * version (* ex: [%%expect_in 5.0 >> 5.3 *)

type versioned_expect = version_range * string
(*[%%ignore], [%%expect] or [%%expect_in]*)
type expect_block =
| Ignore
| Regular
| Versioned of (version_range * string) list

type chunk =
{ phrases : string
; phrases_start : Lexing.position
; expect : expect_block
}

let make_version major minor =
let major = int_of_string major in
let minor = int_of_string minor in
(major, minor)

let extract_chunk txt start lexbuf =
let extract_string all_file start lexbuf =
let pos = start.Lexing.pos_cnum in
let len = Lexing.lexeme_start lexbuf - pos in
String.sub txt ~pos ~len
String.sub all_file ~pos ~len
}

let digit = ['0'-'9']
Expand All @@ -28,113 +38,124 @@ let digit = ['0'-'9']
Parses blocks of code to execute seperated by [%%expect{|...|}] statement.
Code blocks can be separated by a single [%%expect{|...|}] statement or by a
series of [%%expect_in <version-range> {|...|}]. *)
rule code txt start = parse
rule code all_file phrases_start = parse
| "[%%expect{|\n" {
let s = extract_chunk txt start lexbuf in
let phrases = extract_string all_file phrases_start lexbuf in
Lexing.new_line lexbuf;
(start, s, []) :: expectation txt lexbuf
let chunk = {phrases; phrases_start; expect = Regular} in
chunk :: expectation all_file lexbuf
}
| "[%%ignore]\n" {
let phrases = extract_string all_file phrases_start lexbuf in
Lexing.new_line lexbuf;
let chunk = {phrases; phrases_start; expect = Ignore} in
chunk :: code all_file lexbuf.lex_curr_p lexbuf
}
| "[%%expect_in " (digit+ as major) '.' (digit+ as minor) " {|\n" {
let s = extract_chunk txt start lexbuf in
let phrases = extract_string all_file phrases_start lexbuf in
Lexing.new_line lexbuf;
let version = make_version major minor in
let range = Only version in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt (start, s) [] (range, cstart) lexbuf
let start = lexbuf.lex_curr_p in
versioned_expectation_content all_file
(phrases_start, phrases) [] (range, start) lexbuf
}
| "[%%expect_in " (digit+ as major) '.' (digit+ as minor) " + {|\n" {
let s = extract_chunk txt start lexbuf in
let phrases = extract_string all_file phrases_start lexbuf in
Lexing.new_line lexbuf;
let version = make_version major minor in
let range = From version in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt (start, s) [] (range, cstart) lexbuf
let start = lexbuf.lex_curr_p in
versioned_expectation_content all_file
(phrases_start, phrases) [] (range, start) lexbuf
}
| "[%%expect_in " (digit+ as major) '.' (digit+ as minor) " - {|\n" {
let s = extract_chunk txt start lexbuf in
let phrases = extract_string all_file phrases_start lexbuf in
Lexing.new_line lexbuf;
let version = make_version major minor in
let range = Up_to version in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt (start, s) [] (range, cstart) lexbuf
let start = lexbuf.lex_curr_p in
versioned_expectation_content all_file
(phrases_start, phrases) [] (range, start) lexbuf
}
| "[%%expect_in "
(digit+ as major1) '.' (digit+ as minor1)
" >> "
(digit+ as major2) '.' (digit+ as minor2)
" {|\n" {
let s = extract_chunk txt start lexbuf in
let phrases = extract_string all_file phrases_start lexbuf in
Lexing.new_line lexbuf;
let v1 = make_version major1 minor1 in
let v2 = make_version major2 minor2 in
let range = Between (v1, v2) in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt (start, s) [] (range, cstart) lexbuf
let start = lexbuf.lex_curr_p in
versioned_expectation_content all_file
(phrases_start, phrases) [] (range, start) lexbuf
}
| [^'\n']*'\n' {
Lexing.new_line lexbuf;
code txt start lexbuf
code all_file phrases_start lexbuf
}
| eof {
let pos = start.Lexing.pos_cnum in
let len = String.length txt - pos in
let pos = phrases_start.Lexing.pos_cnum in
let len = String.length all_file - pos in
if pos > 0 then begin
let s = String.sub txt ~pos ~len in
if String.trim s = "" then
let phrases = String.sub all_file ~pos ~len in
if String.trim phrases = "" then
[]
else
[(start, s, [])]
[{phrases_start; phrases; expect = Regular}]
end else
[]
}

and expectation txt = parse
and expectation all_file = parse
| "|}]\n" {
Lexing.new_line lexbuf;
code txt lexbuf.lex_curr_p lexbuf
code all_file lexbuf.lex_curr_p lexbuf
}
| [^'\n']*'\n' {
Lexing.new_line lexbuf;
expectation txt lexbuf
expectation all_file lexbuf
}

(* Parses the content of a [%%expect_in .. {| ... |}] block along with following
blocks in the same group *)
and versioned_expectation_content txt code_chunk vexpects curr = parse
and versioned_expectation_content all_file code_chunk vexpects curr = parse
| "|}]\n[%%expect_in " (digit+ as major) '.' (digit+ as minor) " {|\n" {
let range, start = curr in
let s = extract_chunk txt start lexbuf in
let s = extract_string all_file start lexbuf in
Lexing.new_line lexbuf;
Lexing.new_line lexbuf;
let block = range, s in
let version = make_version major minor in
let next_range = Only version in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt code_chunk
versioned_expectation_content all_file code_chunk
(block::vexpects) (next_range, cstart) lexbuf
}
| "|}]\n[%%expect_in " (digit+ as major) '.' (digit+ as minor) " + {|\n" {
let range, start = curr in
let s = extract_chunk txt start lexbuf in
let s = extract_string all_file start lexbuf in
Lexing.new_line lexbuf;
Lexing.new_line lexbuf;
let block = range, s in
let version = make_version major minor in
let next_range = From version in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt code_chunk
versioned_expectation_content all_file code_chunk
(block::vexpects) (next_range, cstart) lexbuf
}
| "|}]\n[%%expect_in " (digit+ as major) '.' (digit+ as minor) " - {|\n" {
let range, start = curr in
let s = extract_chunk txt start lexbuf in
let s = extract_string all_file start lexbuf in
Lexing.new_line lexbuf;
Lexing.new_line lexbuf;
let block = range, s in
let version = make_version major minor in
let next_range = Up_to version in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt code_chunk
versioned_expectation_content all_file code_chunk
(block::vexpects) (next_range, cstart) lexbuf
}
| "|}]\n[%%expect_in "
Expand All @@ -143,30 +164,31 @@ and versioned_expectation_content txt code_chunk vexpects curr = parse
(digit+ as major2) '.' (digit+ as minor2)
" {|\n" {
let range, start = curr in
let s = extract_chunk txt start lexbuf in
let s = extract_string all_file start lexbuf in
Lexing.new_line lexbuf;
Lexing.new_line lexbuf;
let block = range, s in
let v1 = make_version major1 minor1 in
let v2 = make_version major2 minor2 in
let next_range = Between (v1, v2) in
let cstart = lexbuf.lex_curr_p in
versioned_expectation_content txt code_chunk
versioned_expectation_content all_file code_chunk
(block::vexpects) (next_range, cstart) lexbuf
}
| "|}]\n" {
let range, start = curr in
let pos = start.Lexing.pos_cnum in
let len = Lexing.lexeme_start lexbuf - pos in
let s = String.sub txt ~pos ~len in
let s = String.sub all_file ~pos ~len in
Lexing.new_line lexbuf;
let vexpects = List.rev ((range, s)::vexpects) in
let start, s = code_chunk in
(start, s, vexpects) :: code txt lexbuf.lex_curr_p lexbuf
let phrases_start, phrases = code_chunk in
let chunk = {phrases; phrases_start; expect = Versioned vexpects} in
chunk :: code all_file lexbuf.lex_curr_p lexbuf
}
| [^'\n']*'\n' {
Lexing.new_line lexbuf;
versioned_expectation_content txt code_chunk vexpects curr lexbuf
versioned_expectation_content all_file code_chunk vexpects curr lexbuf
}

{
Expand Down
19 changes: 15 additions & 4 deletions test/expect/expect_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ let run_code ppf starting_pos code =
execute_phrase ppf phr
with exn -> Location.report_exception ppf exn))

let trash_buffer = Buffer.create 1024
let trash_ppf = Format.formatter_of_buffer trash_buffer

let handle_ignore_block ppf starting_pos code =
Format.fprintf ppf "%s[%%%%ignore]@." code;
run_code trash_ppf starting_pos code;
Buffer.clear trash_buffer

let handle_regular_expect_block ppf starting_pos code =
Format.fprintf ppf "%s[%%%%expect{|@." code;
run_code ppf starting_pos code;
Expand Down Expand Up @@ -192,10 +200,13 @@ let main () =
is statically linked in *)
Topfind.load_deeply [ "ppxlib" ];

List.iter chunks ~f:(fun (pos, s, vexpects) ->
match vexpects with
| [] -> handle_regular_expect_block ppf pos s
| _ -> handle_versioned_expect_blocks ppf pos s vexpects);
List.iter chunks
~f:(fun { Expect_lexer.phrases; phrases_start; expect } ->
match expect with
| Ignore -> handle_ignore_block ppf phrases_start phrases
| Regular -> handle_regular_expect_block ppf phrases_start phrases
| Versioned vexpects ->
handle_versioned_expect_blocks ppf phrases_start phrases vexpects);
Buffer.contents buf)

let () =
Expand Down
Loading

0 comments on commit fb3c5bf

Please sign in to comment.