github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/fmt_test.go (about) 1 package strings_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/runtime/core" 10 "github.com/MontFerret/ferret/pkg/runtime/values" 11 "github.com/MontFerret/ferret/pkg/stdlib/strings" 12 ) 13 14 type testCase struct { 15 Name string 16 Expected string 17 Format string 18 Args []core.Value 19 ShouldErr bool 20 } 21 22 func TestFmt(t *testing.T) { 23 tcs := []*testCase{ 24 &testCase{ 25 Name: `FMT("{}", 1) return "1"`, 26 Expected: "1", 27 Format: "{}", 28 Args: []core.Value{ 29 values.NewInt(1), 30 }, 31 }, 32 &testCase{ 33 Name: `FMT("{1} {} {0} {}", 1, 2) return "2 1 1 2"`, 34 Expected: "2 1 1 2", 35 Format: "{1} {} {0} {}", 36 Args: []core.Value{ 37 values.NewInt(1), 38 values.NewInt(2), 39 }, 40 }, 41 &testCase{ 42 Name: `FMT("{1} {} {0} {} {}", 1, 2, 3) return "2 1 1 2 3"`, 43 Expected: "2 1 1 2 3", 44 Format: "{1} {} {0} {} {}", 45 Args: []core.Value{ 46 values.NewInt(1), 47 values.NewInt(2), 48 values.NewInt(3), 49 }, 50 }, 51 &testCase{ 52 Name: `FMT("{2}{1} {0}", "World!", ",", "Hello") return "Hello, World!"`, 53 Expected: "Hello, World!", 54 Format: "{2}{1} {0}", 55 Args: []core.Value{ 56 values.NewString("World!"), 57 values.NewString(","), 58 values.NewString("Hello"), 59 }, 60 }, 61 &testCase{ 62 Name: `FMT({}, {key:"value"}) return "{"key":"value"}"`, 63 Expected: `{"key":"value"}`, 64 Format: "{}", 65 Args: []core.Value{ 66 values.NewObjectWith( 67 values.NewObjectProperty( 68 "key", values.NewString("value"), 69 ), 70 ), 71 }, 72 }, 73 &testCase{ 74 Name: `FMT({}, {key:"value"}) return "{"key":"value"}"`, 75 Expected: `{"key":"value","yek":"eulav"}`, 76 Format: "{}", 77 Args: []core.Value{ 78 values.NewObjectWith( 79 values.NewObjectProperty( 80 "key", values.NewString("value"), 81 ), 82 values.NewObjectProperty( 83 "yek", values.NewString("eulav"), 84 ), 85 ), 86 }, 87 }, 88 &testCase{ 89 Name: `FMT("string") return "string"`, 90 Expected: "string", 91 Format: "string", 92 }, 93 &testCase{ 94 Name: `FMT("string") return "string"`, 95 Expected: "string", 96 Format: "string", 97 Args: []core.Value{ 98 values.NewInt(1), 99 }, 100 }, 101 &testCase{ 102 Name: `FMT("{}") return error`, 103 Format: "{}", 104 Args: []core.Value{}, 105 ShouldErr: true, 106 }, 107 &testCase{ 108 Name: `FMT("{1}", 10) return error`, 109 Format: "{1}", 110 Args: []core.Value{ 111 values.NewInt(10), 112 }, 113 ShouldErr: true, 114 }, 115 &testCase{ 116 Name: `FMT("{1} {} {0} {}", 1, 2, 3) return error`, 117 Format: "{1} {} {0} {}", 118 Args: []core.Value{ 119 values.NewInt(1), 120 values.NewInt(2), 121 values.NewInt(3), 122 }, 123 ShouldErr: true, 124 }, 125 } 126 127 for _, tc := range tcs { 128 tc.Do(t) 129 } 130 } 131 132 func (tc *testCase) Do(t *testing.T) { 133 Convey(tc.Name, t, func() { 134 var expected core.Value = values.NewString(tc.Expected) 135 136 args := []core.Value{values.NewString(tc.Format)} 137 args = append(args, tc.Args...) 138 139 formatted, err := strings.Fmt(context.Background(), args...) 140 141 if tc.ShouldErr { 142 So(err, ShouldBeError) 143 expected = values.None 144 } else { 145 So(err, ShouldBeNil) 146 } 147 148 So(formatted, ShouldEqual, expected) 149 }) 150 }