-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for lib_kind encode/decode
Signed-off-by: Marc Lasson <marc.lasson@lexifi.com>
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|}] |