...

Source file src/github.com/ory/x/castx/castx_test.go

Documentation: github.com/ory/x/castx

     1  package castx
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestToFloatSliceE(t *testing.T) {
    11  	tests := []struct {
    12  		input  interface{}
    13  		expect []float64
    14  		iserr  bool
    15  	}{
    16  		{[]int{1, 3}, []float64{1, 3}, false},
    17  		{[]interface{}{1.2, 3.2}, []float64{1.2, 3.2}, false},
    18  		{[]string{"2", "3"}, []float64{2, 3}, false},
    19  		{[]string{"2.2", "3.2"}, []float64{2.2, 3.2}, false},
    20  		{[2]string{"2", "3"}, []float64{2, 3}, false},
    21  		{[2]string{"2.2", "3.2"}, []float64{2.2, 3.2}, false},
    22  		// errors
    23  		{nil, nil, true},
    24  		{testing.T{}, nil, true},
    25  		{[]string{"foo", "bar"}, nil, true},
    26  	}
    27  
    28  	for i, test := range tests {
    29  		errmsg := fmt.Sprintf("i = %d", i) // assert helper message
    30  
    31  		v, err := ToFloatSliceE(test.input)
    32  		if test.iserr {
    33  			assert.Error(t, err, errmsg)
    34  			continue
    35  		}
    36  
    37  		assert.NoError(t, err, errmsg)
    38  		assert.Equal(t, test.expect, v, errmsg)
    39  
    40  		// Non-E test
    41  		v = ToFloatSlice(test.input)
    42  		assert.Equal(t, test.expect, v, errmsg)
    43  	}
    44  }
    45  
    46  func TestToStringSlice(t *testing.T) {
    47  	assert.Equal(t, []string{"foo", "bar"}, ToStringSlice("foo,bar"))
    48  }
    49  

View as plain text