github.com/Jeffail/benthos/v3@v3.65.0/public/bloblang/arguments_test.go (about)

     1  package bloblang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestArgumentsLength(t *testing.T) {
    10  	var a, b, c int
    11  	set := NewArgSpec().IntVar(&a).IntVar(&b).IntVar(&c)
    12  
    13  	assert.EqualError(t, set.Extract([]interface{}{0, 1}), "expected 3 arguments, received 2")
    14  	assert.EqualError(t, set.Extract([]interface{}{0, 1, 2, 3}), "expected 3 arguments, received 4")
    15  	assert.NoError(t, set.Extract([]interface{}{0, 1, 2}))
    16  }
    17  
    18  func TestArgumentTypes(t *testing.T) {
    19  	var a int
    20  	var b int64
    21  	var c float64
    22  	var d bool
    23  	var e string
    24  	var f interface{}
    25  	set := NewArgSpec().
    26  		IntVar(&a).
    27  		Int64Var(&b).
    28  		Float64Var(&c).
    29  		BoolVar(&d).
    30  		StringVar(&e).
    31  		AnyVar(&f)
    32  
    33  	testCases := []struct {
    34  		name string
    35  		args []interface{}
    36  		exp  []interface{}
    37  		err  string
    38  	}{
    39  		{
    40  			name: "bad int",
    41  			args: []interface{}{
    42  				"nope", int64(2), 3.0, true, "hello world", "and this",
    43  			},
    44  			err: `bad argument 0: expected int value, got string (nope)`,
    45  		},
    46  		{
    47  			name: "bad int64",
    48  			args: []interface{}{
    49  				int64(1), "nope", 3.0, true, "hello world", "and this",
    50  			},
    51  			err: `bad argument 1: expected int64 value, got string (nope)`,
    52  		},
    53  		{
    54  			name: "bad float64",
    55  			args: []interface{}{
    56  				int64(1), int64(2), "nope", true, "hello world", "and this",
    57  			},
    58  			err: `bad argument 2: expected float64 value, got string (nope)`,
    59  		},
    60  		{
    61  			name: "bad bool",
    62  			args: []interface{}{
    63  				int64(1), int64(2), 3.0, "nope", "hello world", "and this",
    64  			},
    65  			err: `bad argument 3: expected bool value, got string (nope)`,
    66  		},
    67  		{
    68  			name: "bad string",
    69  			args: []interface{}{
    70  				int64(1), int64(2), 3.0, true, 30, "and this",
    71  			},
    72  			err: "bad argument 4: expected string value, got int (30)",
    73  		},
    74  		{
    75  			name: "good values",
    76  			args: []interface{}{
    77  				int64(1), int64(2), 3.0, true, "hello world", "and this",
    78  			},
    79  			exp: []interface{}{
    80  				1, int64(2), 3.0, true, "hello world", "and this",
    81  			},
    82  		},
    83  	}
    84  
    85  	for _, test := range testCases {
    86  		test := test
    87  		t.Run(test.name, func(t *testing.T) {
    88  			err := set.Extract(test.args)
    89  			if len(test.err) > 0 {
    90  				assert.EqualError(t, err, test.err)
    91  			} else {
    92  				assert.NoError(t, err)
    93  				assert.Equal(t, test.exp, []interface{}{
    94  					a, b, c, d, e, f,
    95  				})
    96  			}
    97  		})
    98  	}
    99  }