Skip to content

Commit

Permalink
Feature/#263 waitfor event (#590)
Browse files Browse the repository at this point in the history
* Added new WAITFOR syntax

* Added support of event options

* Added support of options

* Added support of using WAITFOR EVENT in variable assignment
  • Loading branch information
ziflex committed Jul 14, 2021
1 parent e6c18eb commit 742bdae
Show file tree
Hide file tree
Showing 36 changed files with 5,746 additions and 4,376 deletions.
5 changes: 5 additions & 0 deletions e2e/pages/dynamic/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IframePage from './pages/iframes/index.js';
import MediaPage from './pages/media/index.js';
import PaginationPage from './pages/pagination/index.js';
import ListsPage from './pages/lists/index.js';
import NavigationPage from './pages/navigation/index.js';

const e = React.createElement;
const Router = ReactRouter.Router;
Expand Down Expand Up @@ -66,6 +67,10 @@ export default React.memo(function AppComponent(params = {}) {
path: '/lists',
component: ListsPage
}),
e(Route, {
path: '/navigation',
component: NavigationPage
}),
]),
redirectTo
])
Expand Down
44 changes: 44 additions & 0 deletions e2e/pages/dynamic/components/pages/navigation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const e = React.createElement;

export default class NavigationPage extends React.Component {
constructor(props) {
super(props);

this.state = {
url: "",
};

this.handleTextInput = (evt) => {
evt.preventDefault();

this.setState({
url: evt.target.value
});
};

this.handleClick = () => {
window.location.href = this.state.url;
};
}

render() {
return e("div", { id: "navigation" }, [
e("div", { className: "form-group" }, [
e("label", null, "Url"),
e("input", {
id: "url",
type: "text",
className: "form-control",
onChange: this.handleTextInput
}),
e("input", {
id: "submit",
type: "button",
value: "Go",
className: "form-control",
onClick: this.handleClick
}),
])
])
}
}
12 changes: 12 additions & 0 deletions e2e/tests/dynamic/doc/waitfor_event/frame_navigation.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LET url = @lab.cdn.dynamic + "?redirect=/iframe&src=/iframe"
LET page = DOCUMENT(url, { driver: 'cdp' })
LET original = FIRST(FRAMES(page, "name", "nested"))

INPUT(original, "#url_input", "https://getbootstrap.com/")
CLICK(original, "#submit")

WAITFOR EVENT "navigation" IN page OPTIONS { frame: original }

LET current = FIRST(FRAMES(page, "name", "nested"))

RETURN T::EQ(current.URL, "https://getbootstrap.com/")
9 changes: 9 additions & 0 deletions e2e/tests/dynamic/doc/waitfor_event/navigation.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LET url = @lab.cdn.dynamic + "/#/navigation"
LET page = DOCUMENT(url, { driver: 'cdp' })

INPUT(page, "#url", "https://getbootstrap.com/")
CLICK(page, "#submit")

WAITFOR EVENT "navigation" IN page

RETURN T::EQ(page.URL, "https://getbootstrap.com/")
2 changes: 1 addition & 1 deletion pkg/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *Compiler) Compile(query string) (program *runtime.Program, err error) {
}()

p := parser.New(query)
p.AddErrorListener(&errorListener{})
p.AddErrorListener(newErrorListener())

l := newVisitor(query, c.funcs)

Expand Down
14 changes: 14 additions & 0 deletions pkg/compiler/compiler_let_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,18 @@ func TestLet(t *testing.T) {

So(err, ShouldNotBeNil)
})

