github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_cases_test.go (about) 1 package compiler_test 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 "testing" 8 9 "golang.org/x/text/cases" 10 "golang.org/x/text/language" 11 12 "github.com/MontFerret/ferret/pkg/compiler" 13 "github.com/MontFerret/ferret/pkg/stdlib/arrays" 14 15 . "github.com/smartystreets/goconvey/convey" 16 ) 17 18 func TestCases(t *testing.T) { 19 20 Convey("Should compile", t, func() { 21 22 Convey("String variable should be case-sensitive", func() { 23 param := "String" 24 query := fmt.Sprintf(` 25 let a = "%s" 26 return a 27 `, param) 28 29 res := compiler.New(). 30 MustCompile(query). 31 MustRun(context.Background()) 32 33 So(string(res), ShouldEqual, `"String"`) 34 }) 35 36 Convey("Should work at any case", func() { 37 38 testCases := []struct { 39 Query string 40 Result string 41 }{ 42 // Key-word only 43 { 44 Query: "return 2 * 2", 45 Result: "4", 46 }, 47 // With function 48 { 49 Query: "return last([7, 2, 99])", 50 Result: "99", 51 }, 52 // With namespace 53 { 54 Query: "return x::last([7, 2, 99])", 55 Result: "99", 56 }, 57 } 58 59 strCases := map[string]func(string) string{ 60 "Mixed": func(s string) string { 61 // Capitalize string. 62 // Source: https://stackoverflow.com/questions/33696034/make-first-letter-of-words-uppercase-in-a-string 63 // return strings.Title(strings.ToLower(s)) 64 return cases.Title(language.English).String(strings.ToLower(s)) 65 }, 66 "Upper": strings.ToUpper, 67 "Lower": strings.ToLower, 68 } 69 70 for _, tC := range testCases { 71 tC := tC 72 73 for strCase, toCase := range strCases { 74 query := toCase(tC.Query) 75 tcName := fmt.Sprintf(`%s: "%s"`, strCase, query) 76 77 Convey(tcName, func() { 78 79 compiler := compiler.New() 80 compiler.Namespace("X").RegisterFunction("LAST", arrays.Last) 81 82 res := compiler. 83 MustCompile(query). 84 MustRun(context.Background()) 85 86 So(string(res), ShouldEqual, tC.Result) 87 }) 88 } 89 } 90 }) 91 }) 92 93 Convey("Should not compile", t, func() { 94 95 Convey("Variable should be case-sensitive", func() { 96 _, err := compiler.New(). 97 Compile( 98 ` 99 let a = 1 100 return A 101 `) 102 103 So(err, ShouldNotBeNil) 104 }) 105 }) 106 }