(*pp camlp4o -I ../lib pa_sexp_conv.cmo *) (* File: conv_test.ml Copyright (C) 2005- Jane Street Holding, LLC Author: Markus Mottl email: mmottl@janestcapital.com WWW: http://www.janestcapital.com/ocaml This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *) (** Conv_test: Module for Testing Automated S-expression Conversions and Path Substitutions *) SEXP_CONV_PATH "Conv_test" open Format open Sexplib open Sexp open Conv type my_float = float let sexp_of_my_float n = Atom (sprintf "%.4f" n) let my_float_of_sexp = function | Atom str -> float_of_string str | _ -> failwith "my_float_of_sexp: atom expected" type foo = A | B of int * float with sexp module M = struct module N = struct type ('a, 'b) variant = [ `X of ('a, 'b) variant | `Y of 'a * 'b ] with sexp end end type 'a variant = [ `V1 of [ `Z | ('a, string) M.N.variant ] option | `V2 ] with sexp type 'a t = { x : foo; a : 'a variant; foo : int; bar : (my_float * string) list option } with sexp type u = { t : int t } with sexp open Path let main () = let make_t a = { x = B (42, 3.1); a = a; foo = 3; bar = Some [(3.1, "foo")]} in let u = { t = make_t (`V1 (Some (`X (`Y (7, "bla"))))) } in let u_sexp = sexp_of_u u in let u' = u_of_sexp u_sexp in assert (u = u'); let foo_sexp = Sexp.of_string "A" in let _foo = foo_of_sexp foo_sexp in printf "Original: %a@\n@\n" pp u_sexp; let path_str = ".[0].[1]" in let path = parse path_str in let subst, el = subst_path u_sexp path in printf "Pos(%s): %a -> SUBST1@\n" path_str pp el; let dumb_sexp = subst (Atom "SUBST1") in printf "Pos(%s): %a@\n@\n" path_str pp dumb_sexp; let path_str = ".t.x.B[1]" in let path = parse path_str in let subst, el = subst_path u_sexp path in printf "Record(%s): %a -> SUBST2@\n" path_str pp el; let u_sexp = subst (Atom "SUBST2") in printf "Record(%s): %a@\n@\n" path_str pp u_sexp; printf "SUCCESS!!!@." let () = try main () with | Of_sexp_error (reason, sexp) -> eprintf "%s: %a@." reason pp_hum sexp