Skip to content

Commit 831af1e

Browse files
author
Nathan Rebours
committed
Add test for labeled tuple types encoding
1 parent a3c3012 commit 831af1e

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

.ocamlformat-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ test/driver/non-compressible-suffix/test.ml
4646
test/driver/transformations/test.ml
4747
test/driver/transformations/test_412.ml
4848
test/driver/transformations/test_510.ml
49+
test/encoding/504/api/test.ml
4950
test/expand-header-and-footer/test.ml
5051
test/expansion_helpers/mangle/test.ml
5152
test/expansion_inside_payloads/test.ml

test/encoding/504/api/dune

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(rule
2+
(package ppxlib)
3+
(alias runtest)
4+
(enabled_if
5+
(>= %{ocaml_version} 5.4))
6+
(deps
7+
(:test test.ml)
8+
(package ppxlib))
9+
(action
10+
(chdir
11+
%{project_root}
12+
(progn
13+
(run expect-test %{test})
14+
(diff? %{test} %{test}.corrected)))))

test/encoding/504/api/test.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
open Ppxlib_ast
2+
3+
module To_ocaml = Convert (Js) (Compiler_version)
4+
module From_ocaml = Convert (Compiler_version) (Js)
5+
6+
open Ppxlib
7+
8+
#install_printer Pprintast.core_type;;
9+
10+
module Builder = Ast_builder.Make(struct let loc = Location.none end)
11+
12+
let ptyp_int = Builder.(ptyp_constr (Located.mk (Longident.parse "int")) [])
13+
let ptyp_string =
14+
Builder.(ptyp_constr (Located.mk (Longident.parse "string")) []);;
15+
[%%ignore]
16+
17+
(* Generate an encoded labeled tuple type *)
18+
let encoded_labeled_tuple_type =
19+
Builder.ptyp_labeled_tuple
20+
[ Some "a", ptyp_int
21+
; Some "b", ptyp_int
22+
; None, ptyp_string
23+
]
24+
25+
(* Migrate it to the current compiler (>= 5.4, as per dune rules) *)
26+
let labeled_tuple_type = To_ocaml.copy_core_type encoded_labeled_tuple_type;;
27+
[%%ignore]
28+
29+
let as_source =
30+
Format.asprintf "%a" Astlib.Compiler_pprintast.core_type labeled_tuple_type;;
31+
[%%expect{|
32+
val as_source : string = "(a:int * b:int * string)"
33+
|}]
34+
35+
(* Migrate back to ppxlib's AST *)
36+
let encoded_by_migration = From_ocaml.copy_core_type labeled_tuple_type
37+
38+
let pattern = Ast_pattern.(ptyp_labeled_tuple __);;
39+
[%%ignore]
40+
41+
42+
(* Destruct both the migration and Ast_builder generated encodings with
43+
the Ast_pattern function. *)
44+
let destruct_from_migration =
45+
Ast_pattern.parse_res pattern Location.none encoded_by_migration (fun x -> x);;
46+
[%%expect{|
47+
val destruct_from_migration :
48+
((string option * core_type) list, Location.Error.t Stdppx.NonEmptyList.t)
49+
result = Ok [(Some "a", int); (Some "b", int); (None, string)]
50+
|}]
51+
52+
let destruct =
53+
Ast_pattern.parse_res pattern Location.none
54+
encoded_labeled_tuple_type (fun x -> x);;
55+
[%%expect{|
56+
val destruct :
57+
((string option * core_type) list, Location.Error.t Stdppx.NonEmptyList.t)
58+
result = Ok [(Some "a", int); (Some "b", int); (None, string)]
59+
|}]

test/encoding/504/migrations/dune

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(executable
2+
(name id_driver)
3+
(modules id_driver)
4+
(libraries ppxlib))
5+
6+
(cram
7+
(package ppxlib)
8+
(enabled_if
9+
(>= %{ocaml_version} 5.4))
10+
(deps id_driver.exe))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let () = Ppxlib.Driver.standalone ()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
This test checks that labeled tuple types are correctly encoded
2+
when migrated down to our 5.2 AST
3+
4+
$ cat > test.ml << EOF
5+
> type t = (a: int * b: int * string)
6+
> EOF
7+
8+
$ ./id_driver.exe test.ml
9+
type t =
10+
[%ppxlib.migration.ptyp_labeled_tuple_504 :
11+
(('a * int) * ('b * int) * (_ * string))]
12+
13+
And that it is correctly decoded when migrated back up to 5.4+ ASTS:
14+
15+
$ ./id_driver.exe test.ml --use-compiler-pp
16+
type t = (a:int * b:int * string)

0 commit comments

Comments
 (0)