Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mlasson committed Feb 1, 2021
1 parent e1df5d2 commit e6154ee
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 21 deletions.
80 changes: 59 additions & 21 deletions node-test/test1/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,73 @@ let root : unit Promise.t =
(*** Index signature **)

include [%js:
module MapLike : sig
type 'a t = private Ojs.t
module ArrayLike (K : Ojs.T) : sig
type t = private Ojs.t
val t_to_js: t -> Ojs.t
val t_of_js: Ojs.t -> t

val t_to_js: ('a -> Ojs.t) -> 'a t -> Ojs.t
val t_of_js: (Ojs.t -> 'a) -> Ojs.t -> 'a t
val create: unit -> t [@@js.builder]
val get: t -> int -> K.t option [@@js.index_get]
val set: t -> int -> K.t -> unit [@@js.index_set]
end

val get: 'a t -> string -> 'a option [@@js.index_get]
val set: 'a t -> string -> 'a -> unit [@@js.index_set]
module MapLike (K : Ojs.T) : sig
type t = private Ojs.t
val t_to_js: t -> Ojs.t
val t_of_js: Ojs.t -> t

val create: unit -> t [@@js.builder]
val get: t -> string -> K.t option [@@js.index_get]
val set: t -> string -> K.t -> unit [@@js.index_set]
end

type js_string = private Ojs.t
val js_string_of_js: Ojs.t -> js_string
val js_string_to_js: js_string -> Ojs.t
module Symbol : sig
type t = private Ojs.t
val t_to_js: t -> Ojs.t
val t_of_js: Ojs.t -> t

val fresh: unit -> t [@@js.global "Symbol"]
end

module SymbolMap (K : Ojs.T) : sig
type t = private Ojs.t
val t_to_js: t -> Ojs.t
val t_of_js: Ojs.t -> t

val create: unit -> t [@@js.builder]
val get: t -> Symbol.t -> K.t option [@@js.index_get]
val set: t -> Symbol.t -> K.t -> unit [@@js.index_set]
end

[@@@js.stop]
val js_string: string -> js_string
[@@@js.start]
[@@@js.implem
let js_string = Ojs.string_to_js
]
]

let () =
let test_obj = Ojs.obj [| ("foo", Ojs.string_to_js "bar") |] in
let map_str = MapLike.t_of_js (js_string_of_js) test_obj in
assert (MapLike.get map_str "foo" = Some (js_string "bar"));
MapLike.set map_str "baz" (js_string "boo");
assert (MapLike.get map_str "baz" = Some (js_string "boo"));
()
let module M = MapLike([%js: type t = string val t_to_js: t -> Ojs.t val t_of_js: Ojs.t -> t]) in
let map_str = M.create () in
M.set map_str "foo" "bar";
assert (M.get map_str "foo" = Some "bar");
M.set map_str "baz" "boo";
assert (M.get map_str "baz" = Some "boo");

let module A = ArrayLike([%js: type t = int val t_to_js: t -> Ojs.t val t_of_js: Ojs.t -> t]) in
let map_int = A.create () in
let len = 10 in
for k = 0 to len - 1 do
A.set map_int k k;
assert (A.get map_int k = Some k);
A.set map_int k (k * k);
assert (A.get map_int k = Some (k * k));
done;

let module M = SymbolMap([%js: type t = string val t_to_js: t -> Ojs.t val t_of_js: Ojs.t -> t]) in
let a = Symbol.fresh () in
let b = Symbol.fresh () in
let map_str = M.create () in
M.set map_str a "bar";
assert (M.get map_str a = Some "bar");
M.set map_str b "boo";
assert (M.get map_str b = Some "boo")


(*** Function signature **)

Expand Down
8 changes: 8 additions & 0 deletions ppx-test/binding.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ module M : sig
val new_thing: int -> t [@@js.new]

val builder: ?x:int -> (int [@js "y"]) -> z:int -> t [@@js.builder]

val index_get_int: t -> int -> string option [@@js.index_get]
val index_get_string: t -> string -> string option [@@js.index_get]
val index_get_generic: t -> Ojs.t -> string option [@@js.index_get]

val index_set_int: t -> int -> string -> unit [@@js.index_set]
val index_set_string: t -> string -> string -> unit [@@js.index_set]
val index_set_generic: t -> Ojs.t -> string -> unit [@@js.index_set]
end
9 changes: 9 additions & 0 deletions ppx-test/binding_automatic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ module M : sig
val method_call_unit_unit: t -> unit -> unit
val method_call_args_unit: t -> int -> unit
val global: t

[@@@warning "-32"]
val get: t -> int -> string option
val set: t -> int -> string -> unit
val get: t -> string -> string option
val set: t -> string -> string -> unit
[@@@warning "+32"]
val get: t -> Ojs.t -> string option
val set: t -> Ojs.t -> string -> unit
end
9 changes: 9 additions & 0 deletions ppx-test/binding_explicitly_automatic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ module M : sig
val method_call_unit_unit: t -> unit -> unit [@@js]
val method_call_args_unit: t -> int -> unit [@@js]
val global: t [@@js]

[@@@warning "-32"]
val get: t -> int -> string option [@@js]
val set: t -> int -> string -> unit [@@js]
val get: t -> string -> string option [@@js]
val set: t -> string -> string -> unit [@@js]
[@@@warning "+32"]
val get: t -> Ojs.t -> string option [@@js]
val set: t -> Ojs.t -> string -> unit [@@js]
end
9 changes: 9 additions & 0 deletions ppx-test/binding_manual.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ module M : sig
val method_call_unit_unit: t -> unit -> unit[@@js.call "methodCallUnitUnit"]
val method_call_args_unit: t -> int -> unit[@@js.call "methodCallArgsUnit"]
val global: t[@@js.global "global"]

[@@@warning "-32"]
val get: t -> int -> string option [@@js.index_get]
val set: t -> int -> string -> unit [@@js.index_set]
val get: t -> string -> string option [@@js.index_get]
val set: t -> string -> string -> unit [@@js.index_set]
[@@@warning "+32"]
val get: t -> Ojs.t -> string option [@@js.index_get]
val set: t -> Ojs.t -> string -> unit [@@js.index_set]
end
25 changes: 25 additions & 0 deletions ppx-test/expected/binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,29 @@ module M =
Ojs.set x21 "y" (Ojs.int_to_js x19);
Ojs.set x21 "z" (Ojs.int_to_js x20);
t_of_js x21
let (index_get_int : t -> int -> string option) =
fun x23 ->
fun x24 ->
Ojs.option_of_js Ojs.string_of_js (Ojs.array_get (t_to_js x23) x24)
let (index_get_string : t -> string -> string option) =
fun x26 ->
fun x27 ->
Ojs.option_of_js Ojs.string_of_js (Ojs.get (t_to_js x26) x27)
let (index_get_generic : t -> Ojs.t -> string option) =
fun x29 ->
fun x30 ->
Ojs.option_of_js Ojs.string_of_js
(Ojs.generic_get (t_to_js x29) x30)
let (index_set_int : t -> int -> string -> unit) =
fun x32 ->
fun x33 ->
fun x34 -> Ojs.array_set (t_to_js x32) x33 (Ojs.string_to_js x34)
let (index_set_string : t -> string -> string -> unit) =
fun x35 ->
fun x36 ->
fun x37 -> Ojs.set (t_to_js x35) x36 (Ojs.string_to_js x37)
let (index_set_generic : t -> Ojs.t -> string -> unit) =
fun x38 ->
fun x39 ->
fun x40 -> Ojs.generic_set (t_to_js x38) x39 (Ojs.string_to_js x40)
end
25 changes: 25 additions & 0 deletions ppx-test/expected/binding_automatic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,29 @@ module M =
(Ojs.call (t_to_js x14) "methodCallArgsUnit"
[|(Ojs.int_to_js x13)|])
let (global : t) = t_of_js (Ojs.get Ojs.global "global")
let (get : t -> int -> string option) =
fun x15 ->
fun x16 ->
Ojs.option_of_js Ojs.string_of_js (Ojs.array_get (t_to_js x15) x16)
let (set : t -> int -> string -> unit) =
fun x18 ->
fun x19 ->
fun x20 -> Ojs.array_set (t_to_js x18) x19 (Ojs.string_to_js x20)
let (get : t -> string -> string option) =
fun x21 ->
fun x22 ->
Ojs.option_of_js Ojs.string_of_js (Ojs.get (t_to_js x21) x22)
let (set : t -> string -> string -> unit) =
fun x24 ->
fun x25 ->
fun x26 -> Ojs.set (t_to_js x24) x25 (Ojs.string_to_js x26)
let (get : t -> Ojs.t -> string option) =
fun x27 ->
fun x28 ->
Ojs.option_of_js Ojs.string_of_js
(Ojs.generic_get (t_to_js x27) x28)
let (set : t -> Ojs.t -> string -> unit) =
fun x30 ->
fun x31 ->
fun x32 -> Ojs.generic_set (t_to_js x30) x31 (Ojs.string_to_js x32)
end

0 comments on commit e6154ee

Please sign in to comment.