Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 0337629

Browse files
committed
add tests for v3 and v4 respectively
1 parent 0bacbe3 commit 0337629

22 files changed

+259
-2
lines changed

scripts/test.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
function exp {
99
echo "$(dirname $1)/expected/$(basename $1).txt"
1010
}
11+
function exp2 {
12+
echo "$(dirname $1)/expected/$(basename $1)$2.txt"
13+
}
1114

1215
taskCount=0
1316
function maybeWait {
@@ -35,10 +38,22 @@ while read file; do
3538
rescript $file &> $(exp $file) & maybeWait
3639
done <temp/files.txt
3740

38-
# printing with ppx
41+
# printing with ppx v3
42+
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
43+
while read file; do
44+
rescript -ppx jsx3 $file &> $(exp2 $file "_v3") & maybeWait
45+
done <temp/files.txt
46+
47+
# printing with ppx v4 classic
48+
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
49+
while read file; do
50+
rescript -ppx jsx4 -jsx-runtime classic $file &> $(exp2 $file "_v4_cls") & maybeWait
51+
done <temp/files.txt
52+
53+
# printing with ppx v4 automatic
3954
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
4055
while read file; do
41-
rescript -ppx jsx $file &> $(exp $file) & maybeWait
56+
rescript -ppx jsx4 -jsx-runtime automatic $file &> $(exp2 $file "_v4_auto") & maybeWait
4257
done <temp/files.txt
4358

4459
wait
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@obj external makeProps: (~msg: 'msg, ~key: string=?, unit) => {"msg": 'msg} = "" // test React JSX file
2+
3+
let make =
4+
(@warning("-16") ~msg) => {
5+
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
6+
}
7+
let make = {
8+
let \"CommentAtTop" = (\"Props": {"msg": 'msg}) => make(~msg=\"Props"["msg"])
9+
\"CommentAtTop"
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type props<'msg> = {@optional key: string, msg: 'msg} // test React JSX file
2+
3+
let make = ({msg}: props<'msg>) => {
4+
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
5+
}
6+
let make = {
7+
let \"CommentAtTop" = (props: props<_>) => make(props)
8+
\"CommentAtTop"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Foo = {
2+
@obj
3+
external componentProps: (~a: int, ~b: string, ~key: string=?, unit) => {"a": int, "b": string} =
4+
""
5+
@module("Foo")
6+
external component: React.componentLike<{"a": int, "b": string}, React.element> = "component"
7+
}
8+
9+
let t = React.createElement(Foo.component, Foo.componentProps(~a=1, ~b={"1"}, ()))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Foo = {
2+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
3+
@module("Foo")
4+
external component: React.componentLike<props<'a, 'b>, React.element> = "component"
5+
}
6+
7+
let t = React.jsx(Foo.component, {a: 1, b: "1"})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module FancyInput = {
2+
@obj
3+
external makeProps: (
4+
~className: 'className=?,
5+
~children: 'children,
6+
~key: string=?,
7+
~ref: 'ref=?,
8+
unit,
9+
) => {"className": option<'className>, "children": 'children} = ""
10+
let make =
11+
(@warning("-16") ~className=?, @warning("-16") ~children) =>
12+
@warning("-16")
13+
ref =>
14+
ReactDOMRe.createDOMElementVariadic(
15+
"div",
16+
[
17+
ReactDOMRe.createDOMElementVariadic(
18+
"input",
19+
~props=ReactDOMRe.domProps(
20+
~type_="text",
21+
~className?,
22+
~ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)},
23+
(),
24+
),
25+
[],
26+
),
27+
children,
28+
],
29+
)
30+
let make = React.forwardRef({
31+
let \"ForwardRef$FancyInput" = (
32+
\"Props": {"className": option<'className>, "children": 'children},
33+
ref,
34+
) => make(~children=\"Props"["children"], ~className=?\"Props"["className"], ref)
35+
\"ForwardRef$FancyInput"
36+
})
37+
}
38+
@obj external makeProps: (~key: string=?, unit) => {.} = ""
39+
40+
let make = () => {
41+
let input = React.useRef(Js.Nullable.null)
42+
43+
ReactDOMRe.createDOMElementVariadic(
44+
"div",
45+
[
46+
React.createElement(
47+
FancyInput.make,
48+
FancyInput.makeProps(~ref=input, ~children={React.string("Click to focus")}, ()),
49+
),
50+
],
51+
)
52+
}
53+
let make = {
54+
let \"ForwardRef" = (\"Props": {.}) => make()
55+
\"ForwardRef"
56+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module FancyInput = {
2+
type props<'className, 'children> = {
3+
@optional key: string,
4+
@optional className: 'className,
5+
children: 'children,
6+
@optional ref: ReactDOM.Ref.currentDomRef,
7+
}
8+
let make = ({className, children, ref}: props<'className, 'children>) => {
9+
let ref = Js.Nullable.fromOption(ref)
10+
let _ = ref
11+
12+
ReactDOMRe.createDOMElementVariadic(
13+
"div",
14+
[
15+
ReactDOMRe.createDOMElementVariadic(
16+
"input",
17+
~props=ReactDOMRe.domProps(
18+
~type_="text",
19+
~className?,
20+
~ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)},
21+
(),
22+
),
23+
[],
24+
),
25+
children,
26+
],
27+
)
28+
}
29+
let make = React.forwardRef({
30+
let \"ForwardRef$FancyInput" = (props: props<_>, ref) =>
31+
make({...props, ref: @optional Js.Nullable.toOption(ref)})
32+
\"ForwardRef$FancyInput"
33+
})
34+
}
35+
type props = {@optional key: string}
36+
37+
let make = (_: props) => {
38+
let input = React.useRef(Js.Nullable.null)
39+
40+
ReactDOMRe.createDOMElementVariadic(
41+
"div",
42+
[React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}})],
43+
)
44+
}
45+
let make = {
46+
let \"ForwardRef" = props => make(props)
47+
\"ForwardRef"
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Bar = {
2+
@obj external makeProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
3+
let make =
4+
(@warning("-16") ~a, @warning("-16") ~b, _) => {
5+
Js.log("This function should be named `InnerModule.react$Bar`")
6+
ReactDOMRe.createDOMElementVariadic("div", [])
7+
}
8+
let make = {
9+
let \"InnerModule$Bar" = (\"Props": {"a": 'a, "b": 'b}) =>
10+
make(~b=\"Props"["b"], ~a=\"Props"["a"], ())
11+
\"InnerModule$Bar"
12+
}
13+
@obj external componentProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
14+
15+
let component =
16+
(@warning("-16") ~a, @warning("-16") ~b, _) => {
17+
Js.log("This function should be named `InnerModule.react$Bar$component`")
18+
ReactDOMRe.createDOMElementVariadic("div", [])
19+
}
20+
let component = {
21+
let \"InnerModule$Bar$component" = (\"Props": {"a": 'a, "b": 'b}) =>
22+
component(~b=\"Props"["b"], ~a=\"Props"["a"], ())
23+
\"InnerModule$Bar$component"
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Bar = {
2+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
3+
let make = ({a, b}: props<'a, 'b>) => {
4+
Js.log("This function should be named `InnerModule.react$Bar`")
5+
ReactDOMRe.createDOMElementVariadic("div", [])
6+
}
7+
let make = {
8+
let \"InnerModule$Bar" = (props: props<_>) => make(props)
9+
\"InnerModule$Bar"
10+
}
11+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
12+
13+
let component = ({a, b}: props<'a, 'b>) => {
14+
Js.log("This function should be named `InnerModule.react$Bar$component`")
15+
ReactDOMRe.createDOMElementVariadic("div", [])
16+
}
17+
let component = {
18+
let \"InnerModule$Bar$component" = (props: props<_>) => make(props)
19+
\"InnerModule$Bar$component"
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@obj
2+
external makeProps: (
3+
~a: '\"type-a",
4+
~b: array<option<[#Foo('\"type-a")]>>,
5+
~c: 'a,
6+
~key: string=?,
7+
unit,
8+
) => {"a": '\"type-a", "b": array<option<[#Foo('\"type-a")]>>, "c": 'a} = ""
9+
let make = (type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
10+
ReactDOMRe.createDOMElementVariadic("div", [])
11+
let make = {
12+
let \"Newtype" = (\"Props": {"a": '\"type-a", "b": array<option<[#Foo('\"type-a")]>>, "c": 'a}) =>
13+
make(~c=\"Props"["c"], ~b=\"Props"["b"], ~a=\"Props"["a"])
14+
\"Newtype"
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type props<'a, 'b, 'c> = {@optional key: string, a: 'a, b: 'b, c: 'c}
2+
let make = (_: props<'a, 'b, 'c>, type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
3+
ReactDOMRe.createDOMElementVariadic("div", [])
4+
let make = {
5+
let \"Newtype" = (props: props<_>) => make(props)
6+
\"Newtype"
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@obj external makeProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
2+
let make =
3+
(@warning("-16") ~a, @warning("-16") ~b, _) => {
4+
Js.log("This function should be named 'TopLevel.react'")
5+
ReactDOMRe.createDOMElementVariadic("div", [])
6+
}
7+
let make = {
8+
let \"TopLevel" = (\"Props": {"a": 'a, "b": 'b}) => make(~b=\"Props"["b"], ~a=\"Props"["a"], ())
9+
\"TopLevel"
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
2+
let make = ({a, b}: props<'a, 'b>) => {
3+
Js.log("This function should be named 'TopLevel.react'")
4+
ReactDOMRe.createDOMElementVariadic("div", [])
5+
}
6+
let make = {
7+
let \"TopLevel" = (props: props<_>) => make(props)
8+
\"TopLevel"
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@obj external makeProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
2+
let make:
3+
type a. (~a: a, ~b: a, a) => React.element =
4+
(~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
5+
let make = {
6+
let \"TypeConstraint" = (\"Props": {"a": 'a, "b": 'b}) => make(~b=\"Props"["b"], ~a=\"Props"["a"])
7+
\"TypeConstraint"
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
2+
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = (_: props<'a, 'b>, type a): (
3+
(~a: a, ~b: a, a) => React.element
4+
) => (~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
5+
let make = {
6+
let \"TypeConstraint" = (props: props<_>) => make(props)
7+
\"TypeConstraint"
8+
}

0 commit comments

Comments
 (0)