Skip to content

Commit

Permalink
Merge pull request #5 from NathanReb/extract-ppx-yojson-lib
Browse files Browse the repository at this point in the history
Extract Ppx_yojson_lib and tests
  • Loading branch information
NathanReb committed Oct 5, 2018
2 parents 7971dcc + d7cb0b2 commit d7af3d5
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 16 deletions.
12 changes: 12 additions & 0 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(library
(name ppx_yojson_lib)
(public_name ppx_yojson._lib)
(libraries
ppxlib
)
(preprocess
(pps
ppxlib.metaquot
)
)
)
16 changes: 0 additions & 16 deletions src/ppx_yojson.ml → lib/ppx_yojson_lib.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
open Ppxlib

let name = "yojson"

let unsupported ~loc =
Location.raise_errorf ~loc "ppx_yojson: unsupported payload"

Expand Down Expand Up @@ -69,17 +67,3 @@ and expand_record ~loc ~path l =
in
let expr_list = List.map expand_one l in
[%expr [%e Ast_builder.Default.elist ~loc expr_list]]

let expr_extension =
Extension.declare
name
Extension.Context.expression
Ast_pattern.(single_expr_payload __)
expand_expr

let expr_rule = Ppxlib.Context_free.Rule.extension expr_extension

let () =
Ppxlib.Driver.register_transformation
~rules:[expr_rule]
name
11 changes: 11 additions & 0 deletions lib/ppx_yojson_lib.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Einteger : sig
val is_binary : Ppxlib.label -> bool
val is_octal : Ppxlib.label -> bool
val is_hexadecimal : Ppxlib.label -> bool
end

val expand_expr :
loc: Ppxlib.Location.t ->
path: string ->
Ppxlib.expression ->
Ppxlib.expression
1 change: 1 addition & 0 deletions ppx_yojson.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ depends: [
"ocaml" {>= "4.04.2"}
"ounit" {with-test & >= "2.0.0"}
"ppxlib" {>= "0.3.0"}
"ppx_deriving" {with-test}
"yojson" {with-test}
]
1 change: 1 addition & 0 deletions src/dune → rewriter/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(public_name ppx_yojson)
(kind ppx_rewriter)
(libraries
ppx_yojson_lib
ppxlib
)
(preprocess
Expand Down
17 changes: 17 additions & 0 deletions rewriter/ppx_yojson.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
open Ppxlib

let name = "yojson"

let expr_extension =
Extension.declare
name
Extension.Context.expression
Ast_pattern.(single_expr_payload __)
Ppx_yojson_lib.expand_expr

let expr_rule = Ppxlib.Context_free.Rule.extension expr_extension

let () =
Ppxlib.Driver.register_transformation
~rules:[expr_rule]
name
File renamed without changes.
12 changes: 12 additions & 0 deletions test/lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(test
(name test_ppx_yojson_lib)
(libraries
ppx_yojson_lib
oUnit
)
(preprocess
(pps
ppx_deriving.std
)
)
)
44 changes: 44 additions & 0 deletions test/lib/test_ppx_yojson_lib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
open OUnit2

module Einteger = struct
let test_is_binary =
let test = Utils.test_prop Ppx_yojson_lib.Einteger.is_binary in
let open Utils.Infix in
"is_binary" >:::
[ "0b1" >:= test true
; "0B1" >:= test true
; "123" >:= test false
]

let test_is_octal =
let test = Utils.test_prop Ppx_yojson_lib.Einteger.is_octal in
let open Utils.Infix in
"is_octal" >:::
[ "0o1" >:= test true
; "0O1" >:= test true
; "123" >:= test false
]

let test_is_hexadecimal =
let test = Utils.test_prop Ppx_yojson_lib.Einteger.is_hexadecimal in
let open Utils.Infix in
"is_hexadecimal" >:::
[ "0x1" >:= test true
; "0X1" >:= test true
; "123" >:= test false
]

let suite =
"Einteger" >:::
[ test_is_binary
; test_is_octal
; test_is_hexadecimal
]
end

let suite =
"Ppx_yojson_lib" >:::
[ Einteger.suite
]

let () = run_test_tt_main suite
9 changes: 9 additions & 0 deletions test/lib/utils.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
open OUnit2

let test_prop prop expected input ctxt =
let actual = prop input in
assert_equal ~ctxt ~cmp:[%eq: bool] ~printer:[%show: bool] expected actual

module Infix = struct
let (>:=) name test = name >:: test name
end
9 changes: 9 additions & 0 deletions test/lib/utils.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(** Test function for properties.
[test_prop prop expected input ctxt] is [assert_equal ~ctxt expected (prop input)]
*)
val test_prop : ('a -> bool) -> bool -> 'a -> OUnit2.test_ctxt -> unit

module Infix : sig
(** Create a test which also takes its label as input *)
val (>:=) : string -> (string -> OUnit2.test_fun) -> OUnit2.test
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d7af3d5

Please sign in to comment.