github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_str_test.go (about)

     1  package compiler_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"testing"
     8  
     9  	. "github.com/smartystreets/goconvey/convey"
    10  
    11  	"github.com/MontFerret/ferret/pkg/compiler"
    12  )
    13  
    14  func TestString(t *testing.T) {
    15  	Convey("Should be possible to use multi line string", t, func() {
    16  		out := compiler.New().
    17  			MustCompile(`
    18  			RETURN "
    19  FOO
    20  BAR
    21  "
    22  		`).
    23  			MustRun(context.Background())
    24  
    25  		So(string(out), ShouldEqual, `"\nFOO\nBAR\n"`)
    26  	})
    27  
    28  	Convey("Should be possible to use multi line string with nested strings using backtick", t, func() {
    29  		compiler.New().
    30  			MustCompile(fmt.Sprintf(`
    31  RETURN %s<!DOCTYPE html>
    32  		<html lang="en">
    33  		<head>
    34  		<meta charset="UTF-8">
    35  		<title>GetTitle</title>
    36  		</head>
    37  		<body>
    38  			Hello world
    39  		</body>
    40  		</html>%s
    41  `, "`", "`")).
    42  			MustRun(context.Background())
    43  
    44  		out, err := json.Marshal(`<!DOCTYPE html>
    45  		<html lang="en">
    46  		<head>
    47  		<meta charset="UTF-8">
    48  		<title>GetTitle</title>
    49  		</head>
    50  		<body>
    51  			Hello world
    52  		</body>
    53  		</html>`)
    54  
    55  		So(err, ShouldBeNil)
    56  
    57  		So(string(out), ShouldEqual, string(out))
    58  	})
    59  
    60  	Convey("Should be possible to use multi line string with nested strings using tick", t, func() {
    61  		compiler.New().
    62  			MustCompile(fmt.Sprintf(`
    63  RETURN %s<!DOCTYPE html>
    64  		<html lang="en">
    65  		<head>
    66  		<meta charset="UTF-8">
    67  		<title>GetTitle</title>
    68  		</head>
    69  		<body>
    70  			Hello world
    71  		</body>
    72  		</html>%s
    73  `, "´", "´")).
    74  			MustRun(context.Background())
    75  
    76  		out, err := json.Marshal(`<!DOCTYPE html>
    77  		<html lang="en">
    78  		<head>
    79  		<meta charset="UTF-8">
    80  		<title>GetTitle</title>
    81  		</head>
    82  		<body>
    83  			Hello world
    84  		</body>
    85  		</html>`)
    86  
    87  		So(err, ShouldBeNil)
    88  
    89  		So(string(out), ShouldEqual, string(out))
    90  	})
    91  }
    92  
    93  func BenchmarkStringLiteral(b *testing.B) {
    94  	p := compiler.New().MustCompile(`
    95  			RETURN "
    96  FOO
    97  BAR
    98  "
    99  		`)
   100  
   101  	for n := 0; n < b.N; n++ {
   102  		p.Run(context.Background())
   103  	}
   104  }