Skip to content

Commit

Permalink
Unit tests for lib_kind encode/decode
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Lasson <marc.lasson@lexifi.com>
  • Loading branch information
mlasson committed May 8, 2019
1 parent 4f99279 commit 246289c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/unit-tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@
(run %{exe:expect_test.exe} %{t})
(diff? %{t} %{t}.corrected)))))

(alias
(name runtest)
(deps (:t lib_kind.mlt)
(glob_files %{project_root}/src/.dune.objs/byte/*.cmi)
(glob_files %{project_root}/src/stdune/.stdune.objs/byte/*.cmi))
(action (chdir %{project_root}
(progn
(run %{exe:expect_test.exe} %{t})
(diff? %{t} %{t}.corrected)))))

(alias
(name runtest)
(deps (:t string.mlt)
Expand Down
102 changes: 102 additions & 0 deletions test/unit-tests/lib_kind.mlt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
(* -*- tuareg -*- *)
open Dune;;
open! Stdune;;

let sexp_pp = Dune_lang.pp Dune;;
#install_printer sexp_pp;;

let decode_string ~version s =
try
Ok (Dune_lang.Decoder.parse Lib_kind.decode (Univ_map.singleton (Syntax.key Stanza.syntax) version)
(Dune_lang.parse_string ~fname:"" ~mode:Dune_lang.Parser.Mode.Single s))
with exn -> Error exn
[%%expect{|
val sexp_pp : Format.formatter -> Dune_lang.t -> unit = <fun>
val decode_string :
version:Syntax.Version.t -> string -> (Lib_kind.t, exn) Stdune.result =
<fun>
|}]

let all_tests =
[
"normal";
"ppx_deriver";
"ppx_rewriter";
"(ppx_deriver (cookies))";
"(ppx_rewriter (cookies))";
"(ppx_deriver (cookies (\"Name with Space\" \"Value with Space\")))";
"(ppx_deriver (cookies (name1 value1) (name2 value2) (name3 value3)))";
"(ppx_rewriter (cookies (name1 value1) (name2 value2) (name3 value3)))";
]

let decodes_1_10 = List.map (decode_string ~version:(1,10)) all_tests
[%%expect{|
val all_tests : string list =
["normal"; "ppx_deriver"; "ppx_rewriter"; "(ppx_deriver (cookies))";
"(ppx_rewriter (cookies))";
"(ppx_deriver (cookies (\"Name with Space\" \"Value with Space\")))";
"(ppx_deriver (cookies (name1 value1) (name2 value2) (name3 value3)))";
"(ppx_rewriter (cookies (name1 value1) (name2 value2) (name3 value3)))"]
val decodes_1_10 : (Lib_kind.t, exn) Stdune.result list =
[Ok Normal; Ok (Ppx_deriver {cookies = []});
Ok (Ppx_rewriter {cookies = []}); Ok (Ppx_deriver {cookies = []});
Ok (Ppx_rewriter {cookies = []});
Ok (Ppx_deriver {cookies = [{name = "Name with Space"; value = <abstr>}]});
Ok
(Ppx_deriver
{cookies =
[{name = "name1"; value = <abstr>};
{name = "name2"; value = <abstr>};
{name = "name3"; value = <abstr>}]});
Ok
(Ppx_rewriter
{cookies =
[{name = "name1"; value = <abstr>};
{name = "name2"; value = <abstr>};
{name = "name3"; value = <abstr>}]})]
|}]

let decodes_1_9 = List.map (decode_string ~version:(1,8)) all_tests
[%%expect{|
val decodes_1_9 : (Lib_kind.t, exn) Stdune.result list =
[Ok Normal; Ok (Ppx_deriver {cookies = []});
Ok (Ppx_rewriter {cookies = []}); Ok (Ppx_deriver {cookies = []});
Ok (Ppx_rewriter {cookies = []});
Error
(Loc_error
({start = {pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 13};
stop = {pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 61}},
"'cookies' is only available since version 1.10 of the dune language"));
Error
(Loc_error
({start = {pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 13};
stop = {pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 67}},
"'cookies' is only available since version 1.10 of the dune language"));
Error
(Loc_error
({start = {pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 14};
stop = {pos_fname = ""; pos_lnum = 1; pos_bol = 0; pos_cnum = 68}},
"'cookies' is only available since version 1.10 of the dune language"))]
|}]

let encode = function
| Error _ -> None
| Ok e -> Some (Lib_kind.encode e)

let encodes_1_10 = List.map encode decodes_1_10
[%%expect{|
val encode : (Lib_kind.t, 'a) Stdune.result -> Dune_lang.t option = <fun>
val encodes_1_10 : Dune_lang.t option list =
[Some normal; Some ppx_deriver; Some ppx_rewriter; Some ppx_deriver;
Some ppx_rewriter;
Some (ppx_deriver (cookies ("Name with Space" "Value with Space")));
Some (ppx_deriver (cookies (name1 value1) (name2 value2) (name3 value3)));
Some (ppx_rewriter (cookies (name1 value1) (name2 value2) (name3 value3)))]
|}]

let encodes_1_9 = List.map encode decodes_1_9
[%%expect{|
val encodes_1_9 : Dune_lang.t option list =
[Some normal; Some ppx_deriver; Some ppx_rewriter; Some ppx_deriver;
Some ppx_rewriter; None; None; None]
|}]

0 comments on commit 246289c

Please sign in to comment.