github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fs/config/configstruct/internal_test.go (about)

     1  package configstruct
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestCamelToSnake(t *testing.T) {
    12  	for _, test := range []struct {
    13  		in   string
    14  		want string
    15  	}{
    16  		{"", ""},
    17  		{"Type", "type"},
    18  		{"AuthVersion", "auth_version"},
    19  		{"AccessKeyID", "access_key_id"},
    20  	} {
    21  		got := camelToSnake(test.in)
    22  		assert.Equal(t, test.want, got, test.in)
    23  	}
    24  }
    25  
    26  func TestStringToInterface(t *testing.T) {
    27  	item := struct{ A int }{2}
    28  	for _, test := range []struct {
    29  		in   string
    30  		def  interface{}
    31  		want interface{}
    32  		err  string
    33  	}{
    34  		{"", string(""), "", ""},
    35  		{"   string   ", string(""), "   string   ", ""},
    36  		{"123", int(0), int(123), ""},
    37  		{"0x123", int(0), int(0x123), ""},
    38  		{"   0x123   ", int(0), int(0x123), ""},
    39  		{"-123", int(0), int(-123), ""},
    40  		{"0", false, false, ""},
    41  		{"1", false, true, ""},
    42  		{"FALSE", false, false, ""},
    43  		{"true", false, true, ""},
    44  		{"123", uint(0), uint(123), ""},
    45  		{"123", int64(0), int64(123), ""},
    46  		{"123x", int64(0), nil, "parsing \"123x\" as int64 failed: expected newline"},
    47  		{"truth", false, nil, "parsing \"truth\" as bool failed: syntax error scanning boolean"},
    48  		{"struct", item, nil, "parsing \"struct\" as struct { A int } failed: can't scan type: *struct { A int }"},
    49  	} {
    50  		what := fmt.Sprintf("parse %q as %T", test.in, test.def)
    51  		got, err := StringToInterface(test.def, test.in)
    52  		if test.err == "" {
    53  			require.NoError(t, err, what)
    54  			assert.Equal(t, test.want, got, what)
    55  		} else {
    56  			assert.Nil(t, got)
    57  			assert.EqualError(t, err, test.err, what)
    58  		}
    59  	}
    60  }