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

     1  package configstruct_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/rclone/rclone/fs"
     8  	"github.com/rclone/rclone/fs/config/configstruct"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  type conf struct {
    14  	A string
    15  	B string
    16  }
    17  
    18  type conf2 struct {
    19  	PotatoPie      string `config:"spud_pie"`
    20  	BeanStew       bool
    21  	RaisinRoll     int
    22  	SausageOnStick int64
    23  	ForbiddenFruit uint
    24  	CookingTime    fs.Duration
    25  	TotalWeight    fs.SizeSuffix
    26  }
    27  
    28  func TestItemsError(t *testing.T) {
    29  	_, err := configstruct.Items(nil)
    30  	assert.EqualError(t, err, "argument must be a pointer")
    31  	_, err = configstruct.Items(new(int))
    32  	assert.EqualError(t, err, "argument must be a pointer to a struct")
    33  }
    34  
    35  func TestItems(t *testing.T) {
    36  	in := &conf2{
    37  		PotatoPie:      "yum",
    38  		BeanStew:       true,
    39  		RaisinRoll:     42,
    40  		SausageOnStick: 101,
    41  		ForbiddenFruit: 6,
    42  		CookingTime:    fs.Duration(42 * time.Second),
    43  		TotalWeight:    fs.SizeSuffix(17 << 20),
    44  	}
    45  	got, err := configstruct.Items(in)
    46  	require.NoError(t, err)
    47  	want := []configstruct.Item{
    48  		{Name: "spud_pie", Field: "PotatoPie", Num: 0, Value: string("yum")},
    49  		{Name: "bean_stew", Field: "BeanStew", Num: 1, Value: true},
    50  		{Name: "raisin_roll", Field: "RaisinRoll", Num: 2, Value: int(42)},
    51  		{Name: "sausage_on_stick", Field: "SausageOnStick", Num: 3, Value: int64(101)},
    52  		{Name: "forbidden_fruit", Field: "ForbiddenFruit", Num: 4, Value: uint(6)},
    53  		{Name: "cooking_time", Field: "CookingTime", Num: 5, Value: fs.Duration(42 * time.Second)},
    54  		{Name: "total_weight", Field: "TotalWeight", Num: 6, Value: fs.SizeSuffix(17 << 20)},
    55  	}
    56  	assert.Equal(t, want, got)
    57  }
    58  
    59  func TestSetBasics(t *testing.T) {
    60  	c := &conf{A: "one", B: "two"}
    61  	err := configstruct.Set(configMap{}, c)
    62  	require.NoError(t, err)
    63  	assert.Equal(t, &conf{A: "one", B: "two"}, c)
    64  }
    65  
    66  // a simple configmap.Getter for testing
    67  type configMap map[string]string
    68  
    69  // Get the value
    70  func (c configMap) Get(key string) (value string, ok bool) {
    71  	value, ok = c[key]
    72  	return value, ok
    73  }
    74  
    75  func TestSetMore(t *testing.T) {
    76  	c := &conf{A: "one", B: "two"}
    77  	m := configMap{
    78  		"a": "ONE",
    79  	}
    80  	err := configstruct.Set(m, c)
    81  	require.NoError(t, err)
    82  	assert.Equal(t, &conf{A: "ONE", B: "two"}, c)
    83  }
    84  
    85  func TestSetFull(t *testing.T) {
    86  	in := &conf2{
    87  		PotatoPie:      "yum",
    88  		BeanStew:       true,
    89  		RaisinRoll:     42,
    90  		SausageOnStick: 101,
    91  		ForbiddenFruit: 6,
    92  		CookingTime:    fs.Duration(42 * time.Second),
    93  		TotalWeight:    fs.SizeSuffix(17 << 20),
    94  	}
    95  	m := configMap{
    96  		"spud_pie":         "YUM",
    97  		"bean_stew":        "FALSE",
    98  		"raisin_roll":      "43 ",
    99  		"sausage_on_stick": " 102 ",
   100  		"forbidden_fruit":  "0x7",
   101  		"cooking_time":     "43s",
   102  		"total_weight":     "18M",
   103  	}
   104  	want := &conf2{
   105  		PotatoPie:      "YUM",
   106  		BeanStew:       false,
   107  		RaisinRoll:     43,
   108  		SausageOnStick: 102,
   109  		ForbiddenFruit: 7,
   110  		CookingTime:    fs.Duration(43 * time.Second),
   111  		TotalWeight:    fs.SizeSuffix(18 << 20),
   112  	}
   113  	err := configstruct.Set(m, in)
   114  	require.NoError(t, err)
   115  	assert.Equal(t, want, in)
   116  }