github.com/Jeffail/benthos/v3@v3.65.0/public/bloblang/method_test.go (about) 1 package bloblang 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestTypedMethods(t *testing.T) { 12 testCases := []struct { 13 name string 14 fn Method 15 in interface{} 16 exp interface{} 17 err string 18 }{ 19 { 20 name: "bad int64", 21 fn: Int64Method(func(i int64) (interface{}, error) { 22 return i, nil 23 }), 24 in: "not an int", 25 err: `expected number value, got string ("not an int")`, 26 }, 27 { 28 name: "good int64", 29 fn: Int64Method(func(i int64) (interface{}, error) { 30 return i * 2, nil 31 }), 32 in: 5, 33 exp: int64(10), 34 }, 35 { 36 name: "bad float64", 37 fn: Float64Method(func(f float64) (interface{}, error) { 38 return f, nil 39 }), 40 in: "not a float", 41 err: `expected number value, got string ("not a float")`, 42 }, 43 { 44 name: "good float64", 45 fn: Float64Method(func(f float64) (interface{}, error) { 46 return f * 2, nil 47 }), 48 in: 5.0, 49 exp: 10.0, 50 }, 51 { 52 name: "bad string", 53 fn: StringMethod(func(s string) (interface{}, error) { 54 return s, nil 55 }), 56 in: 5, 57 err: "expected string value, got number (5)", 58 }, 59 { 60 name: "good string", 61 fn: StringMethod(func(s string) (interface{}, error) { 62 return "yep: " + s, nil 63 }), 64 in: "hey", 65 exp: "yep: hey", 66 }, 67 { 68 name: "bad bytes", 69 fn: BytesMethod(func(s []byte) (interface{}, error) { 70 return s, nil 71 }), 72 in: 5, 73 err: "expected bytes value, got number (5)", 74 }, 75 { 76 name: "good bytes", 77 fn: BytesMethod(func(s []byte) (interface{}, error) { 78 return append([]byte("yep: "), s...), nil 79 }), 80 in: []byte("hey"), 81 exp: []byte("yep: hey"), 82 }, 83 { 84 name: "bad bool", 85 fn: BoolMethod(func(b bool) (interface{}, error) { 86 return b, nil 87 }), 88 in: "nope", 89 err: `expected bool value, got string ("nope")`, 90 }, 91 { 92 name: "good bool", 93 fn: BoolMethod(func(b bool) (interface{}, error) { 94 return !b, nil 95 }), 96 in: true, 97 exp: false, 98 }, 99 { 100 name: "bad object", 101 fn: ObjectMethod(func(o map[string]interface{}) (interface{}, error) { 102 return o, nil 103 }), 104 in: 5, 105 err: "expected object value, got number (5)", 106 }, 107 { 108 name: "bad array", 109 fn: ArrayMethod(func(a []interface{}) (interface{}, error) { 110 return a, nil 111 }), 112 in: 5, 113 err: "expected array value, got number (5)", 114 }, 115 { 116 name: "bad timestamp", 117 fn: TimestampMethod(func(t time.Time) (interface{}, error) { 118 return t, nil 119 }), 120 in: "not a timestamp", 121 err: "parsing time \"not a timestamp\" as \"2006-01-02T15:04:05.999999999Z07:00\": cannot parse \"not a timestamp\" as \"2006\"", 122 }, 123 } 124 125 for _, test := range testCases { 126 test := test 127 t.Run(test.name, func(t *testing.T) { 128 out, err := test.fn(test.in) 129 if len(test.err) > 0 { 130 assert.EqualError(t, err, test.err) 131 } else { 132 require.NoError(t, err) 133 assert.Equal(t, test.exp, out) 134 } 135 }) 136 } 137 }