From 53d9c6446890a424ea62f42780c854191c3031d6 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 20 Jun 2018 16:45:55 +0630 Subject: [PATCH] Print atom using atom constructor Signed-off-by: Rudi Grinberg --- test/unit-tests/sexp.mlt | 61 ++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/test/unit-tests/sexp.mlt b/test/unit-tests/sexp.mlt index 8b521688..79db5495 100644 --- a/test/unit-tests/sexp.mlt +++ b/test/unit-tests/sexp.mlt @@ -2,9 +2,21 @@ open Stdune;; open Sexp.Of_sexp;; -let pp_sexp_ast ppf sexp = - Sexp.pp ppf (Sexp.Ast.remove_locs sexp) +let pp_sexp_ast = + let rec subst_atoms ~f (s : Sexp.t) = + match s with + | Atom a -> f a + | Quoted_string _ -> s + | List xs -> List (List.map ~f:(subst_atoms ~f) xs) + in + fun ppf sexp -> + sexp + |> Sexp.Ast.remove_locs + |> subst_atoms ~f:(fun (A s) -> + List [(Sexp.atom "atom"); Sexp.atom_or_quoted_string s]) + |> Sexp.pp ppf ;; + #install_printer pp_sexp_ast;; [%%expect{| val pp_sexp_ast : Format.formatter -> Stdune.Sexp.Ast.t -> unit = @@ -20,7 +32,7 @@ let sexp = Sexp.parse_string ~fname:"" ~mode:Single {| (foo 2)) |} [%%expect{| -val sexp : Usexp.Ast.t = ((foo 1) (foo 2)) +val sexp : Usexp.Ast.t = (((atom foo) (atom 1)) ((atom foo) (atom 2))) |}] let of_sexp = record (field "foo" int) @@ -75,26 +87,34 @@ val parse : string -> parse_result = parse {| # ## x##y x||y a#b|c#d copy# |} [%%expect{| -- : parse_result = Same (Ok [#; ##; x##y; x||y; a#b|c#d; copy#]) +- : parse_result = +Same + (Ok + [(atom #); (atom ##); (atom x##y); (atom x||y); (atom a#b|c#d); + (atom copy#)]) |}] parse {|x #| comment |# y|} [%%expect{| - : parse_result = -Different {jbuild = Ok [x; y]; dune = Ok [x; #|; comment; |#; y]} +Different + {jbuild = Ok [(atom x); (atom y)]; + dune = Ok [(atom x); (atom #|); (atom comment); (atom |#); (atom y)]} |}] parse {|x#|y|} [%%expect{| - : parse_result = -Different {jbuild = Error "jbuild_atoms cannot contain #|"; dune = Ok [x#|y]} +Different + {jbuild = Error "jbuild_atoms cannot contain #|"; dune = Ok [(atom x#|y)]} |}] parse {|x|#y|} [%%expect{| - : parse_result = -Different {jbuild = Error "jbuild_atoms cannot contain |#"; dune = Ok [x|#y]} +Different + {jbuild = Error "jbuild_atoms cannot contain |#"; dune = Ok [(atom x|#y)]} |}] parse {|"\a"|} @@ -139,28 +159,23 @@ parse {|"$bar%foo%"|} - : parse_result = Same (Ok ["$bar%foo%"]) |}] +parse {|\${foo}|} +[%%expect{| +- : parse_result = Same (Ok [(atom \${foo})]) +|}] + parse {|\%{foo}|} [%%expect{| - : parse_result = Different - {jbuild = - Ok - []; - dune = Error "Invalid atom character '%'"} -|}] - -parse {|\${foo}|} -[%%expect{| -- : parse_result = Same (Ok [\${foo}]) + {jbuild = Ok [(atom "\\%{foo}")]; dune = Error "Invalid atom character '%'"} |}] parse {|\$bar%foo%|} [%%expect{| - : parse_result = Different - {jbuild = - Ok - []; + {jbuild = Ok [(atom "\\$bar%foo%")]; dune = Error "Invalid atom character '%'"} |}] @@ -168,9 +183,7 @@ parse {|\$bar\%foo%|} [%%expect{| - : parse_result = Different - {jbuild = - Ok - []; + {jbuild = Ok [(atom "\\$bar\\%foo%")]; dune = Error "Invalid atom character '%'"} |}] @@ -178,8 +191,6 @@ parse {|\$bar\%foo%{bar}|} [%%expect{| - : parse_result = Different - {jbuild = - Ok - []; + {jbuild = Ok [(atom "\\$bar\\%foo%{bar}")]; dune = Error "Invalid atom character '%'"} |}]