Convey("Should use value returned from WAITFOR EVENT", t, func() {
out, err := newCompilerWithObservable().MustCompile(`
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event", "data", 100)
LET res = (WAITFOR EVENT "event" IN obj)
RETURN res
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"data"`)
})
}
2 changes: 1 addition & 1 deletion pkg/compiler/compiler_like_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestLikeOperator(t *testing.T) {
c := compiler.New()

out1, err := c.MustCompile(`
RETURN true ? false : ("foo" NOT LIKE "b*")s
RETURN true ? false : ("foo" NOT LIKE "b*")
`).Run(context.Background())

So(err, ShouldBeNil)
Expand Down
60 changes: 45 additions & 15 deletions pkg/compiler/compiler_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,56 @@ func TestMember(t *testing.T) {
Convey("Deep computed path", func() {
c := compiler.New()

p, err := c.Compile(`
LET obj = {
first: {
second: {
third: {
fourth: {
fifth: {
bottom: true
}
}
}
}
}
}
p := c.MustCompile(`
LET o1 = {
first: {
second: {
["third"]: {
fourth: {
fifth: {
bottom: true
}
}
}
}
}
}
LET o2 = { prop: "third" }
RETURN obj["first"]["second"]["third"]["fourth"]["fifth"].bottom
RETURN o1["first"]["second"][o2.prop]["fourth"]["fifth"].bottom
`)

out, err := p.Run(context.Background())

So(err, ShouldBeNil)

So(string(out), ShouldEqual, `true`)
})

Convey("Deep computed path 2", func() {
c := compiler.New()

p := c.MustCompile(`
LET o1 = {
first: {
second: {
third: {
fourth: {
fifth: {
bottom: true
}
}
}
}
}
}
LET o2 = { prop: "third" }
RETURN o1.first["second"][o2.prop].fourth["fifth"]["bottom"]
`)

out, err := p.Run(context.Background())

So(err, ShouldBeNil)
Expand Down
14 changes: 14 additions & 0 deletions pkg/compiler/compiler_return_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,18 @@ func TestReturn(t *testing.T) {
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{\"a\":\"foo\"}")
})

Convey("Should compile RETURN (WAITFOR EVENT \"event\" IN obj)", t, func() {
c := newCompilerWithObservable()

out, err := c.MustCompile(`
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event", "data", 100)
RETURN (WAITFOR EVENT "event" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"data"`)
})
}
2 changes: 1 addition & 1 deletion pkg/compiler/compiler_ternary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTernaryOperator(t *testing.T) {
c := compiler.New()
p, err := c.Compile(`
FOR i IN [1, 2, 3, 4, 5, 6]
RETURN i < 3 ? i * 3 : i * 2;
RETURN i < 3 ? i * 3 : i * 2
`)

So(err, ShouldBeNil)
Expand Down
110 changes: 110 additions & 0 deletions pkg/compiler/compiler_waitfor_event_ternary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package compiler_test

import (
"context"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestWaitforEventWithinTernaryExpression(t *testing.T) {
Convey("RETURN foo ? TRUE : (WAITFOR EVENT \"event\" IN obj)", t, func() {
c := newCompilerWithObservable()

out1, err := c.MustCompile(`
LET foo = FALSE
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event", "data", 100)
RETURN foo ? TRUE : (WAITFOR EVENT "event" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out1), ShouldEqual, `"data"`)

out2, err := c.MustCompile(`
LET foo = TRUE
LET obj = X::CREATE()
RETURN foo ? TRUE : (WAITFOR EVENT "event" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out2), ShouldEqual, `true`)
})

Convey("RETURN foo ? (WAITFOR EVENT \"event1\" IN obj) : (WAITFOR EVENT \"event2\" IN obj)", t, func() {
c := newCompilerWithObservable()

out1, err := c.MustCompile(`
LET foo = FALSE
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event2", "data2", 100)
RETURN foo ? (WAITFOR EVENT "event1" IN obj) : (WAITFOR EVENT "event2" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out1), ShouldEqual, `"data2"`)

out2, err := c.MustCompile(`
LET foo = TRUE
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event1", "data1", 100)
RETURN foo ? (WAITFOR EVENT "event1" IN obj) : (WAITFOR EVENT "event2" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out2), ShouldEqual, `"data1"`)
})

Convey("RETURN foo ? (FOR i IN 1..3 RETURN i*2) : (WAITFOR EVENT \"event2\" IN obj)", t, func() {
c := newCompilerWithObservable()

out1, err := c.MustCompile(`
LET foo = FALSE
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event", "data", 100)
RETURN foo ? (FOR i IN 1..3 RETURN i*2) : (WAITFOR EVENT "event" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out1), ShouldEqual, `"data"`)

out2, err := c.MustCompile(`
LET foo = TRUE
LET obj = X::CREATE()
RETURN foo ? (FOR i IN 1..3 RETURN i*2) : (WAITFOR EVENT "event" IN obj)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out2), ShouldEqual, `[2,4,6]`)
})

Convey("RETURN foo ? (WAITFOR EVENT \"event\" IN obj) : (FOR i IN 1..3 RETURN i*2) ", t, func() {
c := newCompilerWithObservable()

out1, err := c.MustCompile(`
LET foo = FALSE
LET obj = X::CREATE()
RETURN foo ? (WAITFOR EVENT "event" IN obj) : (FOR i IN 1..3 RETURN i*2)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out1), ShouldEqual, `[2,4,6]`)

out2, err := c.MustCompile(`
LET foo = TRUE
LET obj = X::CREATE()
X::EMIT_WITH(obj, "event", "data", 100)
RETURN foo ? (WAITFOR EVENT "event" IN obj) : (FOR i IN 1..3 RETURN i*2)
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out2), ShouldEqual, `"data"`)
})
}
Loading

0 comments on commit 742bdae

Please sign in to comment.