1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-12-05 22:26:09 +02:00

Added support of optional chaining (#634)

This commit is contained in:
Tim Voronov
2021-07-16 14:46:08 -04:00
committed by GitHub
parent 742bdae0ae
commit 1d0617eb3b
8 changed files with 458 additions and 287 deletions

View File

@@ -385,6 +385,118 @@ RETURN o1.first["second"][o2.prop].fourth["fifth"]["bottom"]
So(string(out), ShouldEqual, "1")
})
})
Convey("Optional chaining", t, func() {
Convey("Object", func() {
Convey("When value does not exist", func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = { foo: None }
RETURN obj.foo?.bar
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `null`)
})
Convey("When value does exists", func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = { foo: { bar: "bar" } }
RETURN obj.foo?.bar
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"bar"`)
})
})
Convey("Array", func() {
Convey("When value does not exist", func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = { foo: None }
RETURN obj.foo?.bar?.[0]
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `null`)
})
Convey("When value does exists", func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = { foo: { bar: ["bar"] } }
RETURN obj.foo?.bar?.[0]
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"bar"`)
})
})
Convey("Function", func() {
Convey("When value does not exist", func() {
c := compiler.New()
p, err := c.Compile(`
RETURN FIRST([])?.foo
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `null`)
})
Convey("When value does exists", func() {
c := compiler.New()
p, err := c.Compile(`
RETURN FIRST([{ foo: "bar" }])?.foo
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"bar"`)
})
})
})
}
func BenchmarkMemberArray(b *testing.B) {