open Mocha open Test_utils module Caml_splice_call = {} @variadic @val external f: (int, array) => int = "Math.max" f(1, [])->ignore @variadic @send external send: (int, array) => int = "send" let f00 = (a, b) => a->send([b]) @send @variadic external push: (array, int, array) => unit = "push" /* This is only test, the binding maybe wrong since in OCaml array'length is not mutable */ test("splice_test_static", () => { let a = [] a->push(1, [2, 3, 4]) eq(__LOC__, a, [1, 2, 3, 4]) }) let dynamic = arr => { let a = [] a->push(1, arr) eq(__LOC__, a, Array.concatAll([[1], arr])) } dynamic([2, 3, 4]) dynamic([]) dynamic([1, 1, 3]) /* Array constructor with a single parameter `x` just makes an array with its length set to `x`, so at least two parameters are needed */ @variadic @new external newArr: (int, int, array) => array = "Array" test("splice_test_newArr_static", () => { let a = newArr(1, 2, [3, 4]) eq(__LOC__, a, [1, 2, 3, 4]) }) let dynamicNew = arr => { let a = newArr(1, 2, arr) eq(__LOC__, a, Array.concatAll([[1, 2], arr])) } dynamicNew([3, 4]) dynamicNew([]) dynamicNew([1, 3]) %%raw(` class Foo { constructor(...names) { this.names = names; } } `) type foo @variadic @new external newFoo: array => foo = "Foo" @get external fooNames: foo => array = "names" test("splice_test_foo_static", () => { let f = newFoo(["a", "b", "c"]) eq(__LOC__, fooNames(f), ["a", "b", "c"]) }) let dynamicFoo = arr => { let f = newFoo(arr) eq(__LOC__, fooNames(f), arr) } dynamicFoo([]) dynamicFoo(["a"]) dynamicFoo(["a", "b", "c"]) module Pipe = { @send @variadic external push: (array, int, array) => unit = "push" /* This is only test, the binding maybe wrong since in OCaml array'length is not mutable */ let () = { let a = [] a->push(1, [2, 3, 4]) eq(__LOC__, a, [1, 2, 3, 4]) } let dynamic = arr => { let a = [] a->push(1, arr) eq(__LOC__, a, Array.concatAll([[1], arr])) } dynamic([2, 3, 4]) dynamic([]) dynamic([1, 1, 3]) } let f1 = (c: array) => f(1, c) describe(__FILE__, () => { test("f1 with [2, 3]", () => eq(__LOC__, 3, f1([2, 3]))) test("f1 with empty", () => eq(__LOC__, 1, f1([]))) test("f1 with many values", () => eq(__LOC__, 5, f1([1, 2, 3, 4, 5, 2, 3]))) })