package ppx_expect

  1. Overview
  2. Docs

Module Expect_test_matcher.CstSource

Concrete syntax tree of expectations and actual outputs

These types represent the contents of an %expect node or of the actual output. We keep information about the original layout so that we can give an corrected expectation that follows the original formatting.

In the following names, blank means ' ' or '\t', while space means blank or newline.

Sourcemodule Line : sig ... end
Sourcetype 'a single_line = {
  1. leading_blanks : Base.string;
    (*

    regexp: " \t*"

    *)
  2. trailing_spaces : Base.string;
    (*

    regexp: " \t\n*"

    *)
  3. orig : Base.string;
    (*

    regexp: "^ \t\n(^\n*^ \t\n)?"

    *)
  4. data : 'a;
}

Single line represent %expect nodes with data on the first line but not on the subsequent ones.

For instance:

  [%expect "  blah "];
  [%expect {|  blah
           |}]
Sourceval sexp_of_single_line : ('a -> Base.Sexp.t) -> 'a single_line -> Base.Sexp.t
Sourceval compare_single_line : ('a -> 'a -> Base.int) -> 'a single_line -> 'a single_line -> Base.int
Sourceval equal_single_line : ('a -> 'a -> Base.bool) -> 'a single_line -> 'a single_line -> Base.bool
Sourcetype 'a multi_lines = {
  1. leading_spaces : Base.string;
    (*

    regexp: "\( \t*\n\)*"

    *)
  2. trailing_spaces : Base.string;
    (*

    regexp: " \t*" or "\(\n \t*\)*"

    *)
  3. indentation : Base.string;
    (*

    regexp: " \t*"

    *)
  4. lines : 'a Line.t Base.list;
    (*

    regexp: not_blank (.* not_blank)?

    *)
}

Any %expect node with one or more newlines and at least one non-blank line.

This also include the case with exactly one non-blank line such as:

  [%expect {|
    blah
  |}]

This is to preserve this formatting in case the correction is multi-line.

leading_spaces contains everything until the first non-blank line, while trailing_spaces is either:

  • trailing blanks on the last line if of the form:
  [%expect {|
             abc
             def |}]
  • all trailing spaces from the newline character (inclusive) on the last non-blank line to the end if of the form:
  [%expect {|
             abc
             def
  |}]
Sourceval sexp_of_multi_lines : ('a -> Base.Sexp.t) -> 'a multi_lines -> Base.Sexp.t
Sourceval compare_multi_lines : ('a -> 'a -> Base.int) -> 'a multi_lines -> 'a multi_lines -> Base.int
Sourceval equal_multi_lines : ('a -> 'a -> Base.bool) -> 'a multi_lines -> 'a multi_lines -> Base.bool
Sourcetype 'a t =
  1. | Empty of Base.string
    (*

    regexp: " \t\n*"

    *)
  2. | Single_line of 'a single_line
  3. | Multi_lines of 'a multi_lines
Sourceval sexp_of_t : ('a -> Base.Sexp.t) -> 'a t -> Base.Sexp.t
Sourceval compare : ('a -> 'a -> Base.int) -> 'a t -> 'a t -> Base.int
Sourceval equal : ('a -> 'a -> Base.bool) -> 'a t -> 'a t -> Base.bool
Sourceval invariant : ('a -> Base.unit) -> 'a t -> Base.unit
Sourceval empty : 'a t
Sourceval map : 'a t -> f:(Base.string -> 'a -> 'b) -> 'b t
Sourceval data : 'a t -> blank:'a -> 'a Base.list
Sourceval strip : 'a t -> 'a t
Sourceval to_string : _ t -> Base.string
Sourceval to_lines : 'a t -> 'a Line.t Base.list

For single line expectation, leading blanks and trailing spaces are dropped.

Sourceval trim_lines : 'a Line.t Base.list -> 'a Line.t Base.list

Remove blank lines at the beginning and end of the list.

Sourceval reconcile : 'a t -> lines:'a Line.t Base.list -> default_indentation:Base.int -> pad_single_line:Base.bool -> 'a t

Given a contents t and a list of lines, try to produce a new contents containing lines but with the same formating as t.

default_indentation is the indentation to use in case we ignore t's indentation (for instance if t is Single_line or Empty).

Sourceval extract_indentation : 'a Line.t Base.list -> Base.string * 'a Line.t Base.list

Compuute the longest indentation of a list of lines and trim it from every line. It returns the found indentation and the list of trimmed lines.

Sourceval stripped_original_lines : _ t -> Base.string Base.list

All the .orig fields of Line.t or single_line values, using "" for blank lines.

OCaml

Innovation. Community. Security.