2018-10-16 00:50:55 +02:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-22 05:45:00 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-10-16 00:50:55 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/compiler"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestString(t *testing.T) {
|
|
|
|
Convey("Should be possible to use multi line string", t, func() {
|
|
|
|
out := compiler.New().
|
|
|
|
MustCompile(`
|
|
|
|
RETURN "
|
|
|
|
FOO
|
|
|
|
BAR
|
|
|
|
"
|
|
|
|
`).
|
|
|
|
MustRun(context.Background())
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, `"\nFOO\nBAR\n"`)
|
|
|
|
})
|
2018-11-22 05:45:00 +02:00
|
|
|
|
2019-09-02 03:41:16 +02:00
|
|
|
Convey("Should be possible to use multi line string with nested strings using backtick", t, func() {
|
2019-06-19 23:58:56 +02:00
|
|
|
compiler.New().
|
2018-11-22 05:45:00 +02:00
|
|
|
MustCompile(fmt.Sprintf(`
|
|
|
|
RETURN %s<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
2019-06-19 23:58:56 +02:00
|
|
|
<title>GetTitle</title>
|
2018-11-22 05:45:00 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Hello world
|
|
|
|
</body>
|
|
|
|
</html>%s
|
|
|
|
`, "`", "`")).
|
|
|
|
MustRun(context.Background())
|
|
|
|
|
|
|
|
out, err := json.Marshal(`<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
2019-06-19 23:58:56 +02:00
|
|
|
<title>GetTitle</title>
|
2018-11-22 05:45:00 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Hello world
|
|
|
|
</body>
|
|
|
|
</html>`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, string(out))
|
|
|
|
})
|
2019-09-02 03:41:16 +02:00
|
|
|
|
|
|
|
Convey("Should be possible to use multi line string with nested strings using tick", t, func() {
|
|
|
|
compiler.New().
|
|
|
|
MustCompile(fmt.Sprintf(`
|
|
|
|
RETURN %s<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>GetTitle</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Hello world
|
|
|
|
</body>
|
|
|
|
</html>%s
|
|
|
|
`, "´", "´")).
|
|
|
|
MustRun(context.Background())
|
|
|
|
|
|
|
|
out, err := json.Marshal(`<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>GetTitle</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
Hello world
|
|
|
|
</body>
|
|
|
|
</html>`)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(string(out), ShouldEqual, string(out))
|
|
|
|
})
|
2018-10-16 00:50:55 +02:00
|
|
|
}
|
2018-10-25 01:40:57 +02:00
|
|
|
|
|
|
|
func BenchmarkStringLiteral(b *testing.B) {
|
|
|
|
p := compiler.New().MustCompile(`
|
|
|
|
RETURN "
|
|
|
|
FOO
|
|
|
|
BAR
|
|
|
|
"
|
|
|
|
`)
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
p.Run(context.Background())
|
|
|
|
}
|
|
|
|
}
|