github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/vfs/vfscommon/cachemode_test.go (about) 1 package vfscommon 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "testing" 7 8 "github.com/spf13/pflag" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 // Check CacheMode it satisfies the pflag interface 13 var _ pflag.Value = (*CacheMode)(nil) 14 15 // Check CacheMode it satisfies the json.Unmarshaller interface 16 var _ json.Unmarshaler = (*CacheMode)(nil) 17 18 func TestCacheModeString(t *testing.T) { 19 assert.Equal(t, "off", CacheModeOff.String()) 20 assert.Equal(t, "full", CacheModeFull.String()) 21 assert.Equal(t, "Unknown(17)", CacheMode(17).String()) 22 } 23 24 func TestCacheModeSet(t *testing.T) { 25 var m CacheMode 26 27 err := m.Set("full") 28 assert.NoError(t, err) 29 assert.Equal(t, CacheModeFull, m) 30 31 err = m.Set("potato") 32 assert.Error(t, err, "Unknown cache mode level") 33 34 err = m.Set("") 35 assert.Error(t, err, "Unknown cache mode level") 36 } 37 38 func TestCacheModeType(t *testing.T) { 39 var m CacheMode 40 assert.Equal(t, "CacheMode", m.Type()) 41 } 42 43 func TestCacheModeUnmarshalJSON(t *testing.T) { 44 var m CacheMode 45 46 err := json.Unmarshal([]byte(`"full"`), &m) 47 assert.NoError(t, err) 48 assert.Equal(t, CacheModeFull, m) 49 50 err = json.Unmarshal([]byte(`"potato"`), &m) 51 assert.Error(t, err, "Unknown cache mode level") 52 53 err = json.Unmarshal([]byte(`""`), &m) 54 assert.Error(t, err, "Unknown cache mode level") 55 56 err = json.Unmarshal([]byte(strconv.Itoa(int(CacheModeFull))), &m) 57 assert.NoError(t, err) 58 assert.Equal(t, CacheModeFull, m) 59 60 err = json.Unmarshal([]byte("-1"), &m) 61 assert.Error(t, err, "Unknown cache mode level") 62 63 err = json.Unmarshal([]byte("99"), &m) 64 assert.Error(t, err, "Unknown cache mode level") 65 }