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

Commit

Permalink
add tests for v3 and v4 respectively
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Jun 24, 2022
1 parent 167d536 commit 23a6ca1
Show file tree
Hide file tree
Showing 22 changed files with 259 additions and 2 deletions.
19 changes: 17 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
function exp {
echo "$(dirname $1)/expected/$(basename $1).txt"
}
function exp2 {
echo "$(dirname $1)/expected/$(basename $1)$2.txt"
}

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

# printing with ppx
# printing with ppx v3
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
while read file; do
rescript -ppx jsx3 $file &> $(exp2 $file "_v3") & maybeWait
done <temp/files.txt

# printing with ppx v4 classic
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
while read file; do
rescript -ppx jsx4 -jsx-runtime classic $file &> $(exp2 $file "_v4_cls") & maybeWait
done <temp/files.txt

# printing with ppx v4 automatic
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
while read file; do
rescript -ppx jsx $file &> $(exp $file) & maybeWait
rescript -ppx jsx4 -jsx-runtime automatic $file &> $(exp2 $file "_v4_auto") & maybeWait
done <temp/files.txt

wait
Expand Down
10 changes: 10 additions & 0 deletions tests/ppx/react/expected/commentAtTop.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@obj external makeProps: (~msg: 'msg, ~key: string=?, unit) => {"msg": 'msg} = "" // test React JSX file

let make =
(@warning("-16") ~msg) => {
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
}
let make = {
let \"CommentAtTop" = (\"Props": {"msg": 'msg}) => make(~msg=\"Props"["msg"])
\"CommentAtTop"
}
9 changes: 9 additions & 0 deletions tests/ppx/react/expected/commentAtTop.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type props<'msg> = {@optional key: string, msg: 'msg} // test React JSX file

let make = ({msg}: props<'msg>) => {
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
}
let make = {
let \"CommentAtTop" = (props: props<_>) => make(props)
\"CommentAtTop"
}
9 changes: 9 additions & 0 deletions tests/ppx/react/expected/externalWithCustomName.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Foo = {
@obj
external componentProps: (~a: int, ~b: string, ~key: string=?, unit) => {"a": int, "b": string} =
""
@module("Foo")
external component: React.componentLike<{"a": int, "b": string}, React.element> = "component"
}

let t = React.createElement(Foo.component, Foo.componentProps(~a=1, ~b={"1"}, ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Foo = {
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
@module("Foo")
external component: React.componentLike<props<'a, 'b>, React.element> = "component"
}

let t = React.jsx(Foo.component, {a: 1, b: "1"})
56 changes: 56 additions & 0 deletions tests/ppx/react/expected/forwardRef.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module FancyInput = {
@obj
external makeProps: (
~className: 'className=?,
~children: 'children,
~key: string=?,
~ref: 'ref=?,
unit,
) => {"className": option<'className>, "children": 'children} = ""
let make =
(@warning("-16") ~className=?, @warning("-16") ~children) =>
@warning("-16")
ref =>
ReactDOMRe.createDOMElementVariadic(
"div",
[
ReactDOMRe.createDOMElementVariadic(
"input",
~props=ReactDOMRe.domProps(
~type_="text",
~className?,
~ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)},
(),
),
[],
),
children,
],
)
let make = React.forwardRef({
let \"ForwardRef$FancyInput" = (
\"Props": {"className": option<'className>, "children": 'children},
ref,
) => make(~children=\"Props"["children"], ~className=?\"Props"["className"], ref)
\"ForwardRef$FancyInput"
})
}
@obj external makeProps: (~key: string=?, unit) => {.} = ""

let make = () => {
let input = React.useRef(Js.Nullable.null)

ReactDOMRe.createDOMElementVariadic(
"div",
[
React.createElement(
FancyInput.make,
FancyInput.makeProps(~ref=input, ~children={React.string("Click to focus")}, ()),
),
],
)
}
let make = {
let \"ForwardRef" = (\"Props": {.}) => make()
\"ForwardRef"
}
48 changes: 48 additions & 0 deletions tests/ppx/react/expected/forwardRef.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module FancyInput = {
type props<'className, 'children> = {
@optional key: string,
@optional className: 'className,
children: 'children,
@optional ref: ReactDOM.Ref.currentDomRef,
}
let make = ({className, children, ref}: props<'className, 'children>) => {
let ref = Js.Nullable.fromOption(ref)
let _ = ref

ReactDOMRe.createDOMElementVariadic(
"div",
[
ReactDOMRe.createDOMElementVariadic(
"input",
~props=ReactDOMRe.domProps(
~type_="text",
~className?,
~ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)},
(),
),
[],
),
children,
],
)
}
let make = React.forwardRef({
let \"ForwardRef$FancyInput" = (props: props<_>, ref) =>
make({...props, ref: @optional Js.Nullable.toOption(ref)})
\"ForwardRef$FancyInput"
})
}
type props = {@optional key: string}

let make = (_: props) => {
let input = React.useRef(Js.Nullable.null)

ReactDOMRe.createDOMElementVariadic(
"div",
[React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}})],
)
}
let make = {
let \"ForwardRef" = props => make(props)
\"ForwardRef"
}
25 changes: 25 additions & 0 deletions tests/ppx/react/expected/innerModule.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Bar = {
@obj external makeProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
let make =
(@warning("-16") ~a, @warning("-16") ~b, _) => {
Js.log("This function should be named `InnerModule.react$Bar`")
ReactDOMRe.createDOMElementVariadic("div", [])
}
let make = {
let \"InnerModule$Bar" = (\"Props": {"a": 'a, "b": 'b}) =>
make(~b=\"Props"["b"], ~a=\"Props"["a"], ())
\"InnerModule$Bar"
}
@obj external componentProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""

let component =
(@warning("-16") ~a, @warning("-16") ~b, _) => {
Js.log("This function should be named `InnerModule.react$Bar$component`")
ReactDOMRe.createDOMElementVariadic("div", [])
}
let component = {
let \"InnerModule$Bar$component" = (\"Props": {"a": 'a, "b": 'b}) =>
component(~b=\"Props"["b"], ~a=\"Props"["a"], ())
\"InnerModule$Bar$component"
}
}
21 changes: 21 additions & 0 deletions tests/ppx/react/expected/innerModule.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Bar = {
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
let make = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named `InnerModule.react$Bar`")
ReactDOMRe.createDOMElementVariadic("div", [])
}
let make = {
let \"InnerModule$Bar" = (props: props<_>) => make(props)
\"InnerModule$Bar"
}
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}

let component = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named `InnerModule.react$Bar$component`")
ReactDOMRe.createDOMElementVariadic("div", [])
}
let component = {
let \"InnerModule$Bar$component" = (props: props<_>) => make(props)
\"InnerModule$Bar$component"
}
}
15 changes: 15 additions & 0 deletions tests/ppx/react/expected/newtype.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@obj
external makeProps: (
~a: '\"type-a",
~b: array<option<[#Foo('\"type-a")]>>,
~c: 'a,
~key: string=?,
unit,
) => {"a": '\"type-a", "b": array<option<[#Foo('\"type-a")]>>, "c": 'a} = ""
let make = (type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
ReactDOMRe.createDOMElementVariadic("div", [])
let make = {
let \"Newtype" = (\"Props": {"a": '\"type-a", "b": array<option<[#Foo('\"type-a")]>>, "c": 'a}) =>
make(~c=\"Props"["c"], ~b=\"Props"["b"], ~a=\"Props"["a"])
\"Newtype"
}
7 changes: 7 additions & 0 deletions tests/ppx/react/expected/newtype.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type props<'a, 'b, 'c> = {@optional key: string, a: 'a, b: 'b, c: 'c}
let make = (_: props<'a, 'b, 'c>, type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
ReactDOMRe.createDOMElementVariadic("div", [])
let make = {
let \"Newtype" = (props: props<_>) => make(props)
\"Newtype"
}
10 changes: 10 additions & 0 deletions tests/ppx/react/expected/topLevel.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@obj external makeProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
let make =
(@warning("-16") ~a, @warning("-16") ~b, _) => {
Js.log("This function should be named 'TopLevel.react'")
ReactDOMRe.createDOMElementVariadic("div", [])
}
let make = {
let \"TopLevel" = (\"Props": {"a": 'a, "b": 'b}) => make(~b=\"Props"["b"], ~a=\"Props"["a"], ())
\"TopLevel"
}
9 changes: 9 additions & 0 deletions tests/ppx/react/expected/topLevel.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
let make = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named 'TopLevel.react'")
ReactDOMRe.createDOMElementVariadic("div", [])
}
let make = {
let \"TopLevel" = (props: props<_>) => make(props)
\"TopLevel"
}
8 changes: 8 additions & 0 deletions tests/ppx/react/expected/typeConstraint.res_v3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@obj external makeProps: (~a: 'a, ~b: 'b, ~key: string=?, unit) => {"a": 'a, "b": 'b} = ""
let make:
type a. (~a: a, ~b: a, a) => React.element =
(~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
let make = {
let \"TypeConstraint" = (\"Props": {"a": 'a, "b": 'b}) => make(~b=\"Props"["b"], ~a=\"Props"["a"])
\"TypeConstraint"
}
8 changes: 8 additions & 0 deletions tests/ppx/react/expected/typeConstraint.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = (_: props<'a, 'b>, type a): (
(~a: a, ~b: a, a) => React.element
) => (~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
let make = {
let \"TypeConstraint" = (props: props<_>) => make(props)
\"TypeConstraint"
}

0 comments on commit 23a6ca1

Please sign in to comment